[ansible-project] Re: Access dict key when using with_subelements

2016-11-28 Thread General Luken
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:

---
- name: SSH key is installed
  authorized_key: user="{{_users_username}}" key="{{_ssh_key_item}}"
  loop_control:
loop_var: _ssh_key_item
  with_items: "{{_users_ssh_keys}}"

I hope it will help someone.

On Thursday, April 7, 2016 at 12:47:01 AM UTC+2, Peter Thorson wrote:
>
> 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.
>

-- 
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+unsubscr...@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/101a738c-e351-434c-91fe-0397e0c7232b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: Access dict key when using with_subelements

2016-10-13 Thread Peter Thorson
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.

On Thursday, October 13, 2016 at 10:10:32 AM UTC-5, Slavek Jurkowski wrote:
>
> Hi Peter,
>
> Did you find a solution to this? I'm facing the same issue and it sure 
> sucks!
>
> Thanks
>
>
> On Wednesday, April 6, 2016 at 5:47:01 PM UTC-5, Peter Thorson wrote:
>>
>> 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.
>>
>

-- 
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+unsubscr...@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/51dfa4f3-d582-4043-945b-0eb094495ec0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.