How to capture UNREACHBALE HOSTS message in ansible playbook

Viewed 230

I have an inventory file with 800 hosts and I need to capture each host's status, and when it's unreachable I need to save the unreachable hostname and msg to .scv file and send an email with the unreachable hostname and unreachable message. I created a playbook to save unreachable hostname to .csv, but I'm not sure how to capture the massage. Can anyone suggest how to capture the message? Thank you!

 pre_tasks:
 - setup:
     filter:
       - ansible_play_*
 - debug: var=ansible_play_hosts_all
 - debug: var=ansible_play_hosts
 - set_fact:
     unreachable_hosts: "{{ ansible_play_hosts_all|difference(ansible_play_hosts) }}"
     reachable_hosts: "{{ ansible_play_hosts }}"

 - name: copy unreachable hosts
   copy:
     content:  "{{ item }}"
     dest: "unreachable_hosts_report.csv"
   delegate_to: localhost
   loop:
     - "{{unreachable_hosts|join('\n')}}"

Here is the unreachable message example that I need to save to .scv file along with host name

UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname

1 Answers

this is the technic i am using to trap log message of playbook:

configure ansible.cfg and add log-path option:

[defaults]
inventory = hosts.yml
log_path = logs/logging.log

sample of playbook:

  pre_tasks:
    - name: launchsetup
      setup:
        filter:
          - ansible_play_*

    - set_fact:
        log: "{{ lookup('file', 'logs/logging.log').split('\n') }}"

    - debug: 
        var: log

sample log:

ok: [ansible-cible-2] => {
    "log": [
        "2021-11-16 13:35:25,598 p=6163 u=formation-ansible n=ansible | PLAY [all] *********************************************************************************************************************************************************************************",
        "2021-11-16 13:35:25,606 p=6163 u=formation-ansible n=ansible | TASK [launchsetup] *************************************************************************************************************************************************************************",
        "2021-11-16 13:35:25,606 p=6163 u=formation-ansible n=ansible | Tuesday 16 November 2021  13:35:25 +0000 (0:00:00.010)       0:00:00.010 ****** ",
        "2021-11-16 13:35:26,309 p=6163 u=formation-ansible n=ansible | [WARNING]: The value \"['ansible_play_*']\" (type list) was converted to \"u\"['ansible_play_*']\"\" (type string). If this does not look like what you expect, quote the entire value to ensure",
        "it does not change.",
        "",
        "2021-11-16 13:35:26,309 p=6163 u=formation-ansible n=ansible | ok: [ansible-cible-3]",
        "2021-11-16 13:35:26,341 p=6163 u=formation-ansible n=ansible | ok: [ansible-cible-2]",
        "2021-11-16 13:35:28,688 p=6163 u=formation-ansible n=ansible | fatal: [ansible-cible-1]: UNREACHABLE! => {\"changed\": false, \"msg\": \"Failed to connect to the host via ssh: ssh: connect to host 172.17.10.101 port 22: No route to host\", \"unreachable\": true}",
        "2021-11-16 13:35:28,694 p=6163 u=formation-ansible n=ansible | TASK [set_fact] ****************************************************************************************************************************************************************************",
        "2021-11-16 13:35:28,694 p=6163 u=formation-ansible n=ansible | Tuesday 16 November 2021  13:35:28 +0000 (0:00:03.088)       0:00:03.098 ****** ",
        "2021-11-16 13:35:28,742 p=6163 u=formation-ansible n=ansible | ok: [ansible-cible-3]"
    ]
}
ok: [ansible-cible-3] => {
    "log": [
        "2021-11-16 13:35:25,598 p=6163 u=formation-ansible n=ansible | PLAY [all] *********************************************************************************************************************************************************************************",
        "2021-11-16 13:35:25,606 p=6163 u=formation-ansible n=ansible | TASK [launchsetup] *************************************************************************************************************************************************************************",
        "2021-11-16 13:35:25,606 p=6163 u=formation-ansible n=ansible | Tuesday 16 November 2021  13:35:25 +0000 (0:00:00.010)       0:00:00.010 ****** ",
        "2021-11-16 13:35:26,309 p=6163 u=formation-ansible n=ansible | [WARNING]: The value \"['ansible_play_*']\" (type list) was converted to \"u\"['ansible_play_*']\"\" (type string). If this does not look like what you expect, quote the entire value to ensure",
        "it does not change.",
        "",
        "2021-11-16 13:35:26,309 p=6163 u=formation-ansible n=ansible | ok: [ansible-cible-3]",
        "2021-11-16 13:35:26,341 p=6163 u=formation-ansible n=ansible | ok: [ansible-cible-2]",
        "2021-11-16 13:35:28,688 p=6163 u=formation-ansible n=ansible | fatal: [ansible-cible-1]: UNREACHABLE! => {\"changed\": false, \"msg\": \"Failed to connect to the host via ssh: ssh: connect to host 172.17.10.101 port 22: No route to host\", \"unreachable\": true}",
        "2021-11-16 13:35:28,694 p=6163 u=formation-ansible n=ansible | TASK [set_fact] ****************************************************************************************************************************************************************************",
        "2021-11-16 13:35:28,694 p=6163 u=formation-ansible n=ansible | Tuesday 16 November 2021  13:35:28 +0000 (0:00:03.088)       0:00:03.098 ******"
    ]
}

so now you have a variable log which contains the log after the first task where you could search anything in it...

as you could see in my log i have a fatal error on ubreachable and i have a named task launchsetup...

dont forget to delete log file before to launch playbook, it seems to append each time..

Related