Joeri Verdeyen

Ansible: Add SSH key to Gitlab

Published on February 19, 2015

While creating a staging server playbook I came across the following issue.
I want to be able to clone repositories from a private Gitlab instance, as it contains some required libraries.
This can be done with SSH Agent Forwarding, but that doesn’t fit my needs.

Automate everything!

As always, I try to automate as much as possible and keep it maintainable.
So I’ve created a small Ansible role to create a deploy user on a targeted server and then push the generated SSH key onto the private Gitlab instance. You can always restrict the user’ access in Gitlab.
Now I can easily use this private key to clone a private repository.

How?

Creating the deployer user is straight forward. After that an API call to the Gitlab instance will add the SSH key.
The user already has an account in the Gitlab instance (using a restricted deployer user), to authorize and authenticate for the API call.