DevOps / Ansible Interview questions
How can you optimize playbook execution speed?
- Increase forks (
forks = 20or higher in ansible.cfg) so more hosts are processed in parallel instead of the default 5. - Disable unnecessary fact gathering (
gather_facts: false) for plays that don't need host facts. - Enable fact caching (e.g. to Redis or a JSON file) so repeated runs don't re-gather facts from scratch every time.
- Use pipelining (
pipelining = True) to reduce the number of SSH round trips per task. - Prefer the free strategy for independent hosts, so faster hosts aren't held up waiting for slower ones on every task.
- Batch related changes into fewer, well-designed modules rather than many small shell/command tasks, since built-in modules are generally more efficient than shelling out.
Most large gains come from parallelism (forks) and cutting unnecessary work (facts, redundant SSH connections) rather than from micro-optimizing individual task logic, so it's worth checking these settings before assuming a slow playbook needs a rewrite.
More Related questions...