Skip to Content

Ansible: Loop over items with a pause between iterations

Some tasks may consume a significant amount of system resources, such as CPU or memory, and running too many of these tasks at once can cause performance issues.

By adding a pause between loop iterations, you can help to manage resource consumption and prevent performance issues. It can help to avoid overwhelming the target system with too many requests at once. This can be particularly useful when working with large or complex playbooks.

You can use the pause directive with loop_control to control the time between the execution of each item in a task loop in Ansible.

The pause directive is used to pause a playbook for a specific amount of time. By using loop_control with pause, you can specify the time to pause between the execution of each item in a task loop.

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!

Here’s an example:

- name: Pause between loop iterations
  hosts: all
  vars:
    items:
      - item1
      - item2
      - item3
  tasks:
    - name: Loop over items with a pause between iterations
      debug:
        msg: "Current item: {{ item }}"
      loop: "{{ items }}"
      loop_control:
        pause: 5

In this example, the pause directive is used to pause the playbook for 5 seconds between each iteration of the loop over the items variable. The debug module is used to display the current item in each iteration.

When running the playbook, it will display the current item and pause for 5 seconds before moving to the next iteration. This can be useful if you need to perform a task on multiple items with a delay between each execution, such as waiting for a service to start up before moving to the next item.

PLAY [Pause between loop iterations] ***********************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Loop over items with a pause between iterations] *****************************************************************************************************************************************************************
ok: [localhost] => (item=item1) => {
    "msg": "Current item: item1."
}
ok: [localhost] => (item=item2) => {
    "msg": "Current item: item2."
}
ok: [localhost] => (item=item3) => {
    "msg": "Current item: item3."
}

PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Here’s a more complex example of an Ansible playbook that uses loop_control with the pause directive:

- name: Manage Users
  hosts: all
  vars:
    users:
      - name: alice
        home_dir: /home/alice
        shell: /bin/bash
      - name: bob
        home_dir: /home/bob
        shell: /bin/sh
      - name: charlie
        home_dir: /home/charlie
        shell: /bin/zsh
  tasks:
    - name: Create users with a pause between iterations
      user:
        name: "{{ item.name }}"
        home: "{{ item.home_dir }}"
        shell: "{{ item.shell }}"
        state: present
      loop: "{{ users }}"
      loop_control:
        pause: 3

In this example, the playbook creates three users with different attributes specified in the “users” list variable. The loop_control directive with the pause: 3 argument adds a 3-second delay between each user creation to prevent overloading the system.

This playbook can be used to create multiple users on different servers with different attributes, and the pause directive ensures that the system is not overloaded with too many user creations at once.