Introduction
ansible.builtin.ping is the simplest Ansible module — it connects to a remote host, verifies Python is working, and returns pong. It's the first thing to run when setting up Ansible or troubleshooting connectivity. Despite the name, it's not an ICMP ping — it tests the full Ansible connection stack (SSH + Python).
Basic Usage
# Ping a single host
ansible myhost -m ping
# Ping all hosts in inventory
ansible all -m ping
# Ping a group
ansible webservers -m ping
# With verbose output for troubleshooting
ansible all -m ping -vvv
Successful Output
web-01 | SUCCESS => {
"changed": false,
"ping": "pong"
}
Failed Output
web-01 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 10.0.0.5 port 22: Connection timed out",
"unreachable": true
}
What ping Actually Tests
[Control Node] → SSH connection → [Remote Host] → Python execution → return "pong"
A successful ping confirms:
- ✅ Network connectivity to the host
- ✅ SSH (or WinRM) authentication works
- ✅ Python is installed and functional on the remote
- ✅ The Ansible user has permission to execute commands
- ✅ The temporary directory is writable
In a Playbook
---
- name: Verify all hosts are reachable
hosts: all
gather_facts: false # Don't waste time on facts
tasks:
- name: Ping all hosts
ansible.builtin.ping:
- name: Custom ping response
ansible.builtin.ping:
data: "alive" # Returns "alive" instead of "pong"
register: result
- name: Show result
ansible.builtin.debug:
msg: "{{ inventory_hostname }} responded: {{ result.ping }}"
Ping Before Deployment
---
- name: Pre-flight connectivity check
hosts: all
gather_facts: false
tasks:
- name: Verify host is reachable
ansible.builtin.ping:
register: ping_result
- name: List unreachable hosts
ansible.builtin.debug:
msg: "{{ inventory_hostname }} is UP"
- name: Deploy application (only runs if ping succeeded)
hosts: all
tasks:
- name: Deploy code
ansible.builtin.copy:
src: app/
dest: /opt/app/
Windows Hosts: win_ping
For Windows hosts using WinRM or PSRP, use ansible.windows.win_ping:
# Command line
ansible windows_hosts -m ansible.windows.win_ping
# Playbook
- name: Test Windows connectivity
hosts: windows
tasks:
- name: Ping Windows host
ansible.windows.win_ping:
register: win_result
- name: Show result
ansible.builtin.debug:
msg: "{{ win_result.ping }}"
win_ping vs ping
| Module | Connection | Tests |
|---|---|---|
ansible.builtin.ping | SSH | SSH + Python |
ansible.windows.win_ping | WinRM/PSRP | WinRM + PowerShell |
# WRONG — ping uses SSH, won't work on Windows WinRM hosts
- ansible.builtin.ping: # Fails on Windows
# RIGHT — use win_ping for Windows
- ansible.windows.win_ping: # Works on Windows
Troubleshooting Connection Failures
"UNREACHABLE" — SSH Can't Connect
# Test SSH manually
ssh user@10.0.0.5
# Check if host is up (ICMP ping)
ping -c 3 10.0.0.5
# Ansible with verbose for details
ansible myhost -m ping -vvv
Common causes:
- Host is down or firewall blocks port 22
- Wrong IP address or hostname in inventory
- SSH key not accepted
"Permission denied"
# Specify user and key
ansible myhost -m ping -u deploy --private-key ~/.ssh/deploy_key
# Or in inventory
# [webservers]
# web-01 ansible_user=deploy ansible_ssh_private_key_file=~/.ssh/deploy_key
"MODULE FAILURE" — Python Issue
web-01 | FAILED! => {
"msg": "/usr/bin/python: not found"
}
Fix: Install Python or point Ansible to the right interpreter:
# inventory
[webservers]
web-01 ansible_python_interpreter=/usr/bin/python3
Or for all hosts in ansible.cfg:
[defaults]
interpreter_python = auto_silent
"Host key verification failed"
# Option 1: Accept the key
ssh-keyscan -H 10.0.0.5 >> ~/.ssh/known_hosts
# Option 2: Disable host key checking (less secure)
# ansible.cfg
[defaults]
host_key_checking = False
Timeout Issues
# ansible.cfg — increase timeout
[defaults]
timeout = 30 # Default is 10 seconds
ping vs ICMP Ping
| Ansible ping | ICMP ping | |
|---|---|---|
| What | SSH + Python test | Network reachability |
| Command | ansible host -m ping | ping host |
| Requires | SSH, Python | Network access |
| Tests | Full Ansible stack | Only network |
If you need an actual ICMP ping from Ansible:
# Not ansible.builtin.ping — this is a network ICMP ping
- name: ICMP ping test
ansible.builtin.command: ping -c 3 {{ target_host }}
register: icmp_result
ignore_errors: true
- name: Check result
ansible.builtin.debug:
msg: "{{ target_host }} is {{ 'reachable' if icmp_result.rc == 0 else 'unreachable' }}"
Batch Connectivity Check
# Quick check — see who's up
ansible all -m ping --one-line
# Output:
# web-01 | SUCCESS => {"changed":false,"ping":"pong"}
# web-02 | SUCCESS => {"changed":false,"ping":"pong"}
# db-01 | UNREACHABLE! => {"changed":false,"msg":"...","unreachable":true}
# With forks for faster parallel checking
ansible all -m ping -f 50
Related Articles
- How to Install Ansible
- Ansible Inventory Basics
- Ansible win_ping Module
- Ansible SSH Connection
- Ansible raw Module (No Python)
Conclusion
ansible.builtin.ping is your first-line diagnostic tool. It tests the complete Ansible connection: network → SSH → Python. Run ansible all -m ping to verify your inventory before any deployment. Use ansible.windows.win_ping for Windows hosts. A failed ping means your playbook won't work either — fix the connection issue first by checking SSH access, Python interpreter, and host key verification.