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

--- 
interfaces:
  lo0:
    addresses:
      - address: 10.1.1.1
        mask: 32
      - address: 10.1.1.2
        mask: 32
        secondary: True
    mode: layer3
  mgmt0:
    addresses:
      - address: 10.2.1.1
        mask: 25
    vrf: management
    mode: layer3
  Ethernet1/16:
    addresses:
      - address: 10.3.1.1
        mask: 31
    mode: layer3
    neighbor:
      name: switch-b
      interface: Ethernet1/1

The intention here is looping over the addresses subelement, but being able 
to extract the interface name.  Part of the reasoning for that setup is to 
be able to reference interfaces simply by name (e.g. interfaces.eth0) in 
other tasks/plays rather than needing to iterate over a list of interface 
and doing a when statement on a name attribute, e.g. when item.name == eth0.

I'm currently solving this with includes and with_dict on the outer task, 
but being able to reference item.key directly in a single task while still 
iterating over subelements would be a much cleaner solution.

-- 
Hugo


On Thursday, October 13, 2016 at 8:10:32 AM UTC-7, Slavek Jurkowski wrote:
>
> Joanna,
>
> Did you find any solution to this? I'm facing the same issue and it sure 
> sucks!
>
> Thanks!
>
> On Wednesday, June 29, 2016 at 1:43:39 PM UTC-5, Joanna Delaporte wrote:
>>
>> 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:
>> ---
>> staff:
>>   user1:
>>     uid: 
>>     firstname: 
>>     lastname:  
>>     comment: ""
>>     gid: 
>>     shell: /bin/bash
>>     passwordhash: ""
>>     primarygroup: 
>>     extragroups: ['code']
>>   user2:
>>     uid: 
>>     firstname: 
>>     lastname: 
>>     comment: ""
>>     gid: 
>>     shell: /bin/bash
>>     passwordhash: ""
>>     primarygroup: 
>>     extragroups: ['dm', 'code']
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/a662e0e5-b8b3-4a0f-b1b7-142e76a29859%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to