# accessing yaml dictionary in playbook

**URL:** <https://forum.ansible.com/t/accessing-yaml-dictionary-in-playbook/33161>\
**Category:** Ansible Project\
**Created:** [June 12, 2020, 12:35pm UTC](https://forum.ansible.com/t/accessing-yaml-dictionary-in-playbook/33161 "2020-06-12T12:35:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doug\_OLeary](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@Doug\_OLeary](https://forum.ansible.com/u/Doug_OLeary)\
**Post date:** [June 12, 2020, 12:35pm UTC](https://forum.ansible.com/t/accessing-yaml-dictionary-in-playbook/33161/1 "2020-06-12T12:35:51Z")

</div>

Hi

I have a dictionary of disks in my host vars:

`

disks:  
sdc:  
dev: sdc  
vg: vg01  
lv: app  
mp: ‘/opt/app’  
sdd:  
dev: sdd  
vg: vg02tmp  
lv: shared  
mp: ‘/opt/app/oracle/shared’

`

A **very** basic playbook, just to access the dictionary::

`  
tasks:

- debug:  
msg: “disk name: {{item}}”  
loop:
- “{{disks}}”

`

shows good results so I’m pretty sure I have the dictionary defined correctly:

`TASK [debug] ************************************************************************************************************************************************** ok: [mynewhost] => (item=[{'key': 'sdc', 'value': {'dev': 'sdc', 'vg': 'vg01', 'lv': 'app', 'mp': '/opt/app'}}, {'key': 'sdd', 'value': {'dev': 'sdd', 'vg': 'vg02tmp', 'lv': 'shared', 'mp': '/opt/app/oracle/shared'}}]) => { "msg": "disk name: [{'key': 'sdc', 'value': {'dev': 'sdc', 'vg': 'vg01', 'lv': 'app', 'mp': '/opt/app'}}, {'key': 'sdd', 'value': {'dev': 'sdd', 'vg': 'vg02tmp', 'lv': 'shared', 'mp': '/opt/app/oracle/shared'}}]" }`

How do I loop over the dictionary key? in this case, it’d be sdc and sdd… I’ve been trying iteration of {{item.key}}, and {{disks.key}} but nothing has worked yet.

Thanks for any hints/tips/suggestions.

Doug O’Leary

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [June 12, 2020, 12:44pm UTC](https://forum.ansible.com/t/accessing-yaml-dictionary-in-playbook/33161/2 "2020-06-12T12:44:09Z")

</div>

> Hi
> 
> I have a dictionary of disks in my host vars:
> 
> \>  
> disks:  
> sdc:  
> dev: sdc  
> vg: vg01  
> lv: app  
> mp: '/opt/app'  
> sdd:  
> dev: sdd  
> vg: vg02tmp  
> lv: shared  
> mp: '/opt/app/oracle/shared'  
> \>
> 
> A \*\*very\*\* basic playbook, just to access the dictionary::
> 
> \>  
> tasks:  
> -debug:  
> msg:"disk name: {{item}}"  
> loop:  
> -"{{disks}}"
> 
> \>
> 
> shows good results so I'm pretty sure I have the dictionary defined correctly:
> 
> \>  
> TASK  
> [debug]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok:[mynewhost]=\>(item=[{'key':'sdc','value':{'dev':'sdc','vg':'vg01','lv':'app','mp':'/opt/app'}},{'key':'sdd','value':{'dev':'sdd','vg':'vg02tmp','lv':'shared','mp':'/opt/app/oracle/shared'}}])=\>{  
> "msg":"disk name: [{'key': 'sdc', 'value': {'dev': 'sdc', 'vg': 'vg01', 'lv': 'app', 'mp': '/opt/app'}}, {'key':  
> 'sdd', 'value': {'dev': 'sdd', 'vg': 'vg02tmp', 'lv': 'shared', 'mp': '/opt/app/oracle/shared'}}]"  
> }  
> \>
> 
> How do I loop over the dictionary key? in this case, it'd be sdc and sdd... I've been trying iteration of  
> {{item.key}}, and {{disks.key}} but nothing has worked yet.
> 
> Thanks for any hints/tips/suggestions.

Old style:

with\_dict: "{{ disks }}"

New style:

loop: "{{ disks | dict2items }}"

In both cases the key is in item.key and values can be accessed with item.value, e.g item.value.dev.

Reference: [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_loops.html#iterating-over-a-dictionary](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#iterating-over-a-dictionary)

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Doug\_OLeary](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@Doug\_OLeary](https://forum.ansible.com/u/Doug_OLeary)\
**Post date:** [June 12, 2020, 12:45pm UTC](https://forum.ansible.com/t/accessing-yaml-dictionary-in-playbook/33161/3 "2020-06-12T12:45:51Z")

</div>

Never mind, I got it. Amazing how often I ask the question then immediately find the answer…

The trick is:

`  
tasks:

- debug:  
msg: “disk name: {{item.key}}”  
with\_dict: “{{disks}}”  
`

Theoretically, this should work too:

`  
loop:

- “{{lookup(‘dict’, disks)}}”  
`

Hopefully, that’ll help someone.

Thanks

Doug
