GitHub + Ansible: deploy your public SSH keys to your servers

When I work with some mates in a side project, I need to give them access to the server. This is a quick tip about how to easily do it using GitHub and Ansible. The solution is simple and elegant.

If you and your mates work with GitHub, you may have added all your public SSH keys to GitHub. A really nice feature that GitHub provides is that you can access any user public SSH keys by visiting:

https://github.com/{{ item }}.keys

So, for example, if you want to take a look at mine, just visit:

https://github.com/carlosbuenosvinos.keys

Ok, so now we have a repository of SSH public keys, now it’s time to deploy them into your server. I love Ansible. The trick here is to use the authorized_key module and the key option. In the documentation, you can read:

The SSH public key(s), as a string or (since 1.9) url (https://github.com/username.keys).

So, you can add in your provisioning roles a task to include all your friends public SSH keys. In this example, I allow my friends Christian (theUniC), Ricard (ricardclau) and myself.

tasks:
  - name: Ensure collaborators SSH keys are installed
    authorized_key: user=root key=https://github.com/{{ item }}.keys
    with_items:
      - carlosbuenosvinos
      - theUniC
      - ricardclau

Hope it helps!