How to Use the Ansible Ping Module

To perform an automated task on a remote host with Ansible, it is necessary to fill and work with certain variables, as to be expected. For example, this type of task is necessary to put a remote host online, allow login and user privileges, and set up a Python environment.

It is possible to SSH into a remote host and check whether the host meets all the criteria. However, as an automation tool, Ansible provides modules that allow you to perform these tedious, time-consuming tasks in a single line of command.

This tutorial shows you how to use the Ansible built-in ping module to perform various set variables on remote hosts. The Ansible ping request is a simple yet useful tool that you can use to test the availability and practicality of remote hosts.

We will now take a closer look at how the Ansible ping works.

NOTE: This section is somewhat theoretical. If all you need is the technical part of the article, then you can skip this section. Even so, we recommend reading this section, as it provides some useful insights and information on the ping module.

With that noted, we will now move on to introduce the ping module.

About the Ansible ping Module

The Ansible ping request checks up on the remote host. This module specifically checks for:

  • Whether the remote host is up and accessible.
  • Whether the Python environment can successfully run the required playbooks.
  • The user login and privileges.

After a ping request is sent to the remote host, the module returns a value indicating whether the ping was successful. By default, the ping module returns the string ‘pong’ on success, and an exception on failure (if specified).

PRO TIP: To ensure that your Ansible playbooks on the target remote hosts do not fail, use the ping module to check whether the requirements are met, and only carry out the tasks if a successful output is obtained.

Features of the Ansible ping Module

The ping module is quite simple, and the following is a list of the features provided by this module:

  • Not an ICMP ping; rather, it is a small module that requires a valid Python environment on remote hosts.
  • Windows remote hosts should use the win_ping module instead.
  • Provides a net_ping module for network devices.
  • Accepts only a single parameter to raise an exception.
  • Used by default when calling the ansible command in the /usr/bin/ansible directory to verify login permissions and a valid Python environment.
  • Returns the string ‘pong’ on success.

How to Use the Ansible ping Module

We will now look at a few examples that use the Ansible ping module.

Using the AD HOC ping Command

The simplest way to run the Ansible ping module is to run a simple AD HOC command in the terminal.

The AD HOC command is as follows:

ansible all -m ping -v

The above command starts by calling Ansible, followed by the specific pattern of the host. In this case, we want to ping ‘all’ hosts. The next part, ‘-m,’ specifies the module that we want to use. The ‘-v’ stands for verbose.

On success, you will get an output like the one shown below:

Using /etc/ansible/ansible.cfg as config file

| SUCCESS => {
   "ansible_facts": {
       "discovered_interpreter_python": "/usr/bin/python"     },
   "changed": false,
   "ping": "pong"

If the remote hosts are unavailable (perhaps they are offline), you will obtain an output like the one shown below:

 Using /etc/ansible/ansible.cfg as config file

   "changed": false,
   "msg": "Failed to connect to the host via ssh: ssh: connect to host port 22: Connection timed out",
   "unreachable": true

Using the ping Module in Playbooks

You can also use the ping module inside a playbook. In most cases, you will not build a playbook that only performs a ping request. However, you can wrap all the tasks inside the conditions of the ping request.

Consider the following simple playbook:

- hosts: all  

  become: yes  

   - ping

You may notice that in the above playbook, I included the ‘become’ directive. This is not a requirement, but I have encountered cases in which the ping module fails for a standard user. You can now run the playbook and check whether the host server meets the requirements for a successful ‘pong’ response.

ansible-playbook ping.yml

If the ping is unsuccessful, you will get the following ‘unreachable’ error:

fatal: []: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host port 22: Connection timed out", "unreachable": true}

Using the raise Exception with the ping Module

You can also specify data as a crash to induce an exception. This changes the default return from ‘pong’ to ‘crash.’

Consider the following playbook:

- hosts: all  

  become: yes  

   - ping:
       crash: data

Running the playbook with exception should return the following value:

ansible-playbook ping_except.yml


This tutorial showed you the diverse utility of the ping module. Though this module does not contain complex parameters like most Ansible modules, it still contains powerful capabilities. For example, you can use the ping module to check whether an error is due to a missing Python environment and create a task to install the required environment on the remote host.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list