# No idea how to do this... please help

**URL:** <https://forum.ansible.com/t/no-idea-how-to-do-this-please-help/27100>\
**Category:** Ansible Project\
**Created:** [March 23, 2018, 6:18pm UTC](https://forum.ansible.com/t/no-idea-how-to-do-this-please-help/27100 "2018-03-23T18:18:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dayton\_Jones](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@Dayton\_Jones](https://forum.ansible.com/u/Dayton_Jones)\
**Post date:** [March 23, 2018, 6:18pm UTC](https://forum.ansible.com/t/no-idea-how-to-do-this-please-help/27100/1 "2018-03-23T18:18:19Z")

</div>

I have 2 separate dictionaries that I need to iterate over to 1) create a user and 2) add their ssh keys (some users might have multiple keys)  
The first task works, the second…not so much - I’ve tried so many things, I can’t even remember (with\_subelements, with\_items, lookups, etc). How would I iterate over the 2 dictionaries to properly add the correct key(s) to each user created in the first task? I know it would easier to combine the 2 dictionaries, but (for reasons) I can’t at this time.

`

---

<div class="post-metadata">

**Author:** ![Gary\_Collins](https://avatars.discourse-cdn.com/v4/letter/g/3d9bf3/32.png) [@Gary\_Collins](https://forum.ansible.com/u/Gary_Collins)\
**Post date:** [March 23, 2018, 6:33pm UTC](https://forum.ansible.com/t/no-idea-how-to-do-this-please-help/27100/2 "2018-03-23T18:33:08Z")

</div>

Hi,

I do something similar…

users:

- user\_name: user1

user\_uid: 667

user\_key: “https://URL-TO-KEY-/user1/id\_rsa.pub”

- user\_name: user2

user\_group: wheel

user\_key: “https://URL-TO-KEY-/user2/id\_rsa.pub”

This way you can iterate over add user with same object users

TASK

Let Add Keys for the users defined in group\_vars/users

- name: addd authorized\_keys for users

authorized\_key:

user: “{{ item.user\_name }}”

state: present

key: “{{ item.user\_key }}”

with\_items: “{{users}}”

---

<div class="post-metadata">

**Author:** ![Dayton\_Jones](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@Dayton\_Jones](https://forum.ansible.com/u/Dayton_Jones)\
**Post date:** [March 23, 2018, 6:43pm UTC](https://forum.ansible.com/t/no-idea-how-to-do-this-please-help/27100/3 "2018-03-23T18:43:34Z")

</div>

The reason I have 2 dictionaries (and can’t combine them) is primarily the “public\_ssh\_keys” resides in group\_var/all/ssh\_keys.yml and contains all the user keys. But ssh\_users reside in group\_vars/[group\_name].yml and the list of users can be different per [group name]

While I know having a structure like:

public\_ssh\_keys:

- user: user1

key: key

group:

- group1

- group4

- user: user2

key:

- key1

- key2

group:

- group1

- group2

- group6

makes sense, and would make this play trivial, I can’t make that change at this time… .

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 23, 2018, 6:52pm UTC](https://forum.ansible.com/t/no-idea-how-to-do-this-please-help/27100/4 "2018-03-23T18:52:39Z")

</div>

Then you need to use loop in a loop  
[https://docs.ansible.com/ansible/latest/playbooks\_loops.html#loop-control](https://docs.ansible.com/ansible/latest/playbooks_loops.html#loop-control)

---

<div class="post-metadata">

**Author:** ![Dayton\_Jones](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@Dayton\_Jones](https://forum.ansible.com/u/Dayton_Jones)\
**Post date:** [March 23, 2018, 7:24pm UTC](https://forum.ansible.com/t/no-idea-how-to-do-this-please-help/27100/5 "2018-03-23T19:24:52Z")

</div>

Yes, that’s what I’ve been trying - just failing. I’ve tried “with\_subelements” “with\_nested” even attempted something like:  
key: |

{% for name, keys in public\_ssh\_keys if name in ssh\_users %}

…

But haven’t hit it yet…

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 23, 2018, 7:47pm UTC](https://forum.ansible.com/t/no-idea-how-to-do-this-please-help/27100/6 "2018-03-23T19:47:32Z")

</div>

I don't use dict that much, I try to avoid them and use list instead so look at this as pseudocode since it like has some errors, but I think you get the idea.

- include\_tasks: include.yml  
&nbsp;&nbsp;with\_dict: '{{ public\_ssh\_keys }}'  
&nbsp;&nbsp;loop\_control:  
&nbsp;&nbsp;&nbsp;&nbsp;loop\_var: outer

include.yml

---

<div class="post-metadata">

**Author:** ![Dayton\_Jones](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@Dayton\_Jones](https://forum.ansible.com/u/Dayton_Jones)\
**Post date:** [March 23, 2018, 8:04pm UTC](https://forum.ansible.com/t/no-idea-how-to-do-this-please-help/27100/7 "2018-03-23T20:04:30Z")

</div>

Ah… thanks! That’s almost got it. It fails when a “user” in public\_ssh\_keys isn’t in ssh\_users, but I can work with that. The biggest issue though, is if a user has multiple keys in public\_ssh\_keys, only the first one gets placed. So I’ll need to figure out how to check for multiple keys and ensure all get added for the user…

Thanks again for the help, seems simple now that I look at it - was just too close I guess…

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 23, 2018, 8:20pm UTC](https://forum.ansible.com/t/no-idea-how-to-do-this-please-help/27100/8 "2018-03-23T20:20:04Z")

</div>

> Ah... thanks! That's almost got it. It fails when a "user" in  
> public\_ssh\_keys isn't in ssh\_users, but I can work with that.

That can be solved by a when statement

&nbsp;&nbsp;when: outer.key in ssh\_users

> The biggest  
> issue though, is if a user has multiple keys in public\_ssh\_keys, only the  
> first one gets placed. So I'll need to figure out how to check for  
> multiple keys and ensure all get added for the user...

That's was the hole point out include.yml, it loops over all the keys and add them.

---

<div class="post-metadata">

**Author:** ![Dayton\_Jones](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@Dayton\_Jones](https://forum.ansible.com/u/Dayton_Jones)\
**Post date:** [March 23, 2018, 9:42pm UTC](https://forum.ansible.com/t/no-idea-how-to-do-this-please-help/27100/9 "2018-03-23T21:42:14Z")

</div>

Ah…the problem was the “exclusive: yes” directive… I removed that, and all the keys are present as expected…thanks again!
