# Convert with\_dict to loop - help!

**URL:** <https://forum.ansible.com/t/convert-with-dict-to-loop-help/35465>\
**Category:** Ansible Project\
**Created:** [July 5, 2021, 9:52pm UTC](https://forum.ansible.com/t/convert-with-dict-to-loop-help/35465 "2021-07-05T21:52:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piya\_Attapatto](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@Piya\_Attapatto](https://forum.ansible.com/u/Piya_Attapatto)\
**Post date:** [July 5, 2021, 9:52pm UTC](https://forum.ansible.com/t/convert-with-dict-to-loop-help/35465/1 "2021-07-05T21:52:21Z")

</div>

Hello there!

I’m trying (and failing) to convert a with\_dict to a loop (have referred to this section of the docs [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_loops.html#with-dict](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-dict)) with no success. I’m looping over a list of dicts and trying to access the key attribute for use in a template.

The data looks like this:

- debug:  
var: delta

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“delta”: [  
{  
“test\_developers”: {  
“members”: [  
“foo”,  
“bar”  
]  
}  
},  
{  
“test\_qa”: {  
“members”: [  
“cheese”,  
“rice”  
]  
}  
}  
]  
}

The attribute I want to access is for example “test\_developers” or “test\_qa”, which I can do with with\_dict like so:

#loop over with\_dict and print key

- name: debug  
debug:  
msg: “{{ item.key }}”  
with\_dict: “{{ delta }}”

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item={‘key’: ‘test\_developers’, ‘value’: {‘members’: [‘foo’, ‘bar’]}}) =\> {  
“msg”: “test\_developers”  
}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 6, 2021, 7:06am UTC](https://forum.ansible.com/t/convert-with-dict-to-loop-help/35465/2 "2021-07-06T07:06:17Z")

</div>

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ group|dict2items }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ delta }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_control:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_var: group  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;label: "{{ group }}"

---

<div class="post-metadata">

**Author:** ![Piya\_Attapatto](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@Piya\_Attapatto](https://forum.ansible.com/u/Piya_Attapatto)\
**Post date:** [July 6, 2021, 11:19am UTC](https://forum.ansible.com/t/convert-with-dict-to-loop-help/35465/3 "2021-07-06T11:19:39Z")

</div>

Ah that works, thanks Dick - much appreciated!
