Ansible

Ansible Loop Dictionary

Ansible is an exceptional free and open-source automation tool that allows us to configure and automate the DevOps tasks such as configuration management, application deployment, infrastructure orchestration, etc.

One of Ansible’s many powerful features is the ability to loop over items and execute tasks based on each item. We can combine the power of Ansible to loop over items with data types such as dictionaries to perform complex automation scenarios.

In this tutorial, we will learn how to configure and loop over dictionaries in Ansible using the YAML features.

What Is a Dictionary?

Let us start by discussing what a dictionary in the context of Ansible and related disciplines is.

A dictionary refers to a collection of key-value pairs. Each key is unique and maps to a specific value.

We can use dictionaries in Ansible to define multiple variables and their associated values, making them particularly useful in configuration management and automation tasks.

The following shows the structure of a dictionary in Ansible:

variable_name:
  key1: value1
  key2: value2
  ...

Create a Dictionary in Ansible

In Ansible, we can define Ansible in a playbook inside the roles or within the variable files. An example demonstration is shown in the following:

---
- hosts: localhost
  vars:
    user_data:
      dev: 'Developer'
      root: 'Admin'
      test: 'Tester'

In this case, we define a dictionary that contains three-key value pairs representing the username and corresponding role.

Ansible Loop Dictionary

Once we define a dictionary in Ansible, we can use the “loop” keyword to iterate over the key and values of a dictionary.

Iterate Over Both Keys and Values

We can use the “dict2item” filter to get both the key and value of the dictionary in a given loop. An example usage is as follows:

---
- hosts: localhost
  vars:
    user_data:
      dev: 'Developer'
      root: 'Admin'
      test: 'Tester'
  tasks:
    - name: Display users and their roles
      debug:
        msg: "User {{ item.key }} is a {{ item.value }}"
      loop: "{{ user_data | dict2items }}"

Once we run the given playbook, we should get an output as follows:

ok: [localhost] => (item={'key': 'dev', 'value': 'Developer'}) => {
    "msg": "User dev is a Developer"
}
ok: [localhost] => (item={'key': 'root', 'value': 'Admin'}) => {
    "msg": "User root is a Admin"
}
ok: [localhost] => (item={'key': 'test', 'value': 'Tester'}) => {
    "msg": "User test is a Tester"
}

Iterate Over Dictionary Values

In other cases, we may only need the dictionary keys, not the values. In this case, we can use the values filter as follows:

---
- hosts: localhost
  vars:
    user_data:
      dev: 'Developer'
      root: 'Admin'
      test: 'Tester'
  tasks:
    - name: Display only roles
      debug:
        msg: "Role {{ item }}"
      loop: "{{ user_data.values() | list }}"

Output:

ok: [localhost] => (item=Developer) => {
    "msg": "Role Developer"
}
ok: [localhost] => (item=Admin) => {
    "msg": "Role Admin"
}
ok: [localhost] => (item=Tester) => {
    "msg": "Role Tester"
}

Iterate Over Dictionary Keys

Similarly, we can get the keys of the dictionary only using the keys filter as demonstrated in the following example:

---
- hosts: localhost
  vars:
    user_data:
      dev: 'Developer'
      root: 'Admin'
      test: 'Tester'
  tasks:
    - name: Display only users
      debug:
        msg: "User {{ item }}"
      loop: "{{ user_data.keys() | list }}"

Output:

ok: [localhost] => (item=dev) => {
    "msg": "User dev"
}
ok: [localhost] => (item=root) => {
    "msg": "User root"
}
ok: [localhost] => (item=test) => {
    "msg": "User test"
}

Conclusion

Ansible provides powerful tools for looping over the dictionary structures which allows us to access and utilize the data in various ways. Using this tutorial, you understood the core structure of dictionaries and the loop syntax in Ansible playbooks.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list