*$ 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
On 11/16/22 8:39 AM, saravanan jothilingam wrote:
Thanks for this input.
Could you please share one sample playbook code which does that?
Appreciate your help!
On Wed, Nov 16, 2022 at 5:43 PM 'Rowe, Walter P. (Fed)' via Ansible
Project <[email protected]> wrote:
The command module runs the ping. It won't display the output you
register in ping_result. You need a debug task if you want to see
the result. If you want formatted output you may want to look at
using a jinja2 template.
Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123
On Nov 15, 2022, at 11:11 PM, saravanan jothilingam
<[email protected]> wrote:
We have around 300+ on-prem linux servers to manage its
availability. We found ansible is one easy tool to manage it.
On Wed, Nov 16, 2022 at 9:20 AM Dick Visser <[email protected]>
wrote:
Hi
What is the reason you need to use ansible for this?
On Wed, 16 Nov 2022 at 04:09, saravanan jothilingam
<[email protected]> wrote:
Hi,
I need to get the ping status of all remote hosts and
print its results in one neat tabular column format.
Since I am a novice to ansible, I am looking for the
appropriate playbook which does this.
*note *- the below task doesnt give any clear output .
- name: check reachable hosts
hosts: remote-hosts
gather_facts: no
tasks:
- command: ping -c1 {{ inventory_hostname }}
delegate_to: localhost
register: ping_result
Thanks
saravanan
--
Todd
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/3fffc2b8-44f1-9e08-1520-cf82ece8e025%40gmail.com.