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

  1. Test SSH connectivity manually:

    ssh -v user@hostname
  2. Verify the host IP and SSH port in the inventory.

  3. Verify the SSH key is authorized on the target:

    ssh-copy-id user@hostname
  4. Check 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 ping

A successful result shows "pong".