Example 2: List artifacts’ job events
Test negative result
Let’s modify the playbook so that it will fail. For example (8)
1shell> cat ~/.ansible/runner/test_02/project/pb-01.yml
2- hosts: test_02
3 remote_user: admin
4 gather_facts: false
5 tasks:
6 - debug:
7 msg: TEST
8 - command: /usr/bin/false
Cron email on failure
Then the cron task in the example Cron: Example 1 will fail and admin will receive an email similar to this one
1Date: Wed, 8 Jul 2020 13:27:07 +0200 (CEST)
2From: Cron Daemon <root@cntrlr.example.com>
3To: admin@cntrlr.example.com
4Subject: Cron <admin@cntrlr> $HOME/bin/ansible-cron-test.bash
5
6[OK] test_01 pb-01.yml PASSED
7[ERR] 2020-07-08 13:27:03 /home/admin/bin/arwrapper.bash
8
9PLAY [test_02] **************************************************************
10
11TASK [debug] ****************************************************************
12ok: [test_02] => {
13 "msg": "TEST"
14 }
15
16TASK [command] **********************************************************
17fatal: [test_02]: FAILED! =>
18{"changed": true,
19 "cmd": ["/usr/bin/false"],
20 "delta": "0:00:00.013809",
21 "end": "2020-07-08 +13:26:32.197207",
22 "msg": "non-zero return code",
23 "rc": 1,
24 "start": "2020-07-08 13:26:32.183398",
25 "stderr": "",
26 "stderr_lines": [],
27 "stdout": "",
28 "stdout_lines": []}
29
30PLAY RECAP **************************************************************
31test_02: ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
32.........................................................................
33[OK] test_03 pb-01.yml PASSED
Artifacts
Let’s take look at the artifacts of the failed project
1shell> tree ~/.ansible/runner/test_02/artifacts/
2/home/admin/.ansible/runner/test_02/artifacts/
3└── 0428ede5-40c2-48f9-b33d-b9d1a64609af
4 ├── command
5 ├── fact_cache
6 ├── job_events
7 │ ├── 1-ff7d4af5-26bc-4d4c-8eac-6c75e547cb22.json
8 │ ├── 2-5ce0c5a2-1f02-fda4-7a07-00000000001f.json
9 │ ├── 3-5ce0c5a2-1f02-fda4-7a07-000000000021.json
10 │ ├── 4-a1e17955-d452-424d-a1c1-bb4b387fd180.json
11 │ ├── 5-97175f4b-9c82-4160-a17c-32a3e6d0c3ff.json
12 │ ├── 6-5ce0c5a2-1f02-fda4-7a07-000000000022.json
13 │ ├── 7-e1a3349e-199f-4ad7-969c-8680bbb1bac0.json
14 │ ├── 8-bb64ec8e-d1b0-4114-9093-9bbd6807b293.json
15 │ └── 9-72588652-8937-4eda-9aa7-b6bc443e4aa9.json
16 ├── rc
17 ├── status
18 └── stdout
19
203 directories, 13 files
Playbook
Prepare a playbook to help with the analysis of the artifacts. For example, the playbook below will use Ansible library task al_runner_events.yml (13) and display selected attributes (18) from the job events. Feel free to modify msg (18) and display other attributes
1shell> cat ar-events.yml
2- hosts: localhost
3 gather_facts: false
4
5 vars:
6 my_home: "{{ lookup('env','HOME') }}"
7 al_runner_events_dir: "{{ my_home ~
8 '/.ansible/runner/test_02/artifacts/0428ede5-40c2-48f9-b33d-b9d1a64609af/job_events' }}"
9
10 tasks:
11 - include_role:
12 name: vbotka.ansible_lib
13 tasks_from: al_runner_events
14 apply:
15 tags: always
16 tags: always
17 - debug:
18 msg: "{{ item.counter }} {{ item.event }}"
19 loop: "{{ al_runner_events_list|sort(attribute='counter') }}"
20 loop_control:
21 label: "{{ item.counter }}"
22 tags: events
23 - debug:
24 msg: "{{ item.stdout }}"
25 loop: "{{ al_runner_events_list|sort(attribute='counter') }}"
26 loop_control:
27 label: "{{ item.counter }}"
28 when: item.event == 'runner_on_failed'
29 tags: failed
See also
Events
The play below gives the list of the events
shell> ansible-playbook ar-events.yml -t events | grep msg\":
"msg": "1 playbook_on_start"
"msg": "2 playbook_on_play_start"
"msg": "3 playbook_on_task_start"
"msg": "4 runner_on_start"
"msg": "5 runner_on_ok"
"msg": "6 playbook_on_task_start"
"msg": "7 runner_on_start"
"msg": "8 runner_on_failed"
"msg": "9 playbook_on_stats"
Failed event(s)
The next play displays the details of the failed event(s)
shell> echo -e $(ansible-playbook ar-events.yml -t failed | grep msg\":)
"msg": "fatal: [test_02]: FAILED! =>{
\"changed\": true,
\"cmd\": [\"/usr/bin/false\"],
\"delta\": \"0:00:00.014716\",
\"end\": \"2020-07-08 17:05:56.104764\",
\"msg\": \"non-zero return code\",
\"rc\": 1,
\"start\": \"2020-07-08 17:05:56.090048\",
\"stderr\": \"\",
\"stderr_lines\": [],
\"stdout\": \"\",
\"stdout_lines\": []}"