Skip to main content

Optimizing Ansible Performance

1. Increasing Parallelism

Option Values Notes
forks 5 Number of parallel processes to use when executing tasks
serial 10 Number of hosts to process in serial (default: 1)
max_fail_percentage 10% Maximum percentage of hosts that can fail before stopping execution

Example: Setting forks in playbook

---
- hosts: all
  forks: 10
  tasks:
    - name: Ping all hosts
      ping:

Example: Using serial for large inventories

---
- hosts: webservers
  serial: 5
  tasks:
    - name: Update packages
      apt:
        update_cache: yes

2. Enabling Pipelining

Pipelining in Ansible allows you to reduce the number of SSH connections required to execute tasks by reusing the same connection for multiple commands. This can significantly improve performance, especially when running tasks on a large number of hosts.

Option Values Notes
ansible_connection ssh Must be set to ssh for pipelining to work.
ansible_ssh_pipelining true Enables pipelining for SSH connections.

Example: Enabling Pipelining in a Playbook

---
- hosts: all
  vars:
    ansible_ssh_pipelining: true
  tasks:
    - name: Example task
      command: whoami
Note: Pipelining is not supported for all connection types. It only works with ssh connections.
Warning: Enabling pipelining can increase the load on the control node, especially when running tasks on a large number of hosts.

3. Using Fact Caching

Option Values Notes
fact_caching memory, redis, jsonfile Backend to use for caching facts
fact_caching_connection /tmp/ansible_facts.json Path to cache file or Redis URL
fact_caching_timeout 86400 Cache expiration in seconds (24h default)

Example: Enable fact caching with JSON file

