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

1,005 views
Skip to first unread message

Joanna Delaporte

unread,
Jun 29, 2016, 2:43:39 PM6/29/16
to Ansible Project
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']


Joanna Delaporte

unread,
Jun 30, 2016, 12:11:58 PM6/30/16
to Ansible Project
Apparently with_subelements needs a list, not a dict? 

Joanna Delaporte

unread,
Jul 5, 2016, 4:13:37 PM7/5/16
to Ansible Project
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?


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


The syntax I am aiming for is --users=user1.  However, I am having trouble accessing the usernames with with_subelements. 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. Is there a reason I cannot access it with item.0.key?

Slavek Jurkowski

unread,
Oct 13, 2016, 11:10:32 AM10/13/16
to Ansible Project
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:

Joanna Delaporte

unread,
Oct 13, 2016, 2:14:36 PM10/13/16
to ansible...@googlegroups.com
Slavek,

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

Joanna

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/4jqKl1v69mI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/52bb457e-6b09-4c66-b619-4cc0074a8eef%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--


Joanna Delaporte
Linux Systems Administrator | Parkland College
joannad...@gmail.com

Slavek Jurkowski

unread,
Oct 13, 2016, 2:21:17 PM10/13/16
to ansible...@googlegroups.com
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


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



To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAG5Enc%2Bev%2BJCMrJ964oy1Gdao%3DryBQ7Nywc06ppy4_%2Bx2Q0DVw%40mail.gmail.com.

Guy Matz

unread,
Nov 7, 2016, 1:03:58 PM11/7/16
to ansible...@googlegroups.com
I think you want {{ item.0.name }}

Slavek



--
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 ansible-project+unsubscribe@googlegroups.com.

To post to this group, send email to ansible-project@googlegroups.com.

Slavek Jurkowski

unread,
Nov 7, 2016, 1:10:46 PM11/7/16
to Ansible Project
yes you are right.. my code example wasn't tested in this thread. My actual use case does have the brackets. 
Slavek


To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.



--


Joanna Delaporte
Linux Systems Administrator | Parkland College
joannad...@gmail.com

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/4jqKl1v69mI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Hugo Slabbert

unread,
Jun 15, 2017, 1:36:41 PM6/15/17
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages