Ping a host inside ansible playbook

19,333

Solution 1

You can use ping command:

---

- hosts: all
  gather_facts: False
  connection: local

  tasks:

    - name: ping
      shell: ping -c 1 -w 2 8.8.8.8
      ignore_errors: true

Solution 2

Try to use delegate_to module to specify that this task should be executed on localhost. Maybe ansible is trying to connect to those devices to execute ping shell command. The following code sample works for me.

tasks:
- name: ping test
  shell: ping -c 1 -w 2 {{ ansible_host }}
  delegate_to: localhost
  ignore_errors: true
Share:
19,333

Related videos on Youtube

Veerendra K
Author by

Veerendra K

Hi there 👋 🏃 I'm Veerendra 🇮🇳 Im from India 🧑‍💻 I'm a DevOps Engineer ☕ I love coffee 📝 I python, shell, ansible,docker, kubernetes, openshift, elasticsearch... 🔥 Im passionate about 🔐 Information Security 💾 Operating Systems(Linux) 🖥️ Computer networking

Updated on June 04, 2022

Comments

  • Veerendra K
    Veerendra K almost 2 years

    I just want to ping a host(DNS host) to check reachability. Looks there is no proper way to do this? I'm not sure. Below is my playbook with net_ping

    ---
    - name: Set User
      hosts: web_servers
      gather_facts: false
      become: false
      vars:
        ansible_network_os: linux
    
      tasks:
      - name: Pinging Host
        net_ping
          dest: 10.250.30.11
    

    But,

    TASK [Pinging Host] *******************************************************************************************************************
    task path: /home/veeru/PycharmProjects/Miscellaneous/tests/ping_test.yml:10
    ok: [10.250.30.11] => {
        "changed": false, 
        "msg": "Could not find implementation module net_ping for linux"
    }
    
    

    With ping module

    ---
    - name: Set User
      hosts: dns
      gather_facts: false
      become: false
    
      tasks:
      - name: Pinging Host
        action: ping
    

    Looks like it is trying to ssh into the IP.(Checked in verbose mode). I don't know why? How can I do ICMP ping? I don't want to put the DNS IP in inventory also.

    UPDATE1:

    hmm, Looks like there no support for linux in ansible_network_os.

    https://www.reddit.com/r/ansible/comments/9dn5ff/possible_values_for_ansible_network_os/

  • Veerendra K
    Veerendra K almost 5 years
    Yep, I thought so. But, I was hoping ping module to do this. :-(
  • itiic
    itiic almost 5 years
    You can do it using different approaches. Personally, I'm not using Ansible as a monitoring tool.
  • Veerendra K
    Veerendra K almost 5 years
    I understood. there another way with wait_for. But it requires port. But I want to use ICMP.