Ansible

Ansible Parallelism

Ansible is a powerful, free, open-source automation tool that is used to manage and configure the remote hosts simultaneously. One of Ansible’s significant features is its ability to execute tasks in parallel across different hosts.

In this tutorial, we will learn about the various features of Ansible that we can configure to modify its parallelism capabilities.

Ansible Parallelism

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.

Setting Forks

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:

[defaults]
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:

- hosts: all
  strategy: free
  tasks:
    - ...

Ansible Throttle

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.

- hosts: all
  tasks:
    - name: Rolling update
      command: /opt/scripts/update.sh
      throttle: 2

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:

- name: Start a long-running process
  command: /opt/scripts/long_running_script.sh
  async: 600
  poll: 0

In this example, the task runs in the background for a maximum of 600 seconds.

Ansible Polling

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.

- name: Wait for the long-running process to finish
  command: /opt/scripts/long_running_script.sh
  async: 600
  poll: 10

In this case, Ansible checks the task’s status every 10 seconds.

Conclusion

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list