# Manage SSh-Keys

**URL:** <https://forum.ansible.com/t/manage-ssh-keys/19571>\
**Category:** Ansible Project\
**Created:** [October 21, 2015, 10:20am UTC](https://forum.ansible.com/t/manage-ssh-keys/19571 "2015-10-21T10:20:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrDth](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@DrDth](https://forum.ansible.com/u/DrDth)\
**Post date:** [October 21, 2015, 10:20am UTC](https://forum.ansible.com/t/manage-ssh-keys/19571/1 "2015-10-21T10:20:58Z")

</div>

Hello Ansible Community,

I’m working on a possebility to manage different ssh public keys to different servers. I try to accomplish that with the most efficient and automated solution. My problem is that i want to lookup the files directly with a loop variable. Therefore I use the lookup plugin logically. But I want the keys I choose to be the only ones in the authorized\_keys file on the remote machine. So is there a chance to use the exclusive parameter of the authorized keys module with a loop variable which uses the lookup module?

Thank you for your time!

---

<div class="post-metadata">

**Author:** ![esco\_real](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@esco\_real](https://forum.ansible.com/u/esco_real)\
**Post date:** [October 22, 2015, 9:25am UTC](https://forum.ansible.com/t/manage-ssh-keys/19571/2 "2015-10-22T09:25:16Z")

</div>

Why not just template the authorized\_keys file?

---

<div class="post-metadata">

**Author:** ![Javier\_Palacios](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@Javier\_Palacios](https://forum.ansible.com/u/Javier_Palacios)\
**Post date:** [October 22, 2015, 9:49am UTC](https://forum.ansible.com/t/manage-ssh-keys/19571/3 "2015-10-22T09:49:19Z")

</div>

If you are using 1.9, there is a “exclusive” parameter that I believe makes exactly what you want, although it will force you into some extra work if you want multiple allowed keys

Javier Palacios

---

<div class="post-metadata">

**Author:** ![DrDth](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@DrDth](https://forum.ansible.com/u/DrDth)\
**Post date:** [October 22, 2015, 11:55am UTC](https://forum.ansible.com/t/manage-ssh-keys/19571/4 "2015-10-22T11:55:38Z")

</div>

@esco This wouldn’t manage the different users on the remote systems

@Javier It does not exactly what I want, cause the last key in the loop is the one who becomes exclusive so none of the others will be in the authorized\_keys file. I need an option for keeping all keys and only those which i choose to be the ones.

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [October 22, 2015, 12:05pm UTC](https://forum.ansible.com/t/manage-ssh-keys/19571/5 "2015-10-22T12:05:25Z")

</div>

What about a parameterised role that takes the user name, then you  
“copy:” a public key (nested under e.g. your-role/files/home/{{ user }}/.ssh/pubkey) up to /home/{{ user }}/.ssh/authorized\_keys

you can use that sort of role with with\_items or similar to provision all the users you want to a given group of hosts pretty easily, there’s no need for lookups as you already have the file to hand.

---

<div class="post-metadata">

**Author:** ![DrDth](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@DrDth](https://forum.ansible.com/u/DrDth)\
**Post date:** [October 22, 2015, 12:39pm UTC](https://forum.ansible.com/t/manage-ssh-keys/19571/6 "2015-10-22T12:39:36Z")

</div>

I have a server on which this playbook will be executed. There is a directory on this server on which all public keys of all computers in the network will be stored in single keyfiles. I edit a variable which provides a list of the users who will have access with there keys on the assigned servers. And the users in this list should be the only keys in the remote auth\_keys file. But there has to be more than one key in every key parameter of the authorized\_key module. Since the exclusive parameter uses the last given file in the loop var I sort of have to stack the keys together in maby a variable or a file or something. this “stacking” together is my problem all other problems are solved. i hope that I made my problem more clear 🙂

---

<div class="post-metadata">

**Author:** ![cohen.ben](https://avatars.discourse-cdn.com/v4/letter/c/c89c15/32.png) [@cohen.ben](https://forum.ansible.com/u/cohen.ben)\
**Post date:** [October 24, 2015, 6:01pm UTC](https://forum.ansible.com/t/manage-ssh-keys/19571/7 "2015-10-24T18:01:39Z")

</div>

I think you should take a look at the authorized\_keys module again – you can pass multiple keys to a single invocation when using the exclusive option which will enforce that exactly and only the list of keys you supplied are in the specified authorized\_keys file.

---

<div class="post-metadata">

**Author:** ![Larry\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/d26b3c/32.png) [@Larry\_Smith1](https://forum.ansible.com/u/Larry_Smith1)\
**Post date:** [October 27, 2015, 2:09pm UTC](https://forum.ansible.com/t/manage-ssh-keys/19571/8 "2015-10-27T14:09:11Z")

</div>

I am using this method here that I created a few weeks ago and it works really well for me. Maybe it will work well for you too?

[https://github.com/mrlesmithjr/ansible-manage-ssh-keys](https://github.com/mrlesmithjr/ansible-manage-ssh-keys)
