I’m writing a playbook to install a software on 5000 machines. The hosts are added to a group dynamically and the later jobs are running on each host in the group. My question is, during the fact gathering process, if some of the hosts are not responding or something wrong happened which caused the fact gathering failed. I want to collect the error messages and use these messages in the later plays.
I’m wondering how to implement it. An example code would be very appreciated.
Hi,
# hosts
[all]
localhost ansible_connection=local
8.8.8.8
# test.yml
---
- hosts: all
gather_facts: false
tasks:
- name: Gather
ansible.builtin.setup:
ignore_errors: true
ignore_unreachable: true
register: _g
- name: Debug failed
ansible.builtin.debug:
var: _g
when: (ansible_facts | length) == 0
Running ansible-playbook -i hosts test.yml
yields:
PLAY [all] ***********************************************************************************************************
TASK [Gather] ********************************************************************************************************
ok: [localhost]
[ERROR]: Task failed: Failed to connect to the host via ssh: ssh: connect to host 8.8.8.8 port 22: Connection timed out
Origin: /home/mikemorency/test2.yml:5:7
3 gather_facts: false
4 tasks:
5 - name: Gather
^ column 7
fatal: [8.8.8.8]: UNREACHABLE! => {"changed": false, "msg": "Task failed: Failed to connect to the host via ssh: ssh: connect to host 8.8.8.8 port 22: Connection timed out", "unreachable": true}
...ignoring
TASK [Debug failed] **************************************************************************************************
skipping: [localhost]
ok: [8.8.8.8] => {
"_g": {
"changed": false,
"exception": "(traceback unavailable)",
"msg": "Task failed: Failed to connect to the host via ssh: ssh: connect to host 8.8.8.8 port 22: Connection timed out",
"unreachable": true
}
}
PLAY RECAP ***********************************************************************************************************
8.8.8.8 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
I hope this helps
1 Like
MichaelK
(Michael Kiser)
July 11, 2025, 7:23pm
3
@ mikemorency Thanks I was looking for something like this. Appreciate it.
1 Like