Sobes.tech
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 forks parameter in the Ansible configuration (default is 5) to run more tasks simultaneously.
  • Using async and poll: run long tasks asynchronously to avoid blocking the playbook execution.
  • Inventory optimization: exclude unnecessary hosts, use dynamic inventory for relevance.
  • Fact caching: use fact_caching to avoid collecting facts on every run.
  • Minimize tasks: combine tasks, avoid redundant module calls.
  • Using check_mode and changed_when: to avoid unnecessary changes and reruns.
  • Using delegate_to and run_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.

How to speed up the execution of Ansible playbooks… - sobes.tech