diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6b97b9f..eca42be 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ on: branches: [ main ] jobs: - test: + ansible-link-ci: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 @@ -28,11 +28,16 @@ jobs: touch test_inventory.ini - name: Create test playbook run: | - echo "- hosts: localhost" > test_playbooks/test_playbook.yml - echo " tasks:" >> test_playbooks/test_playbook.yml - echo " - name: Output OK" >> test_playbooks/test_playbook.yml - echo " debug:" >> test_playbooks/test_playbook.yml - echo " msg: PLAYBOOK RUN OK" >> test_playbooks/test_playbook.yml + cat << EOF > test_playbooks/test_playbook.yml + --- + - name: Test Playbook + hosts: localhost + connection: local + tasks: + - name: Print a message + debug: + msg: "This is a test playbook" + EOF - name: Set PYTHONPATH and ANSIBLE_API_CONFIG run: | echo "PYTHONPATH=$GITHUB_WORKSPACE" >> $GITHUB_ENV @@ -42,6 +47,8 @@ jobs: ls -R echo "PYTHONPATH: $PYTHONPATH" echo "ANSIBLE_API_CONFIG: $ANSIBLE_API_CONFIG" + pip list + cat test_playbooks/test_playbook.yml - name: Update import in ansible_link.py run: | sed -i 's/from webhook import WebhookSender/from ansible_link.webhook import WebhookSender/' ansible_link/ansible_link.py diff --git a/ansible_link/webhook.py b/ansible_link/webhook.py index 720ec8a..d8468b1 100644 --- a/ansible_link/webhook.py +++ b/ansible_link/webhook.py @@ -24,28 +24,43 @@ def format_payload(self, event_type, job_data): "timestamp": datetime.now().isoformat() } + if 'error' in job_data: + base_payload['error'] = job_data['error'] + if self.webhook_type == 'slack': color = "#36a64f" if job_data['status'] in ['completed', 'started'] else "#ff0000" + fields = [ + {"title": "Status", "value": job_data['status'], "short": True}, + {"title": "Job ID", "value": job_data['job_id'], "short": True} + ] + if 'error' in job_data: + fields.append({"title": "Error", "value": job_data['error'], "short": False}) + return { "attachments": [{ "color": color, - "title": f"Ansible {event_type.replace('_', ' ').title()}", - "text": f"Playbook: {job_data['playbook']}\nStatus: {job_data['status']}", - "footer": f"Ansible-Link | {job_data['job_id']}", + "title": job_data['playbook'], + "text": f"Event: {event_type.replace('_', ' ').title()}", + "fields": fields, + "footer": "Ansible-Link", "ts": int(datetime.now().timestamp()) }] } elif self.webhook_type == 'discord': color = 0x36a64f if job_data['status'] in ['completed', 'started'] else 0xff0000 + fields = [ + {"name": "Status", "value": job_data['status'], "inline": True}, + {"name": "Job ID", "value": job_data['job_id'], "inline": True}, + {"name": "Event", "value": event_type.replace('_', ' ').title(), "inline": False} + ] + if 'error' in job_data: + fields.append({"name": "Error", "value": job_data['error'], "inline": False}) + return { "embeds": [{ - "title": f"Ansible {event_type.replace('_', ' ').title()}", + "title": job_data['playbook'], "color": color, - "fields": [ - {"name": "Playbook", "value": job_data['playbook'], "inline": True}, - {"name": "Status", "value": job_data['status'], "inline": True}, - {"name": "Job ID", "value": job_data['job_id'], "inline": False} - ], + "fields": fields, "footer": {"text": "Ansible-Link"}, "timestamp": datetime.now().isoformat() }]