Ping a host inside ansible playbook
Asked Answered
D

4

7

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/

Donelson answered 9/5, 2019 at 7:46 Comment(1)
Network Automation is Different. There are special Network OS for routers, switches etc.Cholecystotomy
K
8

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
Komarek answered 9/5, 2019 at 8:8 Comment(3)
Yep, I thought so. But, I was hoping ping module to do this. :-(Donelson
You can do it using different approaches. Personally, I'm not using Ansible as a monitoring tool.Komarek
I understood. there another way with wait_for. But it requires port. But I want to use ICMP.Donelson
E
5

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
Elmaelmajian answered 7/8, 2020 at 8:42 Comment(1)
Looks like, based on your own post, local_action would be more concise: ... tasks: - name: local_action: ping -c 1 -w 2 {{ ansible_host }}Respectively
S
0

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

ansible all -m shell -a "ping -c3 google.com"
Suellensuelo answered 24/10, 2021 at 8:46 Comment(0)
B
0

Please find the below ping yaml.

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

It will work.

Belle answered 9/11, 2023 at 10:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.