What is a loop?
It is the repetition of a bunch of statements. Sometimes you want to repeat a similar type of statement multiple times then loop control is used. You may encounter situations when a block of code needs to be executed several times.
The loop is conditional base if the condition of the loop is true then the block of code is executed otherwise it will be terminated.
Take a look at the below Flowchart,
Loops With Ansible
By default, the loop variable name is the item.
To get the value of a variable use jinja2 template. E.g: {{ item }} or "{{ item }}".
Type-1: Loops with a static value.
Consider that the below playbook will create a directory in /tmp/ folder using loops.
---
- hosts: localhost
tasks:
- name: Create Directory
command: mkdir /tmp/{{ item }}
with_items:
- new_dir1
- new_dir2
- new_dir3
Check the output by command "ls /tmp/"
Type-2: Loop with dictionary and array of a dictionary.
What is a dictionary?
A collection of key-value pairs is called a dictionary. It is like an Associative array. Where each key has some value.
Below Playbook Show User Creation in your Linux Machine.
---
- hosts: localhost
tasks:
- name: Add Multiple users
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
loop:
- { name: "u1", groups: "wheel" }
- { name: "u2", groups: "root" }
Above Playbook is also written as below
---
- hosts: localhost
tasks:
- name: Add Multiple users
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
loop:
- name: "u3"
groups: "wheel"
- name: "u4"
groups: "root"
Check For Group to exist or not. If not then group error may come. Try it on your own.
You can add multiple values in an array using key-value pair separated by a comma.
To get the value of the item use dot( . ) operator with the item name.
E.g
From Above Playbook to get name use {{ item.name }}.
Creating playbooks with Variable using loops and condition
Below Playbook might look different but try to see how the variable is used with loops. It configures interface VLAN in cisco l3 switches.
---
- name: Configure Vlan
hosts: m_location
gather_facts: false
vars:
intVlan:
- int_parent: "interface Vlan2"
cmd: "ip address 172.16.0.254 255.255.255.0"
shut: "no shutdown"
for_host: "dDisSwitch"
- int_parent: "interface Vlan3"
cmd: "ip address 172.16.1.254 255.255.255.0"
shut: "no shutdown"
for_host: "dDisSwitch"
- int_parent: "interface Vlan4"
cmd: "ip address 172.16.2.254 255.255.255.0"
shut: "no shutdown"
for_host: "dDisSwitch"
tasks:
- name: Configure Vlan on Switches
ios_config:
parents: "{{ item.int_parent }}"
lines:
- "{{ item.cmd }}"
- "{{ item.shut }}"
loop: "{{ intVlan }}"
Now, What if you have multiple systems?
You can see the for_host variable which contains hostname. We will use the when condition to separate out the users.
#Command: Interface Vlan 10
#In all Distribution switches.
---
- name: Configure Vlan
hosts: all
gather_facts: false
vars:
intVlan:
#dDisSwitch
- int_parent: "interface Vlan2"
cmd: "ip address 172.16.0.254 255.255.255.0"
shut: "no shutdown"
for_host: "dDisSwitch"
- int_parent: "interface Vlan7"
cmd: "ip address 172.17.2.254 255.255.255.0"
shut: "no shutdown"
for_host: "mDisSwitch"
- int_parent: "interface Vlan9"
cmd: "ip address 172.18.1.254 255.255.255.0"
shut: "no shutdown"
for_host: "bDisSwitch"
- int_parent: "interface Vlan10"
cmd: "ip address 172.18.2.254 255.255.255.0"
shut: "no shutdown"
for_host: "bDisSwitch"
tasks:
- name: Configure Vlan on Switches
ios_config:
parents: "{{ item.int_parent }}"
lines:
- "{{ item.cmd }}"
- "{{ item.shut }}"
when: inventory_hostname == '{{ item.for_host }}'
loop: "{{ intVlan }}"
- inventory_hostname is a universal variable that returns the running user's name.