Senior
How to speed up the execution of Ansible playbooks?
sobes.tech AI
Answer from AI
To speed up the execution of Ansible playbooks, several approaches can be used:
- Parallelism: increase the
forksparameter in the Ansible configuration (default is 5) to run more tasks simultaneously. - Using
asyncandpoll: run long tasks asynchronously to avoid blocking the playbook execution. - Inventory optimization: exclude unnecessary hosts, use dynamic inventory for relevance.
- Fact caching: use
fact_cachingto avoid collecting facts on every run. - Minimize tasks: combine tasks, avoid redundant module calls.
- Using
check_modeandchanged_when: to avoid unnecessary changes and reruns. - Using
delegate_toandrun_once: for tasks that need to be executed only once.
Example of increasing parallelism in ansible.cfg:
[defaults]
forks = 20
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts
fact_caching_timeout = 86400
It is also recommended to profile the playbook using ANSIBLE_DEBUG=true and --profile to identify bottlenecks.