# Collecting error messages generated during fact gathering

**URL:** <https://forum.ansible.com/t/collecting-error-messages-generated-during-fact-gathering/43610>\
**Category:** Get Help\
**Created:** [June 27, 2025, 11:44pm UTC](https://forum.ansible.com/t/collecting-error-messages-generated-during-fact-gathering/43610 "2025-06-27T23:44:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlyle705](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/carlyle705/32/5552_2.png) [@carlyle705](https://forum.ansible.com/u/carlyle705)\
**Post date:** [June 27, 2025, 11:44pm UTC](https://forum.ansible.com/t/collecting-error-messages-generated-during-fact-gathering/43610/1 "2025-06-27T23:44:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mikemorency](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mikemorency/32/2672_2.png) [@mikemorency](https://forum.ansible.com/u/mikemorency)\
**Post date:** [July 11, 2025, 1:57pm UTC](https://forum.ansible.com/t/collecting-error-messages-generated-during-fact-gathering/43610/2 "2025-07-11T13:57:35Z")

</div>

Hi,

```auto
# 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:

```auto
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

---

<div class="post-metadata">

**Author:** ![MichaelK](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/michaelk/32/6962_2.png) [@MichaelK](https://forum.ansible.com/u/MichaelK)\
**Post date:** [July 11, 2025, 7:23pm UTC](https://forum.ansible.com/t/collecting-error-messages-generated-during-fact-gathering/43610/3 "2025-07-11T19:23:11Z")

</div>

@ [mikemorency](https://forum.ansible.com/u/mikemorency) Thanks I was looking for something like this. Appreciate it.
