DEV Community

Cover image for Control your playbook Task execution
Sunish Surendran K
Sunish Surendran K

Posted on

Control your playbook Task execution

In Ansible, fork, serial, throttle, and async are configuration parameters that control the concurrency and parallelism of task execution

Fork

It specifies the maximum number of parallel connections that Ansible will use when executing tasks. Increasing the fork value allows you to parallelize task execution across multiple hosts.
For example, setting fork: 3 means Ansible will execute tasks on up to 3 hosts simultaneously as shown below.

Ansible Fork

Serial

The serial parameter in an Ansible playbook allows you to control the number of hosts that are targeted at a time during playbook execution. It's useful for scenarios where you want to limit the rate of change or avoid overloading hosts.

For instance, if you set serial: 3, Ansible will target and execute tasks on three hosts at a time, waiting for them to complete before moving on to the next set of hosts.

Ansible Serial

Throttle

The throttle parameter in an Ansible playbook allows you to control the rate at which tasks are executed. In below example for Task 2 only one machine is executed at a time.

Ansible Throttle

Async

The async parameter in an Ansible task allows you to run that task asynchronously. It means Ansible will start the task against specific set of machines and immediately move on to the next set of machines without waiting for first set to complete.

You can also use the poll parameter in conjunction with async to specify how often Ansible should check the status of the asynchronous task. In the below example poll is provided as two, so every two second the ansible will check the status of the task executed in the machines

Ansible async

I end this blog with a question - If you want to achieve a scenario where a machine jumps to Task 2 as soon as its Task 1 is completed, while other machines are still executing Task 1.How you structure your playbook to achieve this scenario? Please comment if you know the answer.

Top comments (0)