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 }}"








I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND