FreeKB - Ansible ping module
Ansible - ping module

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The ping module will ping managed nodes (e.g. target systems). In this example, every managed node will be pinged.

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

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter f7060 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |