Does Ansible manages all hosts in parallel or just five? (-f and :serial)

Viewed 7937

I read this two ansible docs:

ansible-playbook -f --> Statement 1

ansible-playbook :serial --> Statement 2

and I found this two statements:

Statement 1

-f <FORKS>, --forks <FORKS>

specify number of parallel processes to use (default=5)

Statement 2

Rolling Update Batch Size. By default, Ansible will try to manage all of the machines referenced in a play in parallel. For a rolling update use case, you can define how many hosts Ansible should manage at a single time by using the serial keyword:

Question

What's correct? Does ansible uses all hosts at once or just 5? Or is maybe 5 just the default value of the -f parameter?

Thanks for clarifying that!

Cheers

2 Answers

--forks sets number of hosts on which the current task is executed simultaneously (see Ansible Configuration Settings)

serial sets number (or percentage/fraction) of hosts on which the playbook is run first, and after succesful completion, it is applied on another number of hosts (see Delegation, Rolling Updates, and Local Actions)

Example:

With settings:

  • Inventory with host[1-7]
  • Playbook with task1 and task2
  • --fork 2
  • serial: 3

The procedure is:

  1. run task1 on host1 and host2
  2. run task1 on host3
  3. run task2 on host1 and host2
  4. run task2 on host3
  5. finish playbook run on host[1-3]
  6. run task1 on host4 and host5
  7. run task1 on host6
  8. run task2 on host4 and host5
  9. run task2 on host6
  10. finish playbook run on host[4-6]
  11. run task1 on host7
  12. run task2 on host7
  13. finish playbook run on host7

Because:

  • you are executing one task on 2 hosts at a time at most (--fork)
  • you are executing whole playbook on 3 hosts at a time at most (serial)

Reading this doc (Ansible Performance Tuning) should provide all the detail you require. However in summary, --forks broadly configures the amount of parallel processes that Ansible can spawn, and therefore the maximum number of hosts that can be configured in parallel. The value of --fork defaults to 5.

If however, serial is set to a lower figure than is in effect via the --forks parameter, then this is the number of hosts that will be targeted in parallel for the task or play on which the serial parameter is set.

The combination is useful as it allows you to tune your playbook to run against the best number of hosts for performance, but then limit certain tasks to a fraction of that value (for example to ensure that a service restart is staggered rather than applied to all hosts at the same time, thus ensuring no downtime on that service from the POV of end users).

Related