Ansible: UNREACHABLE host
Fix the Ansible UNREACHABLE error when SSH connections to managed hosts fail.
Ansible: UNREACHABLE host
Ansible reports "UNREACHABLE!" when it cannot establish an SSH connection to the managed host.
When Ansible Produces This Error
Ansible displays
fatal: [hostname]: UNREACHABLE! => {"msg": "Failed to connect to the host via ssh"} when the SSH connection fails.
What Causes UNREACHABLE in Ansible
The target host is offline, the SSH service is not running, the SSH port is blocked by a firewall, the IP address is incorrect, the SSH key is not authorized on the target, or the specified user does not exist on the target system.
How to Fix UNREACHABLE in Ansible
Test SSH connectivity manually:
ssh -v user@hostnameVerify the host IP and SSH port in the inventory.
Verify the SSH key is authorized on the target:
ssh-copy-id user@hostnameCheck the firewall allows SSH on the target:
sudo ufw status
How to Verify the Fix
Run the Ansible ping module:
ansible -i inventory.ini hostname -m pingA successful result shows
"pong".