UNREACHABLE! => { ... msg: 'Failed to connect via ssh'
UNREACHABLE! => { ... msg: 'Failed to connect via ssh'
DodaTech
2 min read
The Ansible “UNREACHABLE! => { … msg: ‘Failed to connect via ssh’” error means the control node cannot establish an SSH connection to the target host. Ansible relies entirely on SSH for Linux/Unix target communication.
What It Means
Ansible attempted to open an SSH session to the host defined in your inventory but the TCP connection, SSH handshake, or authentication failed. The host is marked unreachable and the task is skipped for that host.
Why It Happens
- The target host is powered off, unreachable on the network, or a firewall blocks port 22.
- SSH is not running on the target host.
- The SSH key is not deployed to the target host’s
authorized_keys. - The host key verification fails due to a changed host key.
- The user specified in
ansible_userdoes not exist on the target. - The SSH service is configured to reject the connecting user or IP.
How to Fix It
1. Test SSH connectivity manually
ssh -i ~/.ssh/id_rsa user@192.168.1.10 -p 222. Verify the host is reachable
ping -c 3 192.168.1.103. Check SSH service on the target
sudo systemctl status sshd4. Disable host key checking (for testing only)
In ansible.cfg:
[defaults]
host_key_checking = False5. Use ssh-keyscan to add the host key
ssh-keyscan -H 192.168.1.10 >> ~/.ssh/known_hosts6. Verify inventory connection variables
ansible-inventory -i hosts.ini --host target_hostEnsure ansible_host, ansible_user, ansible_port, and ansible_ssh_private_key_file are correct.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro