Ansible - Getting Started with Dictionaries (key value pairs)

Here is how you can create an empty dictionary using vars, a dictionary that contain no keys and no values.

---
- hosts: localhost
  vars:
    food: {}
  tasks:
  - debug: 
      var: food
...

 

Which should return the following.

ok: [localhost] => {
    "food": {}
}

 

Here is how you could create a dictionary that contains some key value pairs.

---
- hosts: localhost
  vars:
    food: 
      - { key: 'fruit', value: 'apple' }
      - { key: 'fruit', value: 'banana' }
      - { key: 'veggy', value: 'onion' }
      - { key: 'veggy', value: 'pepper' }
  tasks:
  - debug: 
      var: food
...

 

Or like this.

---
- hosts: localhost
  vars:
    food:
      - key: fruit
        value: apple
      - key: fruit
        value: banana
      - key: veggy
        value: onion
      - key: veggy
        value: pepper
  tasks:
  - debug: 
      var: food
...

 

Which should return the following.

ok: [localhost] => {
    "food": [
        {
            "key": "fruit",
            "value": "apple"
        },
        {
            "key": "fruit",
            "value": "banana"
        },
        {
            "key": "veggy",
            "value": "onion"
        },
        {
            "key": "veggy",
            "value": "pepper"
        }
    ]
}

 

And here is how you could create a dictionary that contains a list of values.

---
- hosts: localhost
  tasks:
  - set_fact:
      dictionary: { list: [ 'foo', 'bar' ] }

  - debug:
      var: dictionary
...

 


Looping through a dictionary 

The following parameters can be used to loop through each item in a dictionary.

In this example, the loop parameter is used to loop over the food dictionary.

- name: "loop through the 'food' hash"
  debug: 
    msg: "{{ item }}"
  loop:
    - { key: 'fruit', value: 'apple' }
    - { key: 'fruit', value: 'banana' }
    - { key: 'veggy', value: 'onion' }
    - { key: 'veggy', value: 'pepper' }

 

Which should return the following. Note that some or all of the output can be suppressed or limited using the no_log or loop_control parameters.

TASK [loop through the 'food' hash] 
ok: [server1.example.com] => (item={'key': 'fruit', 'value': 'apple'}) => {
    "msg": {
        "key": "fruit",
        "value": "apple"
    }
}
ok: [server1.example.com] => (item={'key': 'fruit', 'value': 'banana'}) => {
    "msg": {
        "key": "fruit",
        "value": "banana"
    }
}
ok: [server1.example.com] => (item={'key': 'veggy', 'value': 'onion'}) => {
    "msg": {
        "key": "veggy",
        "value": "onion"
    }
}
ok: [server1.example.com] => (item={'key': 'veggy', 'value': 'pepper'}) => {
    "msg": {
        "key": "veggy",
        "value": "pepper"
    }
}

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 8f140 in the box below so that we can be sure you are a human.