DevOpsil
ansibleHigh

Fix: Ansible SSH Connection Timeout

Ansible playbook hangs on 'Gathering Facts'

!Symptoms

  • Ansible playbook hangs on 'Gathering Facts'
  • Error: 'Failed to connect to the host via ssh: Connection timed out'
  • Some hosts reachable, others timeout
  • ansible -m ping returns UNREACHABLE

?Root Causes

  • Target host firewall blocking SSH port (22)
  • Incorrect inventory hostname or IP address
  • SSH key not authorized on the target host
  • Network security group or ACL blocking the connection
  • Host is down or unreachable from the Ansible control node

#Diagnosis Steps

  1. 1Test SSH manually: `ssh -v user@host` to see where it hangs
  2. 2Verify connectivity: `ping <host>` and `nc -zv <host> 22`
  3. 3Check inventory file for typos in hostnames/IPs
  4. 4Verify SSH key is correct: `ssh -i /path/to/key user@host`
  5. 5Check firewall rules on target: `sudo iptables -L` or cloud security groups

>Fix

  1. 1Open SSH port in the firewall or security group
  2. 2Fix the hostname/IP in the Ansible inventory
  3. 3Copy SSH key to target: `ssh-copy-id user@host`
  4. 4Increase timeout: `ansible.cfg` → `timeout = 30`
  5. 5Use a bastion/jump host if target is in a private network: `ansible_ssh_common_args='-o ProxyJump=bastion'`

*Prevention

  • Use dynamic inventory that pulls from cloud provider APIs
  • Test connectivity before running playbooks with `ansible all -m ping`
  • Store SSH config in ansible.cfg for consistent connection settings
  • Automate firewall rules as part of infrastructure provisioning
  • Use Ansible vault for SSH key management

Related Error Messages

Connection timed out during banner exchangessh: connect to host port 22: Connection timed outFailed to connect to the host via sshUNREACHABLE! => changed=false