# Access dict key when using with\_subelements

**URL:** <https://forum.ansible.com/t/access-dict-key-when-using-with-subelements/21082>\
**Category:** Ansible Project\
**Created:** [April 6, 2016, 10:47pm UTC](https://forum.ansible.com/t/access-dict-key-when-using-with-subelements/21082 "2016-04-06T22:47:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Thorson](https://avatars.discourse-cdn.com/v4/letter/p/dc4da7/32.png) [@Peter\_Thorson](https://forum.ansible.com/u/Peter_Thorson)\
**Post date:** [April 6, 2016, 10:47pm UTC](https://forum.ansible.com/t/access-dict-key-when-using-with-subelements/21082/1 "2016-04-06T22:47:01Z")

</div>

when using with\_dict, item.key retrieves the dict key and item.value retrieves the value.

however, when using with\_subelements and having the first item being looped over be a dict, item.0 seems to return only the value. Is there a way

concrete example:

`  
vars:  
users:  
bob:  
authorized\_keys:

- “ssh-rsa a…”
- “ssh-rsa b…”  
alice:  
authorized\_keys:
- “ssh-rsa c…”

tasks:

- authorized\_key: user=??? key=“{{ item.1 }}”  
with\_subelements:
- “{{ users }}”
- authorized\_keys  
`

How do I retrieve the username? I understand that I could change users into a list and add username as a property. I want to avoid that because this users list is used for lots of different things where being able to look up information about a user by username is required.

---

<div class="post-metadata">

**Author:** ![Slavek\_Jurkowski](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Slavek\_Jurkowski](https://forum.ansible.com/u/Slavek_Jurkowski)\
**Post date:** [October 13, 2016, 2:17pm UTC](https://forum.ansible.com/t/access-dict-key-when-using-with-subelements/21082/2 "2016-10-13T14:17:20Z")

</div>

Hi Peter,

Did you find a solution to this? I’m facing the same issue and it sure sucks!

Thanks

---

<div class="post-metadata">

**Author:** ![Peter\_Thorson](https://avatars.discourse-cdn.com/v4/letter/p/dc4da7/32.png) [@Peter\_Thorson](https://forum.ansible.com/u/Peter_Thorson)\
**Post date:** [October 13, 2016, 4:24pm UTC](https://forum.ansible.com/t/access-dict-key-when-using-with-subelements/21082/3 "2016-10-13T16:24:08Z")

</div>

I have been using a horrible hacky workaround, specifically, adding the key as a field to the dict element:

`  
vars:  
users:  
bob:  
name: bob  
authorized\_keys:

- “ssh-rsa a…”
- “ssh-rsa b…”  
alice:  
name: alice  
authorized\_keys:
- “ssh-rsa c…”

tasks:

- authorized\_key: user=item.0.name key=“{{ item.1 }}”  
with\_subelements:
- “{{ users }}”
- authorized\_keys

`

This is difficult to maintain because the two names can get out of sync, it is not intuitive at all that making a new user should include the name again when its already used as the key. It is just the least worst solution I’ve found.

I wish there was a sane, unified, flexible solution to looping in Ansible. As it stands there appear to be a bunch of one-off non-interoperable looping constructs designed for very narrow purposes. This is easily the most frustrating aspect of using Ansible for anything non-trivial.

---

<div class="post-metadata">

**Author:** ![Slavek\_Jurkowski](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Slavek\_Jurkowski](https://forum.ansible.com/u/Slavek_Jurkowski)\
**Post date:** [October 13, 2016, 6:18pm UTC](https://forum.ansible.com/t/access-dict-key-when-using-with-subelements/21082/4 "2016-10-13T18:18:17Z")

</div>

Thanks for the reply. i agree…I just converted my dict in the same way…Just wanted to check if there was something better out there that anyone has found 😛

Slavek

---

<div class="post-metadata">

**Author:** ![General\_Luken](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@General\_Luken](https://forum.ansible.com/u/General_Luken)\
**Post date:** [November 27, 2016, 10:26pm UTC](https://forum.ansible.com/t/access-dict-key-when-using-with-subelements/21082/5 "2016-11-27T22:26:51Z")

</div>

I have workaround for this. You basically have to use “include” as a function. Let’s say your {{users}} structure looks like this:

`  
users:  
ssh\_keys:

- somekeyfdsfdsfdsfsdfsdyttytry
- someotherkeyfdsryekfdlmgdfm  
`

In your main.yml there would be:

`

- include: ssh\_key.yml  
\_users\_ssh\_keys={{item.value.ssh\_keys}}  
\_users\_username={{item.key}}  
with\_dict: “{{users}}”

`

And in ssh\_key.yml you would iterate over all ssh keys like this:

`
