Hi,
I have a playbook that attempts to notify via slack of hosts that were successful and those that failed. Below is a simply playbook showing what I’m trying to achieve using debug.
Ansible version: 2.6.5
---------------- Playbook -------------------
- hosts: all
tasks:
-
block:
-
command: /bin/something
-
debug:
msg: “Successfully ran on - {{ ansible_play_hosts|join(', ') }}”
run_once: True
delegate_to: localhost
rescue:
-
debug:
msg: “Failed host - {{ inventory_hostname }} - {{ ansible_failed_result.msg }}”
delegate_to: localhost -
debug:
msg: “Unreachable hosts: {{ (ansible_play_hosts_all| difference(ansible_play_hosts)|join(', ')) }}”
run_once: True
delegate_to: localhost
--------------- End ------------------------
So, as can be seen from the playbook, I’d like to show the hosts that ran the task successfully, then with rescue those that failed and why and finally those that were unreachable before the block execution. The problem I’m having is that because of the ‘rescue’ in the block, all hosts that were in the play at the start of the block remain within ansible_play_hosts so I cannot display just the successful hosts - the debug task in the block lists all hosts in the play (even the ones that failed in the previous task). Below is an example playbook run;
PLAY [all] *******************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************
fatal: [test02]: UNREACHABLE! => {“changed”: false, “msg”: “Failed to connect to the host via ssh: ssh: Could not resolve hostname test02: Name or service not known\r\n”, “unreachable”: true}
ok: [server1]
ok: [server2]
ok: [server3]
TASK [command] ***************************************************************************************************************
fatal: [server1]: FAILED! => {“changed”: false, “cmd”: “/bin/something”, “msg”: “[Errno 2] No such file or directory”, “rc”: 2}
changed: [server3]
fatal: [server2]: FAILED! => {“changed”: false, “cmd”: “/bin/something”, “msg”: “[Errno 2] No such file or directory”, “rc”: 2}
TASK [debug] *****************************************************************************************************************
ok: [server2 → localhost] => {
“msg”: “Successfully ran on - server1, server2, server3”
}
TASK [debug] *****************************************************************************************************************
ok: [server1 → localhost] => {
“msg”: “Failed host - server1 - [Errno 2] No such file or directory”
}
ok: [server2-> localhost] => {
“msg”: “Failed host - server2 - [Errno 2] No such file or directory”
}
TASK [debug] *****************************************************************************************************************
ok: [server1 → localhost] => {
“msg”: “Unreachable hosts: test02”
}
PLAY RECAP *******************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=1
server3 : ok=3 changed=1 unreachable=0 failed=0
server1 : ok=3 changed=0 unreachable=0 failed=1
test02 : ok=0 changed=0 unreachable=1 failed=0
Can anyone suggest a way for me to retrieve the hosts that were successful?