$ cat pingout.yml
---
# pingout.yml
# Run as: $ ansible-playbook pingout.yml -i pingout-inventory
# $ cat pingout-inventory
# $ cat /tmp/ping-report.txt
- name: check reachable hosts
hosts: remote_hosts
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: /tmp/ping-report.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
$ cat pingout-inventory
[remote_hosts]
cloister
bluemoon
lappy
dewdrop
$ ansible-playbook pingout.yml -i pingout-inventory
PLAY [check reachable hosts]
*****************************************************
TASK [Ping each host]
************************************************************
changed: [cloister -> localhost] => (item=cloister)
changed: [cloister -> localhost] => (item=bluemoon)
changed: [cloister -> localhost] => (item=lappy)
changed: [cloister -> localhost] => (item=dewdrop)
TASK [How did we do?]
************************************************************
ok: [cloister] => {
"msg": {
"ansible_loop_var": "item",
"changed": true,
"cmd": [
"ping",
"-c1",
"cloister"
],
"delta": "0:00:00.008754",
"end": "2022-11-16 09:18:34.819681",
"failed": false,
"failed_when_result": false,
"invocation": {
"module_args": {
"_raw_params": "ping -c1 cloister",
"_uses_shell": false,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": false
}
},
"item": "cloister",
"msg": "",
"rc": 0,
"start": "2022-11-16 09:18:34.810927",
"stderr": "",
"stderr_lines": [],
"stdout": "PING cloister (192.168.254.246) 56(84) bytes of
data.\n64 bytes from cloister (192.168.254.246): icmp_seq=1 ttl=64
time=5.98 ms\n\n--- cloister ping statistics ---\n1 packets
transmitted, 1 received, 0% packet loss, time 0ms\nrtt
min/avg/max/mdev = 5.983/5.983/5.983/0.000 ms",
"stdout_lines": [
"PING cloister (192.168.254.246) 56(84) bytes of
data.",
"64 bytes from cloister (192.168.254.246): icmp_seq=1
ttl=64 time=5.98 ms",
"",
"--- cloister ping statistics ---",
"1 packets transmitted, 1 received, 0% packet loss,
time 0ms",
"rtt min/avg/max/mdev = 5.983/5.983/5.983/0.000 ms"
]
}
}
TASK [Format it a bit]
***********************************************************
changed: [cloister -> localhost]
PLAY RECAP
***********************************************************************
cloister : ok=3 changed=2 unreachable=0
failed=0 skipped=0 rescued=0 ignored=0
$ cat /tmp/ping-report.txt
--- PING REPORT ---
PING cloister (192.168.254.246) 56(84) bytes of data.
rtt min/avg/max/mdev = 5.983/5.983/5.983/0.000 ms
PING bluemoon (192.168.254.245) 56(84) bytes of data.
rtt min/avg/max/mdev = 5.455/5.455/5.455/0.000 ms
PING lappy (192.168.254.244) 56(84) bytes of data.
1 packets transmitted, 0 received, +1 errors, 100% packet loss,
time 0ms
PING dewdrop (192.168.254.243) 56(84) bytes of data.
rtt min/avg/max/mdev = 286.843/286.843/286.843/0.000 ms