# 'join' a list and a dict

**URL:** <https://forum.ansible.com/t/join-a-list-and-a-dict/16359>\
**Category:** Ansible Project\
**Created:** [September 26, 2014, 1:00pm UTC](https://forum.ansible.com/t/join-a-list-and-a-dict/16359 "2014-09-26T13:00:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![oravirt](https://avatars.discourse-cdn.com/v4/letter/o/3e96dc/32.png) [@oravirt](https://forum.ansible.com/u/oravirt)\
**Post date:** [September 26, 2014, 1:00pm UTC](https://forum.ansible.com/t/join-a-list-and-a-dict/16359/1 "2014-09-26T13:00:59Z")

</div>

Hi,

I’ve got the following structures (or at least I’m hoping to be able to use them)

`  
list\_of\_files:

- { filename: file1, version: 1 }
- { filename: file2, version: 1 }
- { filename: file3, version: 1 }
- { filename: file1, version: 2 }
- { filename: file2, version: 2 }
- { filename: file1, version: 3 }
- { filename: file1, version: 4 }
- { filename: file1, version: 5 }
- { filename: file2, version: 5 }

dict\_name:  
key1:  
type: x  
dictversion: 1  
key2:  
type: y  
dictversion: 2  
key3:  
type: z  
dictversion: 1

`

I’d like to use **dict\_name.key.dictversion** to download all files from **list\_of\_files** where **dict\_name.keyX.dictversion==list\_of\_files.version.**  
Meaning, for each key, I’d like to get the (variable) number of files with the versionnumber that specific key has.

I tried with this:

`

- name: Get files  
get\_url: url={{ source }}/{{ item.0.filename }} dest={{ somewhere }}  
with\_together:
- dict\_name
- list\_of\_files  
when: “{{ dict\_name[item.1].dictversion }}” == "{{ item.0.version }}  
`

But it doesnt work. It errors out with a stacktrace:

File “/usr/lib/python2.7/site-packages/ansible/runner/ **init**.py”, line 561, in \_executor  
exec\_rc = self.\_executor\_internal(host, new\_stdin)  
File “/usr/lib/python2.7/site-packages/ansible/runner/ **init**.py”, line 696, in \_executor\_internal  
complex\_args=complex\_args  
File “/usr/lib/python2.7/site-packages/ansible/runner/ **init**.py”, line 756, in \_executor\_internal\_inner  
if not utils.check\_conditional(cond, self.basedir, inject, fail\_on\_undefined=self.error\_on\_undefined\_vars):  
File “/usr/lib/python2.7/site-packages/ansible/utils/ **init**.py”, line 254, in check\_conditional  
conditional = template.template(basedir, conditional, inject, fail\_on\_undefined=fail\_on\_undefined)  
File “/usr/lib/python2.7/site-packages/ansible/utils/template.py”, line 115, in template  
varname = template\_from\_string(basedir, varname, vars, fail\_on\_undefined)  
File “/usr/lib/python2.7/site-packages/ansible/utils/template.py”, line 357, in template\_from\_string  
res = jinja2.utils.concat(rf)  
File “”, line 10, in root  
File “/usr/lib/python2.7/site-packages/jinja2/environment.py”, line 372, in getattr  
return getattr(obj, attribute)  
File “/usr/lib/python2.7/site-packages/jinja2/runtime.py”, line 463, in **getattr**  
return self.\_fail\_with\_undefined\_error()  
File “/usr/lib/python2.7/site-packages/jinja2/runtime.py”, line 457, in \_fail\_with\_undefined\_error  
raise self.\_undefined\_exception(hint)  
UndefinedError: dict object has no element None

Is this even possible?

I could work around this by adding something like a ‘download: true/false’ to the list and use that in the when:, but I really like the to be able to control which files to get by using the dictversion variable in **dict\_name**

regards  
/Micke

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [September 26, 2014, 2:10pm UTC](https://forum.ansible.com/t/join-a-list-and-a-dict/16359/2 "2014-09-26T14:10:18Z")

</div>

​Try targetting the values of the dict:​

with\_together:

---

<div class="post-metadata">

**Author:** ![oravirt](https://avatars.discourse-cdn.com/v4/letter/o/3e96dc/32.png) [@oravirt](https://forum.ansible.com/u/oravirt)\
**Post date:** [September 27, 2014, 10:27am UTC](https://forum.ansible.com/t/join-a-list-and-a-dict/16359/3 "2014-09-27T10:27:17Z")

</div>

Ok, haven’t seen that before and I’m starting to think I’m getting out of my depth here, so efter a lot of trial I managed this:

`

---

<div class="post-metadata">

**Author:** ![oravirt](https://avatars.discourse-cdn.com/v4/letter/o/3e96dc/32.png) [@oravirt](https://forum.ansible.com/u/oravirt)\
**Post date:** [October 1, 2014, 5:32pm UTC](https://forum.ansible.com/t/join-a-list-and-a-dict/16359/4 "2014-10-01T17:32:45Z")

</div>

So I havent made any progress with this, and given the lack of response I take it this will not work?

The workaround hack it is then

/Micke

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 2, 2014, 8:46pm UTC](https://forum.ansible.com/t/join-a-list-and-a-dict/16359/5 "2014-10-02T20:46:40Z")

</div>

Probably that nobody is trying it 🙂

---

<div class="post-metadata">

**Author:** ![Mikael\_Sandstrom](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@Mikael\_Sandstrom](https://forum.ansible.com/u/Mikael_Sandstrom)\
**Post date:** [October 7, 2014, 6:59am UTC](https://forum.ansible.com/t/join-a-list-and-a-dict/16359/6 "2014-10-07T06:59:53Z")

</div>

Yeah, fair enough 😉

It would be interesting to know though if this isn’t working because:

1. It should/could work but I’m not doing it right
2. It doesnt/will not work and I should just stop trying

Either way, the stacktrace is kinda ugly?
