If you are not familiar with modules, check out Ansible - Getting Started with Modules.
The ping module will ping Linux nodes whereas the win_ping module is used to ping Windows hosts. In this example, the Ansible ad hoc command is used to ping every Linux node using ping.
~]# ansible all -i hosts.yml -m ping
server1.example.com | SUCCESS => {
"changed": false,
"ping": "pong"
}
Or like this, via a playbook.
---
- hosts: all
tasks:
- ping
...
Running this playbook will return something like this.
PLAY [all]
TASK [Gathering Facts]
ok: [server1.example.com]
ok: [server2.example.com]
ok: [server3.example.com]
ok: [server4.example.com]
ok: [server5.example.com]
TASK [ping]
ok: [server1.example.com]
ok: [server2.example.com]
ok: [server3.example.com]
ok: [server4.example.com]
ok: [server5.example.com]
PLAY RECAP
server1.example.com : ok=2 changed=0 unreachable=0 failed=0
server2.example.com : ok=2 changed=0 unreachable=0 failed=0
server3.example.com : ok=2 changed=0 unreachable=0 failed=0
server4.example.com : ok=2 changed=0 unreachable=0 failed=0
server5.example.com : ok=2 changed=0 unreachable=0 failed=0
It's important to recognize that the register module will not be able to register the ping output. For example, let's say you've this playbook.
---
- hosts: all
tasks:
- ping
register: out
- debug:
msg: "{{ out.stdout }}"
Running this playbook should return the following.
fatal: [server1.example.com]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'"}
If you want to register the stdout, stderr, or return code of the ping command, you will first need to set gather_facts: false to prevent Ansible from attempting an SSH connection to the managed node. Then, the delegate_to module is used to issue the ping command from the control node (that's your Ansible server). The shell module is used to issue the ping command, and the register modules is used to capture the stdout, stderr, and return code. The meta: clear_host_errors module is used to clear errors and move onto the next managed node when ping fails. Finally, the debug and when modules are used to print "100% packet loss" when a managed node has 100% packet loss.
---
- hosts: all
gather_facts: false
tasks:
- name: ping command
delegate_to: localhost
shell: "ping -c4 {{ inventory_hostname }} "
register: out
ignore_errors: yes
- meta: clear_host_errors
- name: standard out
debug:
msg: "{{ out.stdout_lines }}"
- name: return code
debug:
msg: "{{ out.rc }}"
- name: failed ping
debug:
msg: "{{ inventory_hostname }} has 100% packet loss"
when: "out.stdout_lines is search ('100% packet loss')"
Running this playbook will return something like this when the ping is successful.
PLAY [all]
TASK [Gathering Facts]
ok: [server1.example.com]
TASK [ping]
ok: [server1.example.com -> localhost]
TASK [standard out]
ok: [server1.example.com] => {
"msg": [
"PING server1.example.com (10.1.2.3) 56(84) bytes of data.",
"64 bytes from server1.example.com (10.1.2.3): icmp_seq=1 ttl=64 time=0.345 ms",
"64 bytes from server1.example.com (10.1.2.3): icmp_seq=1 ttl=64 time=0.349 ms",
"64 bytes from server1.example.com (10.1.2.3): icmp_seq=1 ttl=64 time=0.337 ms",
"64 bytes from server1.example.com (10.1.2.3): icmp_seq=1 ttl=64 time=0.355 ms",
"",
"--- server1.example.com ping statistics ---"
"4 packet transmitted, 4 received, 0% packet loss, time 0ms",
"rtt min/ave/max/mdev = 0.345/0.349/0.337/0.052 ms"
]
}
TASK [return code]
ok: [server1.example.com] => {
"msg": "0"
}
PLAY RECAP
server1.example.com : ok=4 changed=0 unreachable=0 failed=0
Did you find this article helpful?
If so, consider buying me a coffee over at