Optimizing Ansible for Faster Playbook Execution

Ansible, a popular open-source automation tool, can be vital to managing complex IT environments. However, as your infrastructure grows, playbook executions can become slower. To address these issues and improve your playbook’s performance, here are some strategies and additional tips inspired by practices from experts and real-world scenarios.

1. Limit Resource Hogging with Forks

Ansible uses parallelism to execute tasks across different hosts, and this is controlled by the forks configuration. While increasing the number of forks allows more hosts to be managed simultaneously, it can also consume more resources. It’s essential to find a balance that fits your environment. To adjust it, modify the forks setting in your ansible.cfg:

forks = 10

A practical use case for adjusting forks is when you’re deploying applications across many small environments, such as during a microservices rollout. Increasing forks might speed up the overall deployment time.

2. Reduce Task Load with Polling Intervals

By default, Ansible waits for each task to complete on all nodes before moving on to the next one. In scenarios where tasks involve waiting (like waiting for a service to start), consider using asynchronous execution with specified polling intervals:

- name: Start the long-running process
  command: /usr/bin/long_running_process --option
  async: 3600
  poll: 0

- name: Check back later
    jid: "{{ ansible_job_id }}"
  register: job_result
  until: job_result.finished
  retries: 30
  delay: 60

This setup starts a task and then periodically checks if it’s completed, freeing up resources to execute other tasks in the meantime.

3. Optimize Fact Gathering

Fact gathering can generate a significant delay, especially when dealing with many hosts. If your plays do not require facts, you can disable the gathering using gather_facts: no, or selectively gather only necessary facts to reduce overhead:

- hosts: all
  gather_facts: yes
    - name: Gather only specific facts
          - network

4. Use Static Imports Instead of Dynamic Includes

Dynamic includes (include_tasks or import_role) can add overhead since they are evaluated during runtime. If your play’s structure is well-defined and doesn’t require conditional execution of tasks, switching to static imports (import_tasks, import_role) might provide a performance boost:

- name: Use static imports
  import_tasks: setup.yml

5. Implement Caching

Ansible supports caching facts between playbook runs to avoid unnecessary gathering of data. Set up a caching method in ansible.cfg and choose from memory, JSON files, or a Redis database:

gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible-cache
fact_caching_timeout = 3600

6. Profile Your Playbooks

Identify bottlenecks by enabling callback plugins to profile tasks:

callback_whitelist = profile_tasks

This configuration shows the execution time for each task, helping you to pinpoint where optimizations are most needed.

7. Simplify Expressions and Loops

Complex expressions and extensive loops within tasks can degrade performance. Evaluate expressions ahead of play execution or simplify them, and break down large loops into smaller, more manageable tasks.

8. Leverage Native Modules

Whenever possible, use Ansible’s native modules rather than shell commands. Native modules are optimized in Python and run more efficiently than wrapping shell scripts or commands.


Optimizing Ansible playbooks is crucial as your IT infrastructure scales. By implementing these strategies — adjusting parallel execution, optimizing fact gathering, choosing static over dynamic imports, and more — you can significantly enhance playbook execution times, streamline management tasks, and maintain a more efficient automation environment.

Remember, the key is to test changes in a controlled environment before deploying them widely, ensuring that each adjustment contributes positively to the overall performance without compromising the reliability or functionality of your orchestrated workflows.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *