# Nested looping with with\_dict and with\_file, in Ansible v 1.7.2

**URL:** <https://forum.ansible.com/t/nested-looping-with-with-dict-and-with-file-in-ansible-v-1-7-2/21949>\
**Category:** Ansible Project\
**Created:** [July 7, 2016, 12:01pm UTC](https://forum.ansible.com/t/nested-looping-with-with-dict-and-with-file-in-ansible-v-1-7-2/21949 "2016-07-07T12:01:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junaid\_Naseer](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@Junaid\_Naseer](https://forum.ansible.com/u/Junaid_Naseer)\
**Post date:** [July 7, 2016, 12:01pm UTC](https://forum.ansible.com/t/nested-looping-with-with-dict-and-with-file-in-ansible-v-1-7-2/21949/1 "2016-07-07T12:01:02Z")

</div>

- I am almost certain this is not possible in **Ansible** (at least not in version **1.7.2** ), but just to be thorough in my investigation, after exhausting all lookups on **google** , **stackoverflow** , **serverfault** , etc. I am posting it now here.
- I have a list of usernames in a dictionary, something like this:

`  
keymap:  
userx:  
property1: xyz  
property2: abc

`usery:`  
`property1: xyz`  
`property2: abc`  
`

- And then I have a set of files, essentially named like this in a folder:

` **$ /tmp/pub_keys/username_hostname** `

- I need to merge all the keys for one user from all the hosts into one authorized key.

- So all keys from _userx_ in one file, and all keys from _usery_ in one file and so on.

- To grab the keys from the files over all hosts, I tried with the following just to test, and it works fine.  
`lookup('file', '/tmp/pub_keys/username_'+ansible_hostname+'.pub' )`  
Next Step, was to try the following,

- `

- This make my problem quite similar to this one: [https://serverfault.com/questions/649721/how-can-i-have-a-nested-loop-with-a-fileglob-pattern](https://serverfault.com/questions/649721/how-can-i-have-a-nested-loop-with-a-fileglob-pattern)

- To get the list of all users, I need to use the **with\_dict** module to get the usernames from the dictionary _keymap,_ as named above.

- But it seems there is no way to make **with\_dict** and **with\_file** work together.

- I tried a number of permutations with **with\_nested** and using **lookup** with **fileglob** , but to no avail. The entire list of all permutations tried is maybe too much to copy paste here.

- I tried setting variables using **set\_facts** and **vars** to avoid using the **with\_dict** in the same play, but I could not get the vars defined in one play to be visible in a different play (my bad, I am an ansible n00b).

- The only thing that I did not try is **with\_subelements**. And anyways, I think it is not usable in my case, since I do not know beforehand ‘the list of hosts for a given user’, in the dictionary.

- To expand on, what _not working_ means, (before I get burnt at the stake for saying, those words ), I explain below:

- When I use a construct like the one listed here: [https://docs.ansible.com/ansible/playbooks\_loops.html#nested-loops](https://docs.ansible.com/ansible/playbooks_loops.html#nested-loops), which in my case then becomes,

- `

- The key returned in the above case, does not grab the list of files, but instead starts returning, the literal string **`"{{ lookup('fileglob',`** … and so on till the end of the string.

- Replacing **fileglob** with **pipe** or **file** , did not get rid of the problem. It seems the problem stems from my (right or wrong) use of **with\_nested**

- Things that I avoided using, but will most certainly work: are using **python** and/or the **shell** to do delegate this work from ansible to bash or python. I was trying to solve this problem natively using ansible and ansible alone.

- [https://stackoverflow.com/questions/24250418/finding-file-name-in-files-section-of-current-ansible-role](https://stackoverflow.com/questions/24250418/finding-file-name-in-files-section-of-current-ansible-role)

**tl,dr**

Is it possible to use, **with\_dict** together with **with\_file** to achieve a nested-loop construct in **ansible 1.7.2**? It seems not.  
And especially so after reading the 2nd answer to this question: [https://stackoverflow.com/questions/31566568/double-loop-ansible](https://stackoverflow.com/questions/31566568/double-loop-ansible)

p.s. why ansible 1.7.2, it comes standard with Debian 8.5 (jessie)?

---

<div class="post-metadata">

**Author:** ![Junaid\_Naseer](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@Junaid\_Naseer](https://forum.ansible.com/u/Junaid_Naseer)\
**Post date:** [July 11, 2016, 6:39am UTC](https://forum.ansible.com/t/nested-looping-with-with-dict-and-with-file-in-ansible-v-1-7-2/21949/2 "2016-07-11T06:39:42Z")

</div>

- p.s. why ansible 1.7.2, it comes standard with Debian 8.5 (jessie)?

Err, 😮 I wanted to explain, why am I using, Ansible 1.7.2. and not something new, since it comes standard with the Debian 8.5 distribution. I know it is old. But that is not in my hand. Anyways.
