Ansible Tutorial · Advanced Topics · Lesson 17 of 20

Ansible Performance

Speed up playbook execution with strategies, pipelining and caching.

Members can save this page, take notes and track progress. See membership · Sign in

Why performance matters

Managing 5 hosts is fast. Managing 500 means every optimisation saves minutes. Here are the most effective performance levers.

Increase forks

By default Ansible runs tasks on 5 hosts in parallel. Increase forks to match your fleet:

# ansible.cfg
[defaults]
forks = 50

Or per run:

ansible-playbook site.yml -f 50

Enable pipelining

By default Ansible copies a module to the remote host, then runs it in a separate SSH session. Pipelining sends the module over the existing SSH connection:

[ssh_connection]
pipelining = True

This can cut execution time by 30-50%. It requires requiretty to be disabled in /etc/sudoers on the managed nodes.

SSH multiplexing

Reuse SSH connections instead of creating a new one for each task:

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o PreferredAuthentications=publickey

Strategies

The strategy plugin controls how Ansible executes tasks across hosts:

Strategy Behaviour
linear Default: all hosts complete task 1 before task 2
free Each host runs as fast as it can, independently
host_pinned Like free, but each fork stays with one host
- name: Fast setup
  hosts: all
  strategy: free
  tasks:
    - name: Install packages
      ansible.builtin.apt:
        name: nginx
        state: present

free is fastest when hosts have different speeds or tasks take varying time.

Fact caching

Skip gathering facts on every run by caching them:

[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts
fact_caching_timeout = 7200

For teams, use Redis or Memcached as the fact cache backend.

Async tasks

For long-running tasks, run them asynchronously:

- name: Long-running database backup
  ansible.builtin.command: /usr/local/bin/backup_db.sh
  async: 3600       # allow up to 1 hour
  poll: 0           # don't wait (fire and forget)
  register: backup_job

- name: Wait for backup to finish
  ansible.builtin.async_status:
    jid: "{{ backup_job.ansible_job_id }}"
  register: job_result
  until: job_result.finished
  retries: 60
  delay: 60

gather_subset

Collect only the facts you need:

- hosts: all
  gather_facts: true
  gather_subset:
    - min
    - network

min collects just the essential facts, skipping hardware and virtual detection.

Summary of quick wins

Optimisation Effort Impact
Increase forks Low High
Enable pipelining Low High
SSH multiplexing Low Medium
Fact caching Low Medium
strategy: free Low Medium
gather_subset: min Low Low-Medium
Check performance-related settings
ansible-config dump 2>/dev/null | grep -i "forks\|pipelining\|strategy"

Output

DEFAULT_FORKS(default) = 5
ANSIBLE_PIPELINING(default) = False
DEFAULT_STRATEGY(default) = linear

Run this on your computer — see “Get Started” for setup.

Test yourself

Which strategy lets each host run tasks independently without waiting for others?

  1. linear
  2. free
  3. serial
  4. host_pinned
Show answer

Answer: free

The 'free' strategy lets each host proceed through tasks at its own pace, without waiting for all hosts to finish each task before moving on.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake