Joeri Verdeyen bio photo

Joeri Verdeyen

Web-engineer, cyclist, Nespresso lover, Strava pusher.

Twitter LinkedIn Instagram Github Stackoverflow Strava

Ansible: Add SSH key to Gitlab

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.


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.

The code!

{% gist 476b4ec898f0537aff33 %}

Thanks for reading

Feel free to leave a comment if you have remarks or like this post