Multiple SSH Keys

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.

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

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

Thanks again!!

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