Ansible ping report

299 views
Skip to first unread message

Dimitri Yioulos

unread,
Nov 6, 2023, 10:04:12 AM11/6/23
to Ansible Project
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:

---
- name: check reachable hosts
hosts: dytest
gather_facts: no
tasks:
- name: Ping each host
ansible.builtin.command: ping -c1 {{ item }}
run_once: true
loop: "{{ ansible_play_hosts_all }}"
delegate_to: localhost
register: ping_result
failed_when: false

- name: How did we do?
ansible.builtin.debug:
msg: "{{ ping_result.results[0] }}"
run_once: true

- name: Format it a bit
# Note: the docs for 'copy' say don't do this, to use template instead,
# and there are reasons, but this suffices for posting purposes.
# The 'content:' below should be in your template.
ansible.builtin.copy:
dest: /home/deploy/ping.txt
content: |
--- PING REPORT ---
{% for pr in ping_result.results %}
{{ pr.stdout_lines | first }}
{{ pr.stdout_lines | last }}

{% endfor %}
run_once: true
delegate_to: localhost


That works fine. However, I want to use ansible.builtin.ping, not regular ping. When I substitute in ansible.builtin ping, I get the error:

fatal: [bed-test-9-dy1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'. 'dict object' has no attribute 'stdout'\n\nThe error appears to be in '/etc/ansible/playbooks/ping4.yml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: How did we do?\n 

What do I need to do to accomplish what I'm after?

Many thanks.

Brian Coca

unread,
Nov 6, 2023, 10:15:28 AM11/6/23
to ansible...@googlegroups.com
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.


--
----------
Brian Coca

Dimitri Yioulos

unread,
Nov 6, 2023, 10:32:31 AM11/6/23
to Ansible Project
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.

Brian Coca

unread,
Nov 6, 2023, 12:03:49 PM11/6/23
to ansible...@googlegroups.com
sorry, had only read the bottom, too used to people making that
mistake, but you did not.



--
----------
Brian Coca

Vladimir Botka

unread,
Nov 6, 2023, 2:58:31 PM11/6/23
to Dimitri Yioulos, ansible...@googlegroups.com
On Fri, 3 Nov 2023 08:40:19 -0700 (PDT)
Dimitri Yioulos <dimitri....@gmail.com> wrote:

> --- PING REPORT ---
> {% for pr in ping_result.results %}
> {{ pr.stdout_lines | first }}
> {{ pr.stdout_lines | last }}
>
> {% endfor %}
> run_once: true
> delegate_to: localhost
>
> That works fine. However, I want to use ansible.builtin.ping ...


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

--
Vladimir Botka

dbs34

unread,
Nov 7, 2023, 12:55:50 PM11/7/23
to Ansible Project
would you mind sharing the whole, newly updated playbook?  thank you!

Vladimir Botka

unread,
Nov 7, 2023, 2:15:30 PM11/7/23
to Ansible Project
Sure, here is the gist

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

Dimitri Yioulos

unread,
Nov 8, 2023, 9:22:50 AM11/8/23
to Ansible Project
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.

Thanh Nguyen Duc

unread,
Nov 8, 2023, 10:35:53 AM11/8/23
to ansible...@googlegroups.com
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....@gmail.com> đã viết:
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/47bbdd8e-7b5f-4dfc-ac60-375a532a9adbn%40googlegroups.com.
exportreport.yml
linux_ping.j2
general.yml
createreport.yml
Daily_LinuxPing.yml

Vladimir Botka

unread,
Nov 8, 2023, 4:06:55 PM11/8/23
to Dimitri Yioulos, ansible...@googlegroups.com
On Wed, 8 Nov 2023 06:22:49 -0800 (PST)
Dimitri Yioulos <dimitri....@gmail.com> wrote:

> ... 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

--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages