Types of Timeouts
1. SSH Connection Timeout
fatal: [server]: UNREACHABLE! => {"msg": "Failed to connect to the host via ssh:
ssh: connect to host 192.168.1.10 port 22: Connection timed out"}
# ansible.cfg
[defaults]
timeout = 30 # Connection timeout in seconds (default: 10)
[ssh_connection]
ssh_args = -o ConnectTimeout=30 -o ConnectionAttempts=3
2. Command/Task Timeout
# ansible.cfg
[defaults]
command_timeout = 60 # For network devices
For individual tasks:
- name: Long-running task
ansible.builtin.command: /opt/backup.sh
timeout: 300 # 5 minutes for this task only
3. Privilege Escalation Timeout
Timeout (12s) waiting for privilege escalation prompt
# ansible.cfg
[defaults]
timeout = 30
[privilege_escalation]
become = true
4. Persistent Connection Timeout
# For network devices
[persistent_connection]
connect_timeout = 30
command_timeout = 30
Optimizing for Slow Networks
Use Pipelining
# ansible.cfg
[ssh_connection]
pipelining = true # Reduces SSH operations — major speedup
Use ControlMaster
[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=30
Reduce Forks for Constrained Networks
[defaults]
forks = 5 # Lower = less parallel connections
Per-Host Timeout
[slow_servers]
old-server ansible_host=10.0.0.5 ansible_timeout=60
vpn-server ansible_host=10.0.0.6 ansible_timeout=45
Retry on Timeout
- name: Retry flaky connection
ansible.builtin.ping:
retries: 3
delay: 10
register: result
until: result is not failed
All Timeout Settings Reference
# ansible.cfg — complete timeout configuration
[defaults]
timeout = 30 # SSH connect timeout
gather_timeout = 30 # Fact gathering timeout
[ssh_connection]
ssh_args = -o ConnectTimeout=30 -o ServerAliveInterval=15 -o ServerAliveCountMax=3
pipelining = true
retries = 3
[persistent_connection]
connect_timeout = 60 # Network device connection
command_timeout = 60 # Network device commands
connect_retry_timeout = 15 # Retry duration
Conclusion
Match your timeout values to your environment. LAN: defaults are fine. WAN/VPN: increase to 30-60s. Cloud instances that spin up slowly: use retries with delay. Always enable pipelining and ControlMaster for better performance.