DevOps

Use Ansible Ping to Try to Connect to Host

This tutorial will discuss how to use Ansible built-in ping module to perform many set variables on remote hosts.
Captain Salem 3 min read
Use Ansible Ping to Try to Connect to Host

To perform an automated task on a remote host with Ansible successfully, we require some variables to be fulfilled and work as expected. For example, we need to have the remote host online, allow login and user privileges, and have a python environment set up.

Although we can SSH into the remote hosts and check if it meets all the criteria, being an automation tool, Ansible provides us with modules to perform such tasks in a single line of command.

Ansible ping request is simple but very useful to test the availability and practicality of the remote hosts.

Let us take a simpler look at how the Ansible ping works.

NOTE: This is a little theoretical, and if all you need is the technical part, you can skip it. However, it can provide some insights and information on the ping module.

With that noted, let’s move one:

How the Ansible Ping Works Explain Ping

The Ansible ping request checks up on the remote host. It specifically checks:

  • If the remote host is up and accessible.
  • Python environment to successfully run playbooks that may require it.
  • User login and privileges.

Once a ping request gets sent to the remote host, the module returns a value indicating if the ping was successful or not. By default, it returns a string “pong” on success and an exception on failure (if specified).

NOTE: To ensure your Ansible playbooks on the target remote hosts do not fail, use the ping module to check if the requirements are met and only carry out the tasks if success is true.

Features of Ansible Ping

Although the ping module is simple, here is a synopsis of some of the features it provides:

  • The ping module is not an ICMP ping; it’s a small module that requires a valid python environment on the remote hosts.
  • Windows remote hosts should use the win_ping module instead
  • Ansible ping provides a net_ping module for network devices.
  • It accepts only a single parameter to raise an exception.
  • The module is used by default when calling the command ansible in /usr/bin/ansible to verify login permissions and a valid python env.
  • Return a string “pong” on success.

How to Use the Ansible Ping Module

Let us now see a few examples of how we can utilize the ping module in Ansible.

AD HOC Ping Command

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

The command is:

ansible all -m ping -v

In the above command, we start by calling Ansible, followed by the specific host’s pattern. In this case, we want to ping all hosts. The next part (-m) specifies the module we want to use and the -v for verbose.

On success, you should get an output similar to the one shown below:

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

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

If the remote hosts are unavailable, perhaps they’re offline, you will get an output like the one shown below:

 Using /etc/ansible/ansible.cfg as config file 35.222.210.12
| UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 35.222.210.12 port 22: Connection timed out",
"unreachable": true
}

Using Ansible Ping in Playbooks

We can also dive into the ping module and use it inside a playbook. In most scenarios, 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
  
  tasks:
  - ping

You may notice that in the above playbook, I included the become directive. That’s not a requirement, but I have encountered cases where ping fails for a standard user. You can now run the playbook and see if the host meets the requirements for a successful pong response.

ansible-playbook ping.yml

If unsuccessful, you will get an unreachable error:

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

Ansible ping raise exception

We can also specify data as a crash to induce an exception. This changes the default return from pong to crash. Consider the example playbook:

- hosts: all
  become: yes

tasks:
  - ping:
    crash: data

Running the playbook with exception should return the value as:

ansible-playbook ping_except.yml

Conclusion

From this quick tutorial, we can see the usefulness of the ping module. Although it does not contain complex parameters like most Ansible modules, it has powerful capabilities. For example, you can use ping to check if an error is because of a missing python environment and create a task to install it on the remote host.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.