Skip to main content

How to implement parallelism and rolling updates in Ansible

Interact with multiple hosts simultaneously, on a per-playbook basis with Ansible's serial keyword.
Image
Track lane number 5 with hurdles

Image by Roy Harryman from Pixabay

Parallelism describes a software's ability to spawn multiple processes to execute tasks in tandem. It also applies to Ansible's default ability to interact with numerous hosts simultaneously.

This also means that Ansible's ability to generate multiple processes when interacting with and managing hosts becomes more important the more hosts you run against it. Rather than executing for a single host at a time, Ansible will spawn a process for each host it executes against based on the configuration you define.

Implement parallelism in Ansible

Ansible's parallel processes are known as forks, and the default number of forks is five. The more forks you set, the more resources are used on the Ansible control node. If you have a large number of managed nodes and a control node with sufficient resources, especially memory, you could set the number of forks to 50 or 100, depending on your needs and environment.

You can change the default number of forks for every execution you do by editing the ansible.cfg file. You can also change the number of forks on a per-command basis, either when running an Ansible ad-hoc command or an ansible-playbook command, by using the -f flag. This flag allows you to change the default to a higher or lower value depending on the group you are executing against.

Ansible spawns only the number of processes it needs. In other words, if you set 50 forks but execute against 12, Ansible only spawns 12 processes. If you have 100 hosts and the forks value is set to 50, Ansible executes against 50 hosts at a time.

[ Get started with IT automation by downloading the Red Hat Ansible Automation Platform beginner’s guide. ]

Check your fork value by running the ansible-config view command in the directory containing an ansible.cfg file.

For example, if you want Ansible to attempt to run your playbook against 10 hosts at a time, edit your ansible.cfg file as follows:

[defaults]
retry_files_enabled = False
timeout = 60
connection = smart
interpreter_python = auto
forks = 10
inventory = /home/vcirrus-consulting/RHCE-Ansible/inventory
roles_path = /home/vcirrus-consulting/RHCE-Ansible/roles:/usr/share/ansible/roles
remote_user = ansible-devops
host_key_checking = False
command_warnings = False
deprecation_warnings = False

If you want to change your forks value using the command line, type the following:

ansible host -f desired_forks_number -m module -a “arguments”
ansible-playbook -f desired_forks_number playbook.yml

In the examples above, you can override the default forks value just like you can override any other default configuration by using the -f option.

The exception is working with network appliances or anything else without a fully operational Python stack. The Ansible control node normally sends Python scripts to be executed on the Ansible managed nodes. If you are working with devices without a Python stack, the Ansible control node does the processing. Therefore, you need to monitor performance carefully when you significantly increase the number of forks.

Use rolling updates on playbooks with the serial keyword

You can do rolling updates in Ansible using the serial keyword. This gives you the ability to specify the number of hosts you want to execute against at a time on a per-playbook basis. This also allows you to slowly ramp up your deployment in batches by providing an increasing list.

[ A free guide from Red Hat: 5 steps to automate your business. ]

Here's an example of doing rolling updates using the serial keyword:

---
- hosts: webservers
  serial:
    - 1
    - 2
    - 25%
    - 50%
  tasks:
    - name: Create Group for Webcontent
      ansible.builtin.group:
        name: webcontent
        state: present
    - name: Create Webcontent Dir
      ansible.builtin.file:
        path: /webcontent
        state: directory
        group: webcontent
        owner: ansible-devops
        mode: '2775'

This example shows a playbook where the hosts belong to a group called webservers. The serial keyword has a list that specifies how to slowly increase the number of parallel processes or hosts to execute against. This play will execute on one host, then move to two hosts, then 25% of the hosts, followed by 50% of the hosts, and then by the tasks you'd like to run. Your list can contain numbers or percentages, depending on your environment's needs.

The main advantage of using a list under the serial keyword is executing against several different hosts and wanting to roll out the changes to a few before ramping up the deployment speed.

Wrap up

Parallelism is important when executing playbooks against many hosts. In a cluster environment, all hosts might become temporarily unavailable when using Ansible's default behavior of running one task on all hosts before proceeding to the next task. To avoid this, use the serial keyword in the playbook to run hosts in batches through the entire play.

Topics:   Ansible  
Author’s photo

Robert Kimani

Robert is a Linux enthusiast and an open source advocate, currently transitioning into a site reliability engineering (SRE) role. Always striving to learn more, he's pursuing Red Hat Certified Architect - Infrastructure path certification. Besides his love for Linux, he believes in helping others More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.