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.


저자 소개

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 and is compassionate about giving back to the community. When he's not on a Linux terminal, he likes hiking, mountain biking, and exploring nature.

UI_Icon-Red_Hat-Close-A-Black-RGB

채널별 검색

automation icon

오토메이션

기술, 팀, 인프라를 위한 IT 자동화 최신 동향

AI icon

인공지능

고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트

open hybrid cloud icon

오픈 하이브리드 클라우드

하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요

security icon

보안

환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보

edge icon

엣지 컴퓨팅

엣지에서의 운영을 단순화하는 플랫폼 업데이트

Infrastructure icon

인프라

세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보

application development icon

애플리케이션

복잡한 애플리케이션에 대한 솔루션 더 보기

Virtualization icon

가상화

온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래