Advanced Ping Module Options and Targeting Multiple Groups
Pinging Multiple Host Groups
Let us expand our playbook to ping multiple groups from our inventory. Create a new file named ping_all.yml
:
- In the WebIDE, navigate to
/home/labex/project/ansible
- Right-click on the
ansible
folder and select "New File"
- Name the file
ping_all.yml
- Add the following content to the file:
---
- hosts: all
gather_facts: no
tasks:
- name: Ping all hosts in the inventory
ping:
This playbook will target all hosts in the inventory file. Run it with:
ansible-playbook -i hosts ping_all.yml
You should see output showing successful ping responses from all hosts in your inventory.
Using Custom Data with the Ping Module
The Ansible ping module allows you to send custom data to verify that it comes back unmodified. This can be useful for testing more complex connectivity scenarios.
Create a new playbook named ping_data.yml
:
- In the WebIDE, create a new file named
ping_data.yml
in the /home/labex/project/ansible
directory
- Add the following content:
---
- hosts: webservers
gather_facts: no
tasks:
- name: Ping with custom data
ping:
data: "Hello from Ansible Lab"
Run this playbook:
ansible-playbook -i hosts ping_data.yml -v
The verbose output will show your custom data in the response:
TASK [Ping with custom data] **************************************************
ok: [localhost] => {"changed": false, "ping": "Hello from Ansible Lab"}
Running Ping Against Specific Hosts
You can also run the ping module directly from the command line without creating a playbook. This is useful for quick connectivity checks:
ansible local -i hosts -m ping
This command will ping all hosts in the local
group and display the results. You should see:
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
You can also specify multiple groups or patterns:
ansible 'webservers:local' -i hosts -m ping
This command will ping all hosts in both the webservers
and local
groups.
Ping Module with Arguments
You can also pass arguments to the ping module directly from the command line:
ansible all -i hosts -m ping -a "data='Command line test'"
This command will ping all hosts with the custom data string "Command line test" and display the results.
The output will show each host's response with your custom message:
localhost | SUCCESS => {
"changed": false,
"ping": "Command line test"
}
These examples demonstrate the versatility of the Ansible ping module for verifying connectivity to your managed hosts in various ways.