2 minute read

Ansible, a powerful automation tool, empowers system administrators and DevOps engineers to streamline their infrastructure management tasks. One of Ansible’s key strengths lies in its loop control mechanisms, which allow for efficient and flexible automation workflows. In this article, we will explore the various loop control statements offered by Ansible, demonstrating how they can be leveraged to enhance playbook execution and achieve fine-grained control over iterative tasks.

1. Looping with loop_control

Ansible provides the loop_control directive, which allows you to manipulate the behavior of loops. This directive can be used to customize loop variables, change loop execution patterns, and modify loop output.

- name: Execute task with custom loop variable
  debug:
    msg: "Item: {{ custom_var }}"
  with_items:
    - apple
    - banana
    - orange
  loop_control:
    loop_var: {{ custom_var }}

In the above example, the loop_var option sets the loop variable to custom_var, allowing us to reference the current item as custom_var instead of the default item. By using the raw statements, we ensure that the curly braces are treated as literal text and not interpreted as Jinja2 template expressions.

2. Looping with loop_control for Output Customization

The loop_control directive also provides options to customize loop output, such as changing the label displayed for each loop iteration.

- name: Execute task with custom loop output
  debug:
    msg: "Item: {{ item }}"
  with_items:
    - apple
    - banana
    - orange
  loop_control:
    label: "Processing {{ item }}"

By using the label option, we modify the output label for each iteration, providing more descriptive information during playbook execution. The raw statements ensure that the curly braces are treated as literal text.

3. Looping with when Condition

The when condition allows you to control whether a task should execute within a loop, based on certain conditions or variables.

- name: Execute task conditionally within a loop
  debug:
    msg: "Item: {{ item }}"
  with_items:
    - apple
    - banana
    - orange
  when: {{ item == 'apple' }}

In this example, the task will only execute when the loop item is equal to 'apple'. The when condition provides flexibility and fine-grained control over the execution of tasks within loops. The raw statements ensure that the curly braces are treated as literal text.

4. Looping with until Condition

The until condition is used to repeatedly execute a task until a specified condition is met.

- name: Wait for service to start
  service:
    name: myservice
    state: started
  until: {{ result.status == "running" }}
  register: result

This example demonstrates a task that will be executed repeatedly until the value of result.status is "running". The task will keep running until the desired state is achieved. By using raw statements, we ensure that the curly braces are treated as literal text and not interpreted as Jinja2 template expressions