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
- 1Test SSH manually: `ssh -v user@host` to see where it hangs
- 2Verify connectivity: `ping <host>` and `nc -zv <host> 22`
- 3Check inventory file for typos in hostnames/IPs
- 4Verify SSH key is correct: `ssh -i /path/to/key user@host`
- 5Check firewall rules on target: `sudo iptables -L` or cloud security groups
>Fix
- 1Open SSH port in the firewall or security group
- 2Fix the hostname/IP in the Ansible inventory
- 3Copy SSH key to target: `ssh-copy-id user@host`
- 4Increase timeout: `ansible.cfg` → `timeout = 30`
- 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