The ping module is part of the Ansible core and verifies connectivity between the control node and managed nodes. It helps confirm that the target systems are reachable, properly configured, and ready for Ansible commands. The ping module leverages ICMP packets to validate network connectivity, and when successful, it returns a string response confirming communication.
This guide will provide a comprehensive overview of the ping module in Ansible with practical examples.
Table of Contents
Prerequisites
Before using the ping module, ensure the following:
- Ansible is installed on the control node.
- A properly configured inventory file listing your managed hosts.
- SSH access to the managed nodes.
In this article, we will use the following inventory file:
[web_servers]
192.168.1.10
192.168.1.11
[db_servers]
192.168.1.20
How to Use the Ping Module
There are two ways you can use the ping module in Ansible.
1. Using the Ad-Hoc Command
The quickest way to use the ping module is through an ad-hoc command. This is a great way to quickly verify connectivity and the status of your hosts.
Here is a simple syntax to run the ping module with the -m parameter on all hosts specified in the inventory file and verify the connectivity.
# ansible all -m ping
If all hosts are reachable from the control node, you will see the status as shown below:
192.168.1.10 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.1.11 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.1.20 | SUCCESS => {
"changed": false,
"ping": "pong"
}
Notice the string “pong” indicating a successful response.
2. Using the Ping Module in a Playbook
You can include the ping module in an Ansible playbook for automated testing.
Here is the playbook:
- name: Check connectivity with all hosts
hosts: all
tasks:
- name: Ping all hosts
ansible.builtin.ping:
Run the playbook:
# ansible-playbook ping_check.yml
You will get the following output:
TASK [Ping all hosts] **********************************************************
ok: [192.168.1.10]
ok: [192.168.1.11]
ok: [192.168.1.20]
Now, let’s dive deep to explore some practical scenarios:
Example 1: Check Connectivity to All Hosts
Run a ping command to check all hosts in your inventory:
# ansible all -m ping
Example 2: Ping Specific Host Groups
You can target a specific group of hosts defined in the inventory.
The following command runs the ping module on all remote hosts in the web_servers group.
# ansible web_servers -m ping
Example 3: Handle Unreachable Hosts
If a host is unreachable, the ping module reports an error.
Let’s ping the db_servers:
# ansible db_servers -m ping
If the target host is unreachable, you will get the following output.
192.168.1.20 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Connection timed out",
"unreachable": true
}
Example 4: Verify Connectivity Using Custom SSH Key
You can also specify a custom SSH key for authentication, useful if your default interpreter or SSH configuration differs.
# ansible all -m ping --private-key /path/to/private_key
Example 5: Test Specific Hosts in the Inventory
You can test individual hosts by specifying them via IP address.
The below command runs the ping module on a specific host(192.168.1.10):
# ansible 192.168.1.10 -m ping
Example 6: Run Ping with Verbose Mode
Verbose mode provides additional details about the ping operation.
# ansible all -m ping -v
Output.
Using /etc/ansible/ansible.cfg as config file
192.168.1.10 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.1.11 | SUCCESS => {
"changed": false,
"ping": "pong"
}
Example 7: Ping Hosts from a Custom Inventory File
Specify a custom inventory file to target specific groups or hosts. The -i
option allows you to define a custom file that might contain hosts from various parts of your network:
# ansible all -i custom_inventory.ini -m ping
Conclusion
The ping module is an essential starting point for any Ansible project. It ensures connectivity and verifies node readiness. Mastering the ping module allows you to move forward with more complex automation tasks confidently.
FAQs
1. What is the default timeout for the Ping module?
The default timeout is 10 seconds, but you can adjust it using the ansible_ssh_timeout variable.
2. How do I check multiple groups with the Ping module?
Specify multiple groups in your command, separated by a colon, e.g., ansible web_servers:db_servers -m ping.
3. Can I run the Ping module on multiple hosts in parallel?
Yes, Ansible runs tasks in parallel by default. You can control the concurrency using the -f (forks) option.