PromptHub
DevOps Intermediate 12 views

Ansible Unreachable Host

Ansible cannot connect to the target host via SSH or WinRM.

Explanation

Ansible Unreachable Host occurs when Ansible cannot establish a connection to the managed host. Ansible uses SSH (default) or WinRM to connect to target machines, and this error indicates the connection failed. Common causes include the host being down, SSH service not running, firewall blocking the connection, wrong SSH credentials, host key verification failing, incorrect inventory configuration, DNS resolution failure, and network connectivity issues. The error message typically shows "UNREACHABLE!" with the specific connection failure.

Common Causes

  • Host is down or unreachable
  • SSH service not running
  • Firewall blocking connection
  • Wrong SSH credentials
  • DNS resolution failure

Solution

Verify the host is running and accessible: ping host-ip or ssh user@host. Check Ansible inventory for correct host addresses. Verify SSH credentials and key-based authentication. Check firewall rules for SSH (port 22) or WinRM (port 5985/5986). Test SSH connection manually: ssh -v user@host. Check known_hosts for host key issues: ssh-keyscan host-ip >> ~/.ssh/known_hosts. Increase SSH timeout in ansible.cfg. Verify DNS resolution. Use ansible with -vvv for verbose output. Check for IP changes or network routing issues.

Code Example

# Ansible unreachable host error
# ansible-playbook site.yml
# fatal: [webserver1]: UNREACHABLE! => {"changed": false, ...}

# Test connectivity manually
ssh user@webserver1
ping webserver1

# Check inventory
cat inventory.ini
[webservers]
webserver1 ansible_host=192.168.1.100

# Increase timeout in ansible.cfg
[defaults]
timeout = 30

[ssh_connection]
ssh_args = -o ControlMaster=No -o ServerAliveInterval=30

# Test with verbose output
ansible webserver1 -m ping -vvv

# Check SSH key
ansible webserver1 -m ping --private-key=~/.ssh/mykey

# Skip host key checking (development only!)
ansible webserver1 -m ping -e "ansible_host_key_checking=False"

# Check firewall
sudo ufw status  # Ubuntu
sudo firewall-cmd --list-all  # RHEL/CentOS

Error Information

Language

bash

Framework

ansible

Difficulty

Intermediate

Views

12

Related Errors