Updated April 12, 2023
Introduction to Ansible Ping
In Ansible, to do a task on remote hosts, we need to have some basic requirements on target remote hosts so that Ansible environment can be setup and work as expected. These requirements are mostly related to network, user management, privileges and availability of Python on target remote machine. We can either check these requirements manually one by one or we can use modules like ping to test most of those requirements. When using ping, the return value will be either a success in the form of a string or error messages in case of failure. Ansible ping module a very useful yet simple test module to check the connectivity and workability on target remote machines. This tries to check few things on remote machines which are the basic needs for Ansible to work in non-erratic manner.
So ping module is used to check below requirements on remote target machines.
- Network Connectivity
- Current User Login
- Usable Python Availability
After checking these things on target remote machines. It returns a value based on success or failure. This module should be used as a first check on your Ansible environment to identity non-accessible machines. So that your playbooks will not fail. As Ansible playbook’s work behavior is such that it will fail on first failure occurred and will not continue even the afterwards tasks are feasible. For target Windows environment, Ansible provides win_ping module and for target network devices, Ansible provides net_ping module.
How does Ansible Ping works?
Ansible ping module work in very simple manner and return the success or failure in the form of a defined/default string or error, respectively. This module does not have any special parameters or options.
Given below are the few points which one should know before using it.
- After successful verifying the ability to login and availability of usable python on remote hosts. It by default return pong string, which basically means the connectivity and available Python on remote machines are good enough to work.
- This do not work on ICMP, but work by default on SSH or any other defined connection method.
- One acceptable parameter is data which accepts values as either pong, crash or some other string.
- Default is pong. This represents the return value on success.
- If data is provided with value crash, then this module will return an exception.
- Though we can use this module in playbooks, but this module is mostly used with the command /usr/bin/ansible, which is directly used on terminal.
- This module is guaranteed for backward compatibility by Ansible Core team.
Examples of Ansible Ping
Given below are the examples mentioned:
Here we have an Ansible control server named ansible-controller and two remotes hosts named host-one and host-two. We will create playbooks and run Ansible commands on ansible-controller node and see the results on remote hosts.
Example #1
In this example, we use ansible command like below, using which we try to check the status for Ansible to work on remote target nodes.
Code:
ansible all -m ping -v
Output:
Here we saw that when using Ansible ping module. It checked for network connection, login ability over SSH and python interpreter on remote target machines.
Example #2
In below example, we will try to run this module in a playbook. This is not mostly useful, but can be used when we have a condition to test the connection first, then start doing the rest of tasks.
Code:
hosts:
all
tasks:
name: here we are checking the ping-pong ping:
Now running this playbook like below:
ansible-playbook --syntax-check ansible_ping.yaml
Output:
Now registering the output in a variable named “var_ping” and then use debug module in this playbook like below to print the returned values, we can see that in the output, we have pong as return value.
Code:
hosts:
all
tasks:
name: here we are checking the ping-pong ping:
register: var_ping
debug:
var: var_ping
Run this like below:
ansible-playbook ansible_ping.yaml
Output:
Example #3
In this example, we will try to change the return value from default pong to crash. We can write a playbook like below. Here we are expecting that it will return an exception.
Code:
hosts:
all
tasks:
name: Here we are trying to return "crash" on successful ping:
data: crash
Running the playbook like below:
ansible-playbook ansible_ping_crash.yaml -v
We get output like below, where we can see that an exception was returned from both target remote machines.
Output:
Example #4
In this example we will try to return a value named “test” on successful test validation. We will register the return value in a variable named “var_ping”. Then using debug module, we will print the returned strings.
For this we create a playbook like below:
Code:
hosts: all
gather_facts:
no tasks:
ping:
data: test
register: var_ping
debug:
var: var_ping
Now running this playbook like below:
ansible-playbook ansible_ping_string.yaml
We get the output like below, where we can see that we get “test” in return strings.
Output:
Example #5
In this example, we will remove ssh key from the file .ssh/ authorized_keys of host-one, so that passwordless connection will not be possible from Ansible controller node to host-one. We will not do any changes to host-two. Now checking with ping module like below.
Code:
ansible all -m ping
We get the output as below, here we can see that though the network connection was working towards host-one and Python was available as well. But as password less SSH authentication was not working, the ping test failed on host-one. On other hand it worked on host-two.
Output:
Conclusion
As we saw in above examples, Ansible ping module is a useful and easy to use module which tells you about the connectivity status of target remote hosts. Though this trivial test module is not equipped with many options like other Ansible modules, but we need this kind of modules to check basic level workability.
Recommended Articles
This is a guide to Ansible Ping. Here we discuss the introduction to Ansible Ping, how does this works along with programming examples. You may also have a look at the following articles to learn more –