Ansible Ping Module: Test Host Connectivity & Availability (Guide)
By Luca Berton · Published 2024-01-01 · Category: installation
How to test host availability with Ansible ping module. Verify SSH connectivity, Python interpreter, and troubleshoot unreachable hosts with examples.
Ansible module ping. Today we’re going to talk about the simplest way to test if a managed host is available to receive our commands. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Ansible module ping Today we’re talking about Ansible module ping. The full name is ansible.builtin.ping, which means that is part of the collection of modules “builtin” with ansible and shipped with it. It’s a module pretty stable and out for years. It verify the ability of Ansible to login to the managed host and that there is a Python interpreter that is able to execute our code. So it’s pretty different for the ping in the network context. It's the Linux corresponding to the Ansible win_ping module.
Main parameters and return values • data _string_
People usually don’t specify any parameters or use the return value. For the parameter, it’s possible to change the behavior from the default “pong” to the “crash” that raises an exception in case of failure. • ping _string_
The return value default is the “pong” string, but you could customize it with the data parameter.
Demo Are you ready to make your hands dirty? Let’s jump in a quick live Playbook of a playbook about the ping module.
Conclusion Now you know better the Ansible module ping and you could use it successfully in your playbook.
What ansible.builtin.ping Actually Does
Despite its name, the Ansible ping module does NOT perform an ICMP ping (like the ping command in your terminal). Instead, it: Connects to the managed host via SSH (or WinRM for Windows) Verifies that Python is installed and working Returns pong on success
This makes it the perfect first test after setting up Ansible to verify your entire connection chain works.
Usage Examples
Ad-hoc command (most common)
Expected output (success)
Expected output (failure)
In a playbook
Ping vs Win_ping
| Module | Target OS | Connection | Tests | |--------|-----------|------------|-------| | ansible.builtin.ping | Linux/macOS | SSH | SSH + Python | | ansible.windows.win_ping | Windows | WinRM | WinRM + PowerShell |
Troubleshooting Failed Pings
Problem: UNREACHABLE
The SSH connection itself failed. Check: • Can you ssh user@host manually? • Is the correct SSH key configured? • Is the correct user specified?
Problem: MODULE FAILURE with Python error
SSH works but Python isn't installed or found:
Or tell Ansible where Python is:
Problem: Ping works but playbooks fail
ping only tests basic connectivity. If playbooks fail: • Check become permissions for privileged tasks • Verify gather_facts works: ansible all -m setup -i inventory
FAQ
Should I use ping in production playbooks?
Generally no — it's a diagnostic tool. Your playbook tasks will fail naturally if a host is unreachable. Use wait_for_connection instead for orchestration:
Can I use ping without an inventory file?
Yes, for quick tests:
What's the data parameter for?
You can change the return value from pong to anything:
Use data=crash to test error handling:
Basic Ping
Expected Output
Ping with Custom Data
In Playbooks
Wait for Host
Check Multiple Groups
Windows Ping
Troubleshooting Failed Pings
ping vs wait_for vs wait_for_connection
| Module | What it Tests | Use Case | |--------|--------------|----------| | ping | SSH + Python | Basic connectivity | | wait_for | TCP port | Service availability | | wait_for_connection | SSH ready | Post-reboot | | uri | HTTP endpoint | Application health |
Health Check Pattern
FAQ
Does ping use ICMP?
No — Ansible ping connects via SSH (or WinRM) and runs a small Python script. It's NOT an ICMP ping. For ICMP, use command: ping -c 1 host.
Why does ping fail but SSH works?
Usually Python is missing or the wrong version. Set ansible_python_interpreter in your inventory.
Can I ping without an inventory?
Related Articles • Ansible Become Guide • Ansible Inventory Guide • Ansible for Windows Guide
Category: installation
Watch the video: Ansible Ping Module: Test Host Connectivity & Availability (Guide) — Video Tutorial