Bootstrap FreeKB - Ansible - Looping through a dictionary using with_dict
Ansible - Looping through a dictionary using with_dict

Updated:   |  Ansible articles

with_dict can be used to loop through the keys and values in a dictionary.

---
- hosts: localhost
  tasks:
  - name: loop through the dictionary
    debug:
      msg: "{{ item }}"
    with_dict:
      - { name: 'john.doe', department: 'it' }
      - { name: 'jane.doe', department: 'sales' }
      - { name: 'jack.doe', department: 'hr' }
...

 

Better yet, the dictionary can be defined. Here is how you would create a dictionary of food using the vars plugin and then looped.

---
- hosts: localhost
  vars:
    employees:
      - { name: 'john.doe', department: 'it' }
      - { name: 'jane.doe', department: 'sales' }
      - { name: 'jack.doe', department: 'hr' }
  tasks:
  - name: loop through the dictionary
    debug:
      msg: "{{ item }}"
    with_dict: "{{ employees }}"
...

 

Which should return the following.

ok: [localhost] => (item={u'key': u'department', u'value': u'it'}) => {
    "msg": {
        "key": "department", 
        "value": "it"
    }
}
ok: [localhost] => (item={u'key': u'name', u'value': u'john.doe'}) => {
    "msg": {
        "key": "name", 
        "value": "john.doe"
    }
}
ok: [localhost] => (item={u'key': u'department', u'value': u'sales'}) => {
    "msg": {
        "key": "department", 
        "value": "sales"
    }
}
ok: [localhost] => (item={u'key': u'name', u'value': u'jane.doe'}) => {
    "msg": {
        "key": "name", 
        "value": "jane.doe"
    }
}
ok: [localhost] => (item={u'key': u'department', u'value': u'hr'}) => {
    "msg": {
        "key": "department", 
        "value": "hr"
    }
}
ok: [localhost] => (item={u'key': u'name', u'value': u'jack.doe'}) => {
    "msg": {
        "key": "name", 
        "value": "jack.doe"
    }
}

 


when parameter

Often, the when parameter is used to only return items in the dictionary that contain a certain key or value. In this example, only items in the dictionary that contain the 'name' key will be returned.

---
- hosts: localhost
  vars:
    employees:
      - { name: 'john.doe', department: 'it' }
      - { name: 'jane.doe', department: 'sales' }
      - { name: 'jack.doe', department: 'hr' }
  tasks:
  - name: loop through the dictionary
    debug:
      msg: "{{ item }}"
    with_dict: "{{ employees }}"
    when: item.key == "name"
...

 

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.

skipping: [localhost] => (item={u'key': u'department', u'value': u'it'}) 
ok: [localhost] => (item={u'key': u'name', u'value': u'john.doe'}) => {
    "msg": {
        "key": "name", 
        "value": "john.doe"
    }
}
skipping: [localhost] => (item={u'key': u'department', u'value': u'sales'}) 
ok: [localhost] => (item={u'key': u'name', u'value': u'jane.doe'}) => {
    "msg": {
        "key": "name", 
        "value": "jane.doe"
    }
}
skipping: [localhost] => (item={u'key': u'department', u'value': u'hr'}) 
ok: [localhost] => (item={u'key': u'name', u'value': u'jack.doe'}) => {
    "msg": {
        "key": "name", 
        "value": "jack.doe"
    }
}

 


Accessing keys / values

Here is how you would return the key and value in a loop.

---
- hosts: localhost
  vars:
    employees:
      - { name: 'john.doe', department: 'it' }
      - { name: 'jane.doe', department: 'sales' }
      - { name: 'jack.doe', department: 'hr' }
  tasks:
  - name: loop through the dictionary
    debug:
      msg: "{{ item.key }} = {{ item.value }}"
    with_dict: "{{ employees }}"
...

 

Which should return the following.

ok: [localhost] => (item={u'key': u'department', u'value': u'it'}) => {
    "msg": "department = it"
}
ok: [localhost] => (item={u'key': u'name', u'value': u'john.doe'}) => {
    "msg": "name = john.doe"
}
ok: [localhost] => (item={u'key': u'department', u'value': u'sales'}) => {
    "msg": "department = sales"
}
ok: [localhost] => (item={u'key': u'name', u'value': u'jane.doe'}) => {
    "msg": "name = jane.doe"
}
ok: [localhost] => (item={u'key': u'department', u'value': u'hr'}) => {
    "msg": "department = hr"
}
ok: [localhost] => (item={u'key': u'name', u'value': u'jack.doe'}) => {
    "msg": "name = jack.doe"
}

 




Did you find this article helpful?

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



Comments


Add a Comment


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