# complex dictionary reference including "item" question

**URL:** <https://forum.ansible.com/t/complex-dictionary-reference-including-item-question/25121>\
**Category:** Ansible Project\
**Created:** [July 28, 2017, 7:52pm UTC](https://forum.ansible.com/t/complex-dictionary-reference-including-item-question/25121 "2017-07-28T19:52:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerry1](https://avatars.discourse-cdn.com/v4/letter/j/8c91f0/32.png) [@jerry1](https://forum.ansible.com/u/jerry1)\
**Post date:** [July 28, 2017, 7:52pm UTC](https://forum.ansible.com/t/complex-dictionary-reference-including-item-question/25121/1 "2017-07-28T19:52:25Z")

</div>

Hi,

I am wondering if it possible to do this kind of form in a playbook. I know how to do it in a template:

If I have some data of the form:  
mydict:

- entry1:  
data: xyzzy  
filename: scroll.txt
- entry2:  
data: boom  
filename: sound.txt

things:

- entry1
- entry2

In a template, I can to do this:  
{% for item in things %}  
data = {{ mydict[item][‘data’] }}  
filename = {{ mydict[item][‘filename’] }}  
{% endfor %}

What I want to do is in a role task is

- name test copy from variable  
copy:  
content: "{{ mydict[item][‘data’] }}  
dest: "{{ target\_dir }}/{{ mydict[item][‘filename’] }}’  
with\_items: things

I can’t seem to find the right way to get this to work in a playbook. All my attempts with dots and brackets have failed.  
Can someone explain how to represent this in a playbook?

thanks in advance,  
jerry

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 28, 2017, 8:47pm UTC](https://forum.ansible.com/t/complex-dictionary-reference-including-item-question/25121/2 "2017-07-28T20:47:10Z")

</div>

> I am wondering if it possible to do this kind of form in a playbook. I know  
> how to do it in a template:
> 
> If I have some data of the form:  
> mydict:  
> &nbsp;&nbsp;&nbsp;- entry1:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data: xyzzy  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename: scroll.txt  
> &nbsp;&nbsp;&nbsp;- entry2:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data: boom  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename: sound.txt
> 
> things:  
> &nbsp;&nbsp;&nbsp;- entry1  
> &nbsp;&nbsp;&nbsp;- entry2
> 
> In a template, I can to do this:  
> {% for item in things %}  
> data = {{ mydict[item]['data'] }}  
> filename = {{ mydict[item]['filename'] }}  
> {% endfor %}

Are you sure this work?  
This will fail in the same way as your task below.

> What I want to do is in a role task is  
> - name test copy from variable  
> &nbsp;&nbsp;&nbsp;copy:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: "{{ mydict[item]['data'] }}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ target\_dir }}/{{ mydict[item]['filename'] }}'  
> &nbsp;&nbsp;&nbsp;with\_items: things
> 
> I can't seem to find the right way to get this to work in a playbook. All  
> my attempts with dots and brackets have failed.  
> Can someone explain how to represent this in a playbook?

mydict is not a dict but a list.  
So correct syntax is  
{{ mydict[item][0]['data'] }} for the first and {{ mydict[item][1]['data'] }} for the second.

If you remove the dashes in front of entry1 and 2 you'll get a dict and both of your examples will work.

---

<div class="post-metadata">

**Author:** ![Mathias\_Ettinger](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Mathias\_Ettinger](https://forum.ansible.com/u/Mathias_Ettinger)\
**Post date:** [July 29, 2017, 7:28am UTC](https://forum.ansible.com/t/complex-dictionary-reference-including-item-question/25121/3 "2017-07-29T07:28:26Z")

</div>

You may be able to make this work with a simple:

- name test copy from variable  
&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;content: "{{ item.data }}  
&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ target\_dir }}/{{ item.filename }}'  
&nbsp;&nbsp;with\_items: "{{ mydict }}"

Why do You need an extra list?
