Introduction
ansible.builtin.ping is the first command you run with Ansible — it verifies that the control node can connect to remote hosts, that SSH (or WinRM) works, and that Python is available. It does not send ICMP pings. It's a full Ansible module test.
For an ad-hoc connectivity check, run ansible all -m ping; a successful host returns "ping": "pong", which means Ansible could connect and execute the module.
Basic Usage
# Ping all hosts
ansible all -m ping
# Ping specific group
ansible webservers -m ping
# Ping single host
ansible web-01 -m ping
# With inventory file
ansible -i inventory.ini all -m ping
# Expected output
# web-01 | SUCCESS => {
# "changed": false,
# "ping": "pong"
# }
What ping Actually Tests
┌────────────────────────────────────┐
│ ansible.builtin.ping tests: │
│ │
│ 1. ✅ SSH connection works │
│ 2. ✅ Authentication succeeds │
│ 3. ✅ Python is available │
│ 4. ✅ Ansible can execute modules │
│ 5. ✅ Returns "pong" │
└────────────────────────────────────┘
Not tested: ICMP ping, network connectivity, port availability. For ICMP, use ansible.builtin.command: ping -c 1 hostname.
Parameters
| Parameter | Default | Description |
|---|---|---|
data | pong | Data to return (instead of "pong") |
# Custom return data
- name: Ping with custom response
ansible.builtin.ping:
data: "alive"
# Returns: { "ping": "alive" }
# Crash test
- name: Test exception handling
ansible.builtin.ping:
data: crash
# Raises an exception (useful for testing error handling)
Playbook workflow
Generate a complete playbook from this tutorial
Use Ansible ping Module - Test SSH Connectivity as the starting context and generate an editable Ansible playbook in the playground.
One anonymous generation is available. Do not paste passwords, private keys, tokens, or customer secrets.
In a Playbook
---
- name: Verify all hosts reachable
hosts: all
gather_facts: false # Don't waste time on facts
tasks:
- name: Test connectivity
ansible.builtin.ping:
register: ping_result
- name: Show unreachable hosts
debug:
msg: "{{ inventory_hostname }} is reachable"
Windows — win_ping
# For Windows hosts
- name: Test Windows connectivity
ansible.windows.win_ping:
register: win_result
- debug:
msg: "Windows host responded: {{ win_result.ping }}"
# Ad-hoc
ansible windows_hosts -m win_ping
Troubleshooting
"UNREACHABLE!" — SSH Connection Failed
# Check SSH manually
ssh user@hostname
# Verbose output
ansible web-01 -m ping -vvvv
# Common fixes
ansible web-01 -m ping -e "ansible_user=ubuntu"
ansible web-01 -m ping -e "ansible_ssh_private_key_file=~/.ssh/mykey"
ansible web-01 -m ping --ask-pass
"MODULE FAILURE" — Python Not Found
# Specify Python path
ansible web-01 -m ping -e "ansible_python_interpreter=/usr/bin/python3"
# Or in inventory
[webservers:vars]
ansible_python_interpreter=/usr/bin/python3
"Permission denied"
# Use SSH key
ansible web-01 -m ping --private-key ~/.ssh/id_ed25519
# Use password
ansible web-01 -m ping --ask-pass
# Use become (sudo)
ansible web-01 -m ping --become --ask-become-pass
Host Key Verification Failed
# ansible.cfg
[defaults]
host_key_checking = False
Or:
export ANSIBLE_HOST_KEY_CHECKING=False
ansible all -m ping
Common Patterns
Connectivity Check Before Deploy
---
- name: Pre-deploy verification
hosts: all
gather_facts: false
tasks:
- name: Verify connectivity
ansible.builtin.ping:
- name: Deploy application
hosts: webservers
tasks:
- name: Deploy app
# ... deploy tasks
Check Specific Hosts
# Ping with limit
ansible all -m ping --limit "web-01,web-02"
# Ping with pattern
ansible 'webservers:&production' -m ping
# Ping excluding hosts
ansible 'all:!maintenance' -m ping
Health Dashboard
- name: Check all infrastructure
hosts: all
gather_facts: false
ignore_unreachable: true
tasks:
- ping:
register: result
ignore_errors: true
- name: Report status
debug:
msg: "{{ inventory_hostname }}: {{ 'UP' if result.ping is defined else 'DOWN' }}"
ping vs Other Connectivity Tests
| Tool | Tests | Protocol |
|---|---|---|
ansible.builtin.ping | SSH + Python + module exec | SSH |
ICMP ping | Network reachability | ICMP |
ansible.builtin.wait_for | Port availability | TCP |
ansible.builtin.uri | HTTP endpoint | HTTP |
# TCP port check
- wait_for:
host: "{{ inventory_hostname }}"
port: 22
timeout: 10
delegate_to: localhost
# HTTP check
- uri:
url: "http://{{ inventory_hostname }}:8080/health"
status_code: 200
Related Articles
Conclusion
ansible.builtin.ping is your first diagnostic tool — it confirms SSH connectivity, authentication, and Python availability in one call. Run it before every deployment. If it fails, check SSH access, Python path, and credentials. For Windows hosts, use win_ping. For network-level checks, use wait_for (TCP) or the system ping command (ICMP). Always use -vvvv for detailed troubleshooting output.