veerendra2
veerendra2

Reputation: 2273

Ping a host inside ansible playbook

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/

Upvotes: 8

Views: 51394

Answers (4)

Nasruddin Ansari
Nasruddin Ansari

Reputation: 1

Please find the below ping yaml.

- name: connectivity check
  hosts: all
  tasks:
   - name: ping check
     ping:

It will work.

Upvotes: 0

linux.cnf
linux.cnf

Reputation: 807

can also run/check ping latency by below adhoc command in ansible

ansible all -m shell -a "ping -c3 google.com"

Upvotes: 0

Marios Karatisoglou
Marios Karatisoglou

Reputation: 223

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

Upvotes: 6

itiic
itiic

Reputation: 3712

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

Upvotes: 9

Related Questions