Ansible

Ansible Throttle

Ansible is a free and open-source automation tool that allows the system administrators to dynamically and programmatically set up, configure, and manage the remote servers. Ansible uses a declarative language that is written in YAML, known as Ansible playbooks, to describe the system configuration. Ansible playbooks are, therefore, human-readable and is easy to write, read, and maintain which makes it an exceptional tool even for non-programmers.

While running these playbooks, a need may arise to control the number of hosts that a particular task runs on simultaneously, especially when dealing with hundreds or thousands of hosts. This is where the concept of throttling in Ansible comes into play.

What Is Throttling in Ansible?

In Ansible, the concept of throttling refers to the process of limiting the number of hosts that a task can operate on at the same time.

By default, Ansible tries to manage all the hosts that are specified in a playbook, simultaneously. However, in some cases, doing so may overload the resources or cause other unintended side effects.

For example, if a task involves restarting a service on hundreds of servers at once, it might momentarily disrupt the operation of your system. Using throttling, we can control this operation on ten servers at a time, minimizing disruption.

How Does Ansible Throttling Work?

Although we cannot dive into the implementation and inner workings of the throttle feature in Ansible, here is a quick overview of how it works.

Ansible uses forks configuration to handle the throttling mechanism. The forks configuration tells Ansible how many hosts it can manage concurrently.

Throttling, on the other hand, allows us to specify a number that is smaller or equal to the value of the fork to limit the task execution.

For instance, if we set forks to 50 but throttle a task to 10, Ansible manages up to 50 hosts simultaneously for most tasks. But for the throttled task, it only manages ten hosts concurrently.

Configure Throttling in Ansible

We can configure throttling directly in a playbook for individual tasks using the throttle keyword. An example syntax is shown in the following:

- name: Task Name

module_name
:

module_parameter
: value

throttle
: NUMBER

Where NUMBER is the number of hosts that we want the task to run on concurrently.

Example:

The following playbook shows how to perform a basic throttle configuration in an Ansible task.

For example, suppose we have a task that restarts a given service and we want it to run on a maximum of 5 hosts at once. We can use the throttle feature as follows:

---
- name
: Throttling in Ansible
hosts
: all
tasks
:
- name
: Restart web service
service
:
name
: nginx
state
: restarted
throttle
: 5

In this case, Ansible restarts the Nginx service five hosts at a time.

Throttling with Loop

We can also combine throttling with loops.

For example, suppose we are updating a list of packages on remote servers, but we do not wish to overload the network or the package repositories. We can define a playbook as shown in the following:

---
- name
: Throttling in Ansible
hosts
: all
vars
:
packages
:
- vim
- mariadb
tasks
:
- name
: Update packages
yum
:
name
: "{{ item }}"
state
: latest
loop
: "{{ packages }}"
throttle
: 10

Using Throttle with Serial

Ansible also allows us to use the throttle feature with the serial keyword. The serial keyword mainly defines the number of hosts that an entire play can operate on.

An example playbook is demonstrated in the following:

---
- name
: Throttle and Serial
hosts
: all
serial
: 20
tasks
:
- name
: Copy a large file
copy
:
src
: /largefile.txt
dest
: /destination/
throttle
: 5

Ansible manages 20 hosts simultaneously in this playbook, thanks to the serial keyword. However, for copying the large file, Ansible executes it on only 5 of those 20, simultaneously.

Conclusion

You now learned the workings of the throttling feature in Ansible to manage the resources efficiently and prevent potential system disruptions when executing tasks on many hosts. You also learned how to combine throttle with other Ansible features, such as serial and loop, to create a more precise and customized playbook functionality.

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