Ansible Guide
Isometric ring hub linked by dashed lines to stacked server blocks of varying size, representing a control node pushing playbook tasks across an inventory
Execution

Ansible Playbook Execution: How Forks and Idempotence Work

Trace an Ansible playbook from inventory to rerun. See what five default forks cap, how serial batches hosts, and how creates guards a task.

By Ansible Guide Editorial · ·Updated · 3 min read

Ansible pushes playbook tasks from a control node to inventory hosts matched by each play. The managed hosts need no resident Ansible agent. By default, five forks let the control node work on up to five hosts at a time. On a rerun, idempotent tasks leave hosts unchanged when they already match the desired state.

The agentless push model

Run ansible-playbook on the control node. Ansible uses SSH by default, sends module code to managed hosts as needed, and reports which tasks changed or failed on each host. See Ansible concepts, the CLI reference, and the Ansible playbooks guide.

Inventory is the source of truth for scope

The inventory defines the available hosts and groups. The play’s hosts pattern selects which of them receive its tasks. In the example below, hosts: webservers selects all eight members of the webservers group. The inventory guide explains how to define groups and narrow a pattern.

Forks control how much runs at once

Ansible defaults to five forks, which cap the number of parallel worker processes. With the default linear strategy, a task can run on five hosts at once. Ansible starts that task on the other hosts as slots become available and advances after the task has finished across the active batch. See the execution strategies guide.

This example sets the limit to 30 for one invocation with -f.

ansible-playbook -f 30 my_playbook.yml

To set the default in ansible.cfg, put forks under [defaults].

[defaults]
forks = 30

Strategy decides the order, forks decide the width

Consider an illustrative play with two tasks and eight matched hosts. Both cases use the default linear strategy and five forks. To try rolling batches, put serial: 2 at the play level alongside hosts: webservers.

Play setting How the two tasks run
Default batch For task A, five hosts can start while three wait for worker slots. Once all eight finish task A, task B proceeds under the same five-fork cap.
serial: 2 The first pair finishes task A and task B before the next pair starts task A. This repeats for four batches. At most two workers have host work at once, even with five forks available.

The strategy guide covers the other execution strategies.

Idempotence is the discipline, not a feature you get free

Ansible idempotency means repeating a task leaves the same final state. Prefer modules that check the desired state before acting. For an initialization command, use creates to guard it with a path on the managed host. This illustrative task uses the script and path from the ansible.builtin.command documentation.

- name: Initialize the database
  ansible.builtin.command:
    cmd: /usr/bin/make_database.sh db_user db_name
    creates: /path/to/database
Run Path before the task Expected behavior
First run Absent Ansible runs the script. The task reports a change if the script succeeds.
Rerun Present after the first run Ansible bypasses the script. The task reports no change.

The guard checks only path existence. The example assumes the script creates that path when initialization succeeds.

To check repeat behavior, run the playbook twice against the same sandbox hosts with the same inputs. Compare the per-host recaps. Expect no changes on the second run for hosts that already reached the desired state.

Secrets belong in Vault, not in the repository

Ansible Vault encrypts individual variables or whole files containing sensitive data. See the Ansible Vault documentation and the Vault setup guide.

Preview a run before it changes anything

Use --list-hosts to confirm the targets before executing tasks. Run ansible-playbook --check playbook.yaml to preview the guarded task. It predicts a change if the path is missing and leaves the script unexecuted. See the playbook options guide for more preview flags.

Common mistakes

A recap with zero changes can also contain failed or unreachable hosts. Check the host list and those counts before treating a rerun as confirmation that all targets reached the desired state.

Evidence and limits. The behaviors are documented in the Ansible 14 Community Documentation. The illustrative steps were left unexecuted.

Sources

  1. Ansible documentation: Ansible concepts
  2. Ansible documentation: Patterns: targeting hosts and groups
  3. Ansible documentation: Controlling playbook execution: strategies and more
  4. Ansible documentation: ansible-playbook CLI reference
  5. Ansible documentation: Ansible playbooks
  6. Ansible documentation: ansible.builtin.command module
  7. Ansible documentation: Protecting sensitive data with Ansible Vault

Related