Stage 4 · Provision
Dynamic Inventory & Scale
Performance & Forks
forks, pipelining, mitogen, and fact caching for large fleets.
Understanding Forks
Forks control how many hosts Ansible processes simultaneously. The default is 5 — Ansible runs tasks on 5 hosts in parallel before moving to the next 5. Increasing forks dramatically improves performance on large inventories.
[defaults]
forks = 25 # Process 25 hosts in parallel
timeout = 30 # SSH connection timeout
[ssh_connection]
pipelining = True # Reduce SSH connectionsSet forks to match your infrastructure capacity. Too many forks can overwhelm the control node or target hosts.
More forks means more parallel SSH connections. Your control node needs enough file descriptors and network bandwidth. Start with 25 and increase based on performance.
SSH Pipelining
[ssh_connection]
pipelining = True
ssh_args = -o ControlMaster=auto -o ControlPersist=60s
# Requires on managed nodes:
# Defaults !requiretty in /etc/sudoersPipelining sends the module and its data in a single SSH connection instead of multiple. This reduces connection overhead significantly.
Fact Caching
[defaults]
# Cache facts to JSON files
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts
fact_caching_timeout = 3600
# Or use Redis for faster caching
# fact_caching = redis
# fact_caching_connection = localhost:6379:0
# Or use memcached
# fact_caching = memcached
# fact_caching_connection = localhost:11211Fact caching avoids re-gathering facts on every run. With caching enabled, subsequent runs skip the setup module entirely.
Mitogen Strategy
Mitogen is a third-party strategy plugin that replaces SSH connections with a Python-based protocol. It can be 2-10x faster than default SSH, especially for small tasks with high overhead.
[defaults]
strategy_plugins = /path/to/mitogen/ansible_mitogen/plugins/strategy
strategy = mitogen_linearMitogen has limitations — it does not support all modules and can be complex to install. Test thoroughly before using in production.
Optimization Checklist
- Increase forks to 25+ for large inventories
- Enable SSH pipelining for reduced connection overhead
- Enable fact caching to skip repeated fact gathering
- Use gather_facts: no when facts are not needed
- Use async for long-running tasks on many hosts
- Minimize task count — combine related operations
- Use tags to run only relevant task subsets during development
Use time ansible-playbook site.yml to measure execution time. Profile before and after optimizations to quantify improvements.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.