Skip to content
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_user does 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 22

2. Verify the host is reachable

ping -c 3 192.168.1.10

3. Check SSH service on the target

sudo systemctl status sshd

4. Disable host key checking (for testing only)

In ansible.cfg:

[defaults]
host_key_checking = False

5. Use ssh-keyscan to add the host key

ssh-keyscan -H 192.168.1.10 >> ~/.ssh/known_hosts

6. Verify inventory connection variables

ansible-inventory -i hosts.ini --host target_host

Ensure ansible_host, ansible_user, ansible_port, and ansible_ssh_private_key_file are correct.

FAQ

How do I enable verbose SSH output in Ansible?
Add -vvvv to your ansible command: ansible all -m ping -vvvv. This shows the full SSH command and its output, helping identify the exact failure point.
Can Ansible use password authentication instead of SSH keys?
Yes. Use --ask-pass or set ansible_password in your inventory. However, SSH keys are more secure and recommended for automation.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro