How to Troubleshoot Ansible UNREACHABLE SSH Connection Error
Abotts Logo Abotts Partners with singapore based tech giant to help migrate their public sector customer from Sybase to SQL server.
Upworks Logo Abotts partners with NYPL to integrate with their partner libraries.
Abotts Logo ABOTTS partners with County in Los Angeles to upgrade their court infrastructure into new technologies.
Upworks Logo Upworks Inc partners with ABOTTS to build their Oracle Cloud Infrastructure (OCI) and migrate their custom applications to OCI.
Abotts partners with startup to manage and maintain their IT infrastructure and support SOC2 reporting.
Gnorth Logo Abotts Inc Partners with Gnorth consulting to deploy exadata and ODA for a large public sector customer.
Abotts Logo Abotts Partners with singapore based tech giant to help migrate their public sector customer from Sybase to SQL server.
Upworks Logo Abotts partners with NYPL to integrate with their partner libraries.
Abotts Logo ABOTTS partners with County in Los Angeles to upgrade their court infrastructure into new technologies.
Upworks Logo Upworks Inc partners with ABOTTS to build their Oracle Cloud Infrastructure (OCI) and migrate their custom applications to OCI.
Abotts partners with startup to manage and maintain their IT infrastructure and support SOC2 reporting.
Gnorth Logo Abotts Inc Partners with Gnorth consulting to deploy exadata and ODA for a large public sector customer.

Troubleshooting Ansible “UNREACHABLE! Host Unreachable” SSH Error

Error:

server1 | UNREACHABLE! => {

    “changed”: false,

    “msg”: “Failed to connect to the host via ssh: ssh: Could not resolve hostname server1: Temporary failure in name resolution”,

    “unreachable”: true

}

Understanding the Cause

Ansible relies on hostnames being resolvable either through:

  1. DNS
  2. /etc/hosts
  3. Explicit IP addresses in the inventory file

In this case, the inventory contains:

server1 ansible_host=35.88.167.32 ansible_user=ec2-user

However, the command:

ansible all -i ‘server1,’ -m ping -u ec2-user

overrides the inventory file and treats server1 as a literal hostname. Since server1 is not defined in DNS or /etc/hosts, SSH fails to resolve it.

 

 Steps to Resolve the Error

1. SSH Key and Authentication Checks

SSH connection failures can occur even after hostname resolution. It is important to verify SSH key configuration and the server’s SSH daemon settings.

1. Verify sshd_config

The SSH daemon configuration should allow key-based authentication:

grep PasswordAuthentication /etc/ssh/sshd_config

Example output:

PasswordAuthentication yes

This confirms that password authentication is enabled. For key-based authentication, also ensure:

PubkeyAuthentication yes

2. The SSH daemon must be active:

systemctl status sshd

2. Hostname Resolution

1. Use the IP Address Directly

Providing the IP address explicitly allows SSH to connect without relying on hostname resolution:

ansible all -i ‘35.88.167.32,’ -m ping -u ec2-user

The trailing comma indicates that this is a list of hosts.

2. Use the Inventory File Properly

Defining hosts in an inventory file allows easier management:

[myservers]

server1 ansible_host=35.88.167.32 ansible_user=ec2-user

Then run the command:

ansible myservers -i /etc/ansible/hosts -m ping

This method ensures that hostnames are mapped to IPs using the ansible_host variable.

3. Add Hostname to /etc/hosts (Optional)

Adding the hostname to /etc/hosts makes it resolvable by the system:

echo “35.88.167.32 server1” >> /etc/hosts

After this addition, the command using just server1 will succeed.

 

Refernces: Ansible