Introduction Slow Ansible execution becomes a critical issue as infrastructure scales. Playbooks that take minutes on 10 hosts can take hours on hundreds of hosts without proper optimization.
Symptoms - Playbook takes hours to complete on large host counts - Tasks execute one host at a time instead of in parallel - Fact gathering takes a disproportionate amount of time - SSH connections bottleneck - Memory usage spikes during execution
Common Causes - Default forks count too low (default: 5) - Fact gathering not cached - Serial mode limiting parallelism - SSH connection overhead (no pipelining) - Unnecessary gather_facts on every play
Step-by-Step Fix 1. **Increase forks count**: ```bash ansible-playbook site.yml -f 50 # Or in ansible.cfg: # [defaults] # forks = 50 ```
- 1.Enable SSH pipelining:
- 2.```ini
- 3.[defaults]
- 4.pipelining = True
[ssh_connection] pipelining = True ssh_args = -o ControlMaster=auto -o ControlPersist=60s ```
- 1.Cache facts to avoid repeated gathering:
- 2.```ini
- 3.[defaults]
- 4.gathering = smart
- 5.fact_caching = jsonfile
- 6.fact_caching_connection = /tmp/ansible_facts_cache
- 7.fact_caching_timeout = 3600
- 8.
`