[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts.json
fact_caching_timeout = 3600
Note: Fact caching can significantly improve performance when running playbooks against large inventories by avoiding repeated calls to gather system facts.

4. Disabling Fact Gathering

Option Values Notes
gather_facts false Disables fact gathering for a playbook or task

Example: Disabling facts for a playbook

---
- hosts: all
  gather_facts: false
  tasks:
    - name: Example task without facts
      debug:
        msg: "Running without facts"
Note: Disabling fact gathering can significantly improve performance for large inventories or when facts are not needed.

5. Using Async Tasks

Parameter Description Default Notes
async Number of seconds to wait for task completion 0 (synchronous) Set to 0 for synchronous execution
poll How often to check for completion (seconds) 10 Lower values provide more frequent updates
background Run task in background false Use with async to run tasks asynchronously

Example: Running an async task

- name: Run a long-running command asynchronously
  command: sleep 30
  async: 60
  poll: 5

Example: Checking async task status

- name: Check async task status
  async_status:
    jid: "{{ async_result.ansible_job_id }}"
  register: job_result
  until: job_result.finished
  retries: 10
Note: The async parameter sets the maximum time to wait for a task to complete. If the task does not finish within this time, it will be marked as failed.
Warning: Be cautious with long async tasks as they may consume system resources. Always set appropriate poll intervals to avoid excessive resource usage.

6. Implementing Strategy Plugins

Strategy Plugin Description Use Case Configuration
free Executes tasks on hosts as soon as they are available, without waiting for other hosts to complete. Best for heterogeneous environments or when hosts have different capabilities. strategy: free
debug Executes tasks sequentially, useful for debugging or testing. When troubleshooting or validating task execution order. strategy: debug
linear Executes tasks in a linear fashion, one host at a time. When strict sequential execution is required (e.g., for state-sensitive operations). strategy: linear
host_pinned Assigns tasks to a specific host and keeps them pinned until completion. For operations that must run on the same host across multiple plays or tasks. strategy: host_pinned

Example: Using the free Strategy

- name: Use free strategy for parallel execution
  hosts: all
  strategy: free
  tasks:
    - name: Gather facts from hosts
      setup:
Note: Strategy plugins can be set globally in ansible.cfg or per-playbook.
Warning: Using free strategy may lead to inconsistent task execution order in environments with varying host performance.

7. Reducing SSH Overhead

Option Values Notes
forks 50 Number of parallel processes to run. Default is 5.
timeout 30 Timeout in seconds for each SSH connection. Default is 10.
host_key_checking false Disables host key checking for faster connections. Not recommended in production.
ssh_args -o ControlMaster=auto -o ControlPersist=60s Additional SSH arguments to reduce connection overhead.

Example: SSH Control Master Configuration

ansible_ssh_args: -o ControlMaster=auto -o ControlPersist=60s
Note: Enabling SSH control master can significantly reduce connection overhead when running multiple tasks on the same hosts.

8. Using Mitogen Plugin

The Mitogen plugin for Ansible is designed to dramatically improve performance by reducing the overhead of SSH connections and minimizing the number of times Ansible needs to re-establish connections to managed nodes.

Feature Description Notes
mitogen Plugin name for enabling Mitogen optimizations Available since Ansible 2.10
ansible_connection Connection plugin to use with Mitogen Must be set to mitogen

Example: Enabling Mitogen Plugin

---
- hosts: all
  vars:
    ansible_connection: mitogen
  tasks:
    - name: Gather facts
      setup:
Note: Mitogen requires Python 3.6 or higher on the managed nodes.
Warning: Mitogen is not compatible with all Ansible modules and may require additional configuration or module-specific adjustments.

9. Optimizing Template Rendering

Option Values Notes
template_cache true Enables caching of rendered templates
jinja2_extensions [] Extensions to load for Jinja2 templating

Example: Using template_cache

---
- name: Use template cache
  hosts: all
  vars:
    template_cache: true
  tasks:
    - name: Render template with cache
      template:
        src: example.j2
        dest: /tmp/example.txt
Note: Template caching is enabled by default in newer versions of Ansible.

Example: Custom Jinja2 extensions

---
- name: Load custom Jinja2 extension
  hosts: all
  vars:
    jinja2_extensions:
      - my_custom_extension
  tasks:
    - name: Use custom extension in template
      template:
        src: custom.j2
        dest: /tmp/custom.txt

10. Batching Tasks

Option Values Notes
batch_size 5 Number of tasks to run in parallel per host
serial 10 Number of hosts to process in serial

Example: Using batch_size in a playbook

---
- name: Batch tasks across hosts
  hosts: webservers
  vars:
    batch_size: 5
  tasks:
    - name: Install packages in batches
      apt:
        name: nginx
        state: present
      batch_size: 5
Note: Batching helps reduce resource usage and avoid overwhelming systems during large-scale operations.

11. Using Persistent Connections

Option Values Notes
ansible_connection ssh Connection plugin to use for persistent connections
ansible_persistent_connect_timeout 30 Timeout in seconds for persistent connection attempts
ansible_persistent_command_timeout 30 Timeout in seconds for commands executed over persistent connection

Example: Configuring persistent connections in inventory

[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11

[webservers:vars]
ansible_connection=ssh
ansible_persistent_connect_timeout=60
ansible_persistent_command_timeout=60

Example: Using persistent connections in a playbook

- name: Use persistent connections for performance
  hosts: webservers
  connection: ssh
  vars:
    ansible_persistent_connect_timeout: 60
    ansible_persistent_command_timeout: 60
  tasks:
    - name: Gather facts with persistent connection
      setup:
Note: Persistent connections reduce the overhead of SSH key exchange and can significantly improve performance when running multiple tasks against the same hosts.

12. Minimizing Privilege Escalation

Directive Description Notes
become yes or no Controls whether privilege escalation is used. Set to no to disable.
become_method sudo, su, pfexec, doas Specifies the privilege escalation method. Defaults to sudo.
become_user User name (e.g., root) Specifies the user to escalate to. Defaults to root.
become_flags Flags passed to the become method (e.g., -H) Used to pass additional flags to the privilege escalation command.

Example: Disabling privilege escalation

- name: Run a task without privilege escalation
  command: whoami
  become: no

Example: Using become with specific user

- name: Run a task as a different user
  command: whoami
  become: yes
  become_user: ansible
Note: Privilege escalation can be a security risk. Always ensure that only necessary tasks use become.
Warning: Misconfiguring become can lead to unexpected behavior or security vulnerabilities. Ensure proper permissions and testing.

Was this article helpful?