# Multiple SSH Keys

**URL:** <https://forum.ansible.com/t/multiple-ssh-keys/14342>\
**Category:** Ansible Project\
**Created:** [January 14, 2014, 8:20pm UTC](https://forum.ansible.com/t/multiple-ssh-keys/14342 "2014-01-14T20:20:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrianAI](https://avatars.discourse-cdn.com/v4/letter/b/cab0a1/32.png) [@BrianAI](https://forum.ansible.com/u/BrianAI)\
**Post date:** [January 14, 2014, 8:20pm UTC](https://forum.ansible.com/t/multiple-ssh-keys/14342/1 "2014-01-14T20:20:51Z")

</div>

So, I’ve noticed I have a tendency to do things a little backwards but, I’m hoping someone can clear something up for me.

Originally, i was using the file/copy module in ansible to copy the authorized\_keys2 file from the ansible server to the “node” in question. A colleague of mine stated I should use the ssh key module (authorized\_key) instead of copying the file.

Now I’ve noticed I either have to put the key in the actual playbook, or read it from a file using key=“{{ lookup(‘file’, item.key) }}” . The two problems here are 1) it doesn’t allow for comments and 2) it doesn’t allow for multiple hosts (ie: if someone has multiple source machines they may come from). The file I’m reading from has to be pure host key, that’s it.

Is there a benefit to using the authorized\_key over copying the file? I’d rather do the copy but, I figured let me ask if I’m missing something.

---

<div class="post-metadata">

**Author:** ![Erik\_Anderson](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@Erik\_Anderson](https://forum.ansible.com/u/Erik_Anderson)\
**Post date:** [January 14, 2014, 8:53pm UTC](https://forum.ansible.com/t/multiple-ssh-keys/14342/2 "2014-01-14T20:53:56Z")

</div>

The authorized\_key module _can_ handle multiple keys per file. One per line as is usual with authorized\_keys files.

I do something like:

- name: authorized\_keys - Erik  
action: authorized\_key user=erik key=“{{ lookup(‘file’, ‘pubkeys/erik’) }}” manage\_dir=yes

…where pubkeys/erik is a file with one pubkey per line.

-Erik

---

<div class="post-metadata">

**Author:** ![BrianAI](https://avatars.discourse-cdn.com/v4/letter/b/cab0a1/32.png) [@BrianAI](https://forum.ansible.com/u/BrianAI)\
**Post date:** [January 14, 2014, 9:00pm UTC](https://forum.ansible.com/t/multiple-ssh-keys/14342/3 "2014-01-14T21:00:28Z")

</div>

Ah great! Thanks for the correction, so currently (and not a huge deal) the only limitation is no comments.

Thanks again!!

---

<div class="post-metadata">

**Author:** ![Erik\_Anderson](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@Erik\_Anderson](https://forum.ansible.com/u/Erik_Anderson)\
**Post date:** [January 14, 2014, 9:02pm UTC](https://forum.ansible.com/t/multiple-ssh-keys/14342/4 "2014-01-14T21:02:53Z")

</div>

You're welcome.

There may very well be a better way to manage things, and if so, I hope  
someone chimes in. But for the meantime, this is working well for me.

-Erik
