Combining Conditionals and Loops in Ansible

Combining conditionals and loops in Ansible allows you to create complex automation scenarios by adding conditional logic to your looped tasks. By using this powerful combination, you can perform conditional actions for each item in a loop, enabling dynamic and targeted task execution. In this tutorial, we will explore how to combine conditionals and loops in Ansible.

Introduction to Combining Conditionals and Loops

In Ansible, combining conditionals and loops gives you granular control over task execution based on specific conditions and allows you to iterate over a list of items, performing different actions depending on the state of each item. This combination is particularly useful when you need to apply different configurations or perform different tasks based on specific conditions within a loop.

Let's take a look at an example playbook that combines conditionals and loops:

- name: Configure web servers
  hosts: webservers
    - name: Install package
        name: "{{ item }}"
        state: present
        - package1
        - package2
        - package3
      when: ansible_distribution == 'Ubuntu'

In this example, the playbook installs packages only on hosts with the Ubuntu distribution. The when conditional statement is combined with the loop construct to perform the installation for each package only on the targeted hosts.

Using Conditionals and Loops in Ansible

Here are the steps to combine conditionals and loops in Ansible:

1. Define the Looping Construct

Choose the appropriate looping construct (loop, with_items, etc.) based on your requirements. Specify the list or dictionary of items that you want to iterate over.

2. Specify the Conditional Statement

Define the conditional statement using the when keyword. The conditional statement should evaluate the desired condition based on the current item in the loop. Use the looping variable (e.g., item) to reference the current item within the conditional statement.

3. Specify the Tasks

Specify the tasks that should be executed or skipped based on the condition. Use the when keyword followed by the conditional statement within each task definition.

4. Execute the Playbook

Execute the playbook using the ansible-playbook command. Ansible will evaluate the conditional statements for each item in the loop and execute or skip the tasks accordingly.

Common Mistakes when Combining Conditionals and Loops

  • Not properly formatting the conditional statement within the loop, leading to syntax errors.
  • Using incorrect operators or functions in the conditional statement, resulting in unexpected evaluations.
  • Not considering the scope of variables used within the loop, causing incorrect evaluations or conflicts.
  • Overcomplicating the conditional statements or loops, making the playbook difficult to understand and maintain.
  • Not testing the playbook thoroughly to ensure that the combination of conditionals and loops produces the desired results.

FAQs about Combining Conditionals and Loops in Ansible

  1. Q: Can I combine multiple conditionals within a loop?

    A: Yes, you can combine multiple conditionals within a loop by using logical operators such as and or or. For example, when: condition1 and condition2.

  2. Q: Can I use different tasks for each item in the loop?

    A: Yes, you can use different tasks for each item in the loop. Simply define the tasks within the loop and specify the conditional statements accordingly.

  3. Q: Can I nest loops within loops?

    A: Yes, you can nest loops within loops to handle complex scenarios. However, be cautious as nesting too many loops can make the playbook harder to read and maintain.


Combining conditionals and loops in Ansible provides a powerful mechanism for creating dynamic and targeted automation workflows. By following the steps and best practices outlined in this tutorial, you can leverage the flexibility of conditionals and the repetitive nature of loops to perform conditional actions for each item in a loop. This combination allows you to achieve fine-grained control and make your playbooks more efficient and adaptable to different scenarios.