Ansible ping report

Hi, All.

It’s been some time since I used Ansible, and this list. I’m happy to be back using both.

I’ve created a [playbook to run pin, and create a report of output:

Not all modules return a stdout nor stdout_lines, ping is one of them,
it returns 'data', check the RETURN DOCS for each module to know the
structure of the registered var they return.

Thanks, Brian. I suspected that was the case (I did read doc for the module), but was hoping there was some work-around. I’m still interested in trying to generate some type of report from Ansible ping. As you can see from the playbook I posted, that uses ICMP ping, which is not what I’m after.

sorry, had only read the bottom, too used to people making that
mistake, but you did not.

Ignore unreachable hosts in a block

    - block:
        - ping:
          register: out
        - set_fact:
            unr: "{{ out.unreachable|d(false) }}"
      ignore_unreachable: true

and declare the dictionary

  h_unr: "{{ dict(ansible_play_hosts_all|
                  zip(ansible_play_hosts_all|
                      map('extract', hostvars, 'unr'))) }}"

For example, the below play

- hosts: test_01,test_05,test_06,test_07
  gather_facts: false

  vars:

    h_unr: "{{ dict(ansible_play_hosts_all|
                    zip(ansible_play_hosts_all|
                        map('extract', hostvars, 'unr'))) }}"

  tasks:

    - block:
        - ping:
          register: out
        - set_fact:
            unr: "{{ out.unreachable|d(false) }}"
      ignore_unreachable: true

    - debug:
        var: h_unr
      run_once: true
      delegate_to: localhost

gives (abridged)

ok: [test_01 -> localhost] =>
  h_unr:
    test_01: false
    test_05: true
    test_06: true
    test_07: true

would you mind sharing the whole, newly updated playbook? thank you!

Sure, here is the gist
https://gist.github.com/vbotka/10c57962976dd1e2dd3e9411d3745c75

(Is it possible to reasonably format a code in Google Groups?)

Vladimir, this is very good, and very appreciated. Your playbook looks for hosts that are pingable, and marks them as not unreachable. But, my goal is to see if hosts are Ansible-pingable (not ICMP-pingable; I believe there’s a difference) or not, then take all of the output, and create a report from it. Thus, i end up with a report of hosts that are either Ansible-pingable or not.

You may try the attached playbook i am using for linux, windows will be similar as well. The report will be in html format

Vào Th 4, 8 thg 11, 2023 vào lúc 22:22 Dimitri Yioulos <dimitri.g.yioulos@gmail.com> đã viết:

(attachments)

exportreport.yml (520 Bytes)
linux_ping.j2 (1.85 KB)
general.yml (617 Bytes)
createreport.yml (1.73 KB)
Daily_LinuxPing.yml (1.29 KB)

... my goal is to see if hosts are Ansible-pingable (not
ICMP-pingable; I believe there's a difference) or not, then take
*all* of the output, and create a report from it. Thus, i end up
with a report of hosts that are either Ansible-pingable or not.

Dimitri, the play does what you want. The code is available also here
https://gist.github.com/vbotka/10c57962976dd1e2dd3e9411d3745c75

Let me help you to understand it step by step

- hosts: test_01,test_05,test_06,test_07
  gather_facts: false

There are 4 hosts in the play. Setup is off, hence no connections to
the remote host up till now

vars:

  h_unr: "{{ dict(ansible_play_hosts_all|
                  zip(ansible_play_hosts_all|
                      map('extract', hostvars, 'unr'))) }}"

This dictionary will be evaluated when referenced in the debug task

tasks:

  - block:
      - ping:
        register: out
      - set_fact:
          unr: "{{ out.unreachable|d(false) }}"
    ignore_unreachable: true

Here come the first connections to the remote hosts. There are 2
tasks in the block. The first one is the Ansible module ping, not the
ICMP ping. (Yes, you're right. There is a difference.) The second one
is the module set_fact. Because of the ignore statement the tasks in
the block won't fail if a host is unreachable.

If a host can be reached by ping there is no attribute *unreachable*
in the registered dictionary *out*. Therefor, the value of the
variables *unr* will be the default (alias d) value *false*.

If a host can't be reached the value of the attribute *unreachable*
is *true*.

  - debug:
      var: h_unr
    run_once: true
    delegate_to: localhost

Here is the report. Let's analyse the dictionary *h_unr*. In the
above block, all hosts created the variable *unr*. When you take the
list of all hosts in the play *ansible_play_hosts_all* [1] and *map*
the function to *extract* the *hostvars* [2] variable *unr*
  
  ansible_play_hosts_all|map('extract', hostvars, 'unr')

you get a list, for example, where only the first host was reached

  [false, true, true, true]

Then, *zip* this list with *ansible_play_hosts_all*

  [[test_01,false], [test_02,true], [test_03,true], [test_04,true]]

and apply the function *dict* [3]. Below is the expected result.
Enjoy!

gives (abridged)

ok: [test_01 -> localhost] =>
h_unr:
  test_01: false
  test_05: true
  test_06: true
  test_07: true

[1]
https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html
[2]
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#selecting-values-from-arrays-or-hashtables
[3]
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#combining-items-from-multiple-lists-zip-and-zip-longest