# Using with\_subelements, cannot reference dict item's key (v 2.2.0)

**URL:** <https://forum.ansible.com/t/using-with-subelements-cannot-reference-dict-items-key-v-2-2-0/21891>\
**Category:** Ansible Project\
**Created:** [June 29, 2016, 6:43pm UTC](https://forum.ansible.com/t/using-with-subelements-cannot-reference-dict-items-key-v-2-2-0/21891 "2016-06-29T18:43:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdelaporte](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jdelaporte/32/366_2.png) [@jdelaporte](https://forum.ansible.com/u/jdelaporte)\
**Post date:** [June 29, 2016, 6:43pm UTC](https://forum.ansible.com/t/using-with-subelements-cannot-reference-dict-items-key-v-2-2-0/21891/1 "2016-06-29T18:43:39Z")

</div>

Here’s my version: ansible 2.2.0 (devel 87928ff56b) last updated 2016/06/22 13:32:23 (GMT -500)

I’m not sure if this is a bug, or misplaced syntax. When I attempt to use a dictionary, referencing the key of the dictionary and a subelement, I get the error

`FAILED! => {"failed": true, "msg": "'dict object' has no attribute 'key'"}`

Here is my task:  
`

- name: Add users to groups  
shell: ‘ipa group-add-member {{item.1}} --users={{item.0.key}}’  
with\_subelements:
- ‘{{staff|default()}}’
- ‘extragroups’
- skip\_missing: yes

`

If I use item.0 instead of item.0.key, it tries to pass in the entire dictionary object rather than just the key, and the command spits back a “too many arguments” error, which is expected. I need just the username, eg the argument I want to pass is --users=user1. Is there a reason I cannot access it with item.0.key?

My dict object looks like this:  
`

---

<div class="post-metadata">

**Author:** ![jdelaporte](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jdelaporte/32/366_2.png) [@jdelaporte](https://forum.ansible.com/u/jdelaporte)\
**Post date:** [June 30, 2016, 4:11pm UTC](https://forum.ansible.com/t/using-with-subelements-cannot-reference-dict-items-key-v-2-2-0/21891/2 "2016-06-30T16:11:58Z")

</div>

Apparently with\_subelements needs a list, not a dict?

---

<div class="post-metadata">

**Author:** ![jdelaporte](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jdelaporte/32/366_2.png) [@jdelaporte](https://forum.ansible.com/u/jdelaporte)\
**Post date:** [July 5, 2016, 8:13pm UTC](https://forum.ansible.com/t/using-with-subelements-cannot-reference-dict-items-key-v-2-2-0/21891/3 "2016-07-05T20:13:36Z")

</div>

For a little while I thought maybe subelements cannot use a dict, but I just saw someone else’s error that with\_subelements needs a dict or a list as its first argument. So, how do I access {{item.0.key}} from a dict using with\_subelements?

---

<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:06pm UTC](https://forum.ansible.com/t/using-with-subelements-cannot-reference-dict-items-key-v-2-2-0/21891/4 "2016-10-13T14:06:12Z")

</div>

Joanna,

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

Thanks!

---

<div class="post-metadata">

**Author:** ![jdelaporte](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jdelaporte/32/366_2.png) [@jdelaporte](https://forum.ansible.com/u/jdelaporte)\
**Post date:** [October 13, 2016, 6:14pm UTC](https://forum.ansible.com/t/using-with-subelements-cannot-reference-dict-items-key-v-2-2-0/21891/5 "2016-10-13T18:14:29Z")

</div>

Slavek,

I converted my dicts to lists. 😛 It didn’t take too long with some snappy regex in vim.

Joanna

---

<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:21pm UTC](https://forum.ansible.com/t/using-with-subelements-cannot-reference-dict-items-key-v-2-2-0/21891/6 "2016-10-13T18:21:04Z")

</div>

Thanks for the quick response. I think i’m going to just hack the name into the dict like so as i want to preserve the dict structure and this least bad way i see of doing that. My dict var is auto populated from another tool so it shouldn’t be hard for me to keep these in sync.

Slavek

---

<div class="post-metadata">

**Author:** ![Guy\_Matz1](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@Guy\_Matz1](https://forum.ansible.com/u/Guy_Matz1)\
**Post date:** [November 7, 2016, 6:03pm UTC](https://forum.ansible.com/t/using-with-subelements-cannot-reference-dict-items-key-v-2-2-0/21891/7 "2016-11-07T18:03:52Z")

</div>

I think you want {{ [item.0.name](http://item.0.name) }}

---

<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:** [November 7, 2016, 6:10pm UTC](https://forum.ansible.com/t/using-with-subelements-cannot-reference-dict-items-key-v-2-2-0/21891/8 "2016-11-07T18:10:46Z")

</div>

yes you are right… my code example wasn’t tested in this thread. My actual use case does have the brackets.

---

<div class="post-metadata">

**Author:** ![Hugo\_Slabbert](https://avatars.discourse-cdn.com/v4/letter/h/76d3ee/32.png) [@Hugo\_Slabbert](https://forum.ansible.com/u/Hugo_Slabbert)\
**Post date:** [June 14, 2017, 8:07pm UTC](https://forum.ansible.com/t/using-with-subelements-cannot-reference-dict-items-key-v-2-2-0/21891/9 "2017-06-14T20:07:42Z")

</div>

This would be a super helfpul feature to have, even if it’s another lookup separate from with\_subelements. The use case I have for this is a set of interfaces on a host that are in a dict with the keys being the interface names, e.g.:

`
