Loops in Ansible for static/dynamic variable and dictionary with real-time usage.

Repetition of bunch of statement is called loop. Loop is conditional base if condition of loop is true then block of code is executed. with Flowchart

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.

---
hostslocalhost
  tasks:
    - nameCreate Directory
      commandmkdir /tmp/{{ item }}
      with_items:
        - new_dir1
        - new_dir2
        - new_dir3

Now run the Playbook
 

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.

---
hostslocalhost
  tasks:
    - nameAdd Multiple users
      user:
        name"{{ item.name }}"
        statepresent
        groups"{{ item.groups }}"
      loop:
        - { name"u1"groups"wheel" }
        - { name"u2"groups"root" }
 
Above Playbook is also written as below 

---
hostslocalhost
  tasks:
    - nameAdd Multiple users
      user:
        name"{{ item.name }}"
        statepresent
        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.

---
nameConfigure Vlan
  hostsm_location
  gather_factsfalse
  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:
    - nameConfigure 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.
---
nameConfigure Vlan
  hostsall
  gather_factsfalse
  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:
    - nameConfigure Vlan on Switches
      ios_config:
        parents"{{ item.int_parent }}"
        lines:
          - "{{ item.cmd }}"
          - "{{ item.shut }}"
      wheninventory_hostname == '{{ item.for_host }}'
      loop"{{ intVlan }}"

-  inventory_hostname is a universal variable that returns the running user's name.

Post a Comment