In this tutorial, we will learn about the various features of Ansible that we can configure to modify its parallelism capabilities.
Ansible connects to the defined hosts by default. As you can guess, Ansible performs these actions in parallel, executing the same task across all the target machines before moving on to the next task.
Let us discuss the various parameters that govern the parallelism features of Ansible.
Ansible Forks Configuration
The main parameter that governs Ansible’s parallelism mechanism is the forks configuration.
Forks represent the number of parallel processes that Ansible uses to communicate with remote hosts.
The default forks value is 5 which means that Ansible will communicate with up to 5 hosts simultaneously. However, we can modify this value using the “ansible.cfg” configuration file.
Edit the Ansible configuration file and set the parameter as follows:
forks = 10
You can also specify the value of the fork in the command line using the -f parameter as follows:
ansible-playbook playbook.yml -f 10
Ansible Strategies and Throttle Control
The second factor that governs the Ansible parallelism is the strategy and throttle control parameters.
The strategy plugin dictates the order in which Ansible executes the defined tasks. The default is the linear strategy which means that the tasks are executed on all hosts before moving to the next ones. Another common strategy is free which allows each host to run at its speed.
To set a strategy, you can define it in the playbook as follows:
The throttle parameter is useful to restrict the number of machines that a task can run on simultaneously.
For example, if we perform a rolling update, we might not want all hosts to update simultaneously.
- name: Rolling update
Ansible Async Tasks and Polling
Ansible also allows us to configure the asynchronous execution where some tasks can run in the background and free Ansible to execute other tasks in the meantime.
Ansible Async Task
To run a task asynchronously in Ansible, we can use the async parameter as shown in the following:
In this example, the task runs in the background for a maximum of 600 seconds.
Polling, on the other hand, allows Ansible to check the status of an asynchronous task. If we set the value of the poll parameter to a positive value, Ansible will regularly check the task’s status. However, if we set the value to 0, the task will run in the background without any checks.
In this case, Ansible checks the task’s status every 10 seconds.
In this comprehensive tutorial, we learned about the parallelism features of Ansible. We discussed about controlling the number of parallel tasks with forks, strategies, and throttling and even managing the asynchronous tasks.