Using with_subelements, cannot reference dict item's key (v 2.2.0)

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:
`

Apparently with_subelements needs a list, not a dict?

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?

Joanna,

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

Thanks!

Slavek,

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

Joanna

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

I think you want {{ item.0.name }}

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

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.:

`