🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Working with Ansible loop and iterators with Example

Ansible loop provides a lot of methods to repeat certain tasks until a condition is met. A basic example which can be used to install a lot of Linux packages can be written like the below example.

- name: Ansible Loop example
  apt:
    name: "{{ item }}"
    state: present
  with_items:
     - python3
     - ca-certificates
     - git

In the above task, instead of writing 3 separate task we have consolidated them into a single task. In each iteration, the value of with_items block will be inserted in place of {{ item }}.

Ansible loop with Index In some scenarios knowing the index value might come in handy. You can use the “with indexed_items” for this. The loop index will be available at item.0 and the value will be available at item.1. index value starts at zero as usual.

- hosts: all
  tasks:
  - name: Ansible loop with index example
    debug:
      msg: "echo loop index at {{ item.0 }} and value at {{item.1}}"
    with_indexed_items:
      - "hello1"
      - "hello2"
      - "hello3"

You can also make changes to the index value like addition, subtraction etc.

 - name: Ansible loop with index modification example
    debug:
      msg: "echo loop index at {{ item.0 + 1}} and value at {{item.1}}"

Ansible loop with conditional

You can also use the “when” conditional statement along with the loop structure. Thus you can control the looping based on a variable or system facts. The following example will run the task when the loop value is the same as the “loop_1” variable. Note that “item” is not enclosed in double brackets.

- hosts: all
  vars:
    loop_1: "hello1"
  tasks:
  - name: Ansible loop with conditional example
    debug:
      msg: "{{ item }}"
    with_items:
      - "hello1"
      - "hello2"
      - "hello3"
    when: item == "{{ loop_1 }}"

You can also use the system facts to determine which items to run. For eg., the following tasks will be run only when the target machine is MacOSX.

- hosts: all
  tasks:
  - name: Ansible loop with conditional example
    debug:
      msg: "{{ item }}"
    with_items:
      - "hello1"
      - "hello2"
      - "hello3"
    when: ansible_distribution == "MacOSX"

Looping through Dictionaries

You can loop through Ansible dictionary variable using the with_dict parameter. In the following task, I have declared a variable ‘Fruits’ with 3 key-value pairs. I am using the with_dict to loop through all the values.

- hosts: all
  vars:
    Fruits:
      Apple: 'Red'
      Orange: 'Orange'
      Grapes: 'Greem'
  tasks:
  - name: Ansible dictionary loop Example
    debug:
      msg: "Key is {{ item.key}} and color is {{item.value}}"
    with_dict: "{{ Fruits }}"

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.