Print key/value of each dict by iterating over a list of dicts in ansible

Viewed 32

I have a json file that contains a list of dictionaries:

$ cat test.json 
[
        {
            "key1": "val1"
        }, 
        {
            "key2": "val2"
        }, 
        {
            "key3": "val3"
        }
]

I would like to iterate over the list and print the key and value in each dict object but can't figure out a way to do so.

- hosts: localhost
  gather_facts: no
  tasks:
  - set_fact:
      mylst: "{{ lookup('file', '/home/ansible/test.json') | from_json }}"

  - debug:
       msg: "One dict is {{ item }}"
    with_items:  "{{ mylst }}"

  - debug:
       msg: "Key/values are {{ item.key }} and {{item.value }}"
    with_items:  "{{ mylst }}"


$ ansible-playbook run.yml 

PLAY [localhost] *****************************************************************************

TASK [set_fact] ******************************************************************************
ok: [localhost]

TASK [debug] *********************************************************************************
ok: [localhost] => (item={u'key1': u'val1'}) => {
    "msg": "One dict is {u'key1': u'val1'}"
}
ok: [localhost] => (item={u'key2': u'val2'}) => {
    "msg": "One dict is {u'key2': u'val2'}"
}
ok: [localhost] => (item={u'key3': u'val3'}) => {
    "msg": "One dict is {u'key3': u'val3'}"
}

TASK [debug] *********************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'key'\n\nThe error appears to be in '/home/ansible/run.yml': line 11, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - debug:\n    ^ here\n"}

PLAY RECAP ***********************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
1 Answers

The lookup plugin below

mylst: "{{ lookup('file', 'test.json')|from_json }}"

creates a list of dictionaries

mylst:
  - key1: val1
  - key2: val2
  - key3: val3

If the keys are unique combine the items

    - debug:
        var: mylst|combine

This creates a dictionary

  mylst|combine:
    key1: val1
    key2: val2
    key3: val3

Decompose the dictionaries and create the list

    - debug:
        var: mylst|combine|dict2items

gives

  mylst|combine|dict2items:
  - key: key1
    value: val1
  - key: key2
    value: val2
  - key: key3
    value: val3

Now, you can easily iterate the list and print the key/value pairs of each dictionary

    - debug:
        msg: "key={{ item.key }}, value={{ item.value }}"
      loop: "{{ mylst|combine|dict2items }}"

gives (abridged)

  msg: key=key1, value=val1
  msg: key=key2, value=val2
  msg: key=key3, value=val3

Example of a complete playbook

- hosts: localhost
  vars:
    mylst: "{{ lookup('file', 'test.json')|from_json }}"
  tasks:
    - debug:
        msg: "key={{ item.key }}, value={{ item.value }}"
      loop: "{{ mylst|combine|dict2items }}"
Related