automate usage of SSH local key for git deployment with ansible

oskargicast picture oskargicast · Feb 21, 2014 · Viewed 23.2k times · Source

I am working with vagrant and ansible. I want to automate the deployment role of ansible (You can check my repo here). For this purpose, I am trying to deploy my local ssh key into my VPS and my vagrant guest machine (I am trying SSH agent forwarding).

GOAL

Automate deployment process with git using ansible. I've already done this:

---

- name: read-write git checkout from github
  git: repo={{ repository }} dest=/home/site

Where:

---
# Variables here are applicable to all host groups

repository: [email protected]:dgnest/dgnest.git

PROBLEM

When I do: "vagrant provision", the console stop here:

TASK: [deployment | read-write git checkout from github] ********************** 

That's because I haven't set up the ssh keys.

I TRIED

I would like to use the key_file option that the git module of ansible has. But it fails too.

---                                                                             

- name: read-write git checkout from github                                     
  git: repo={{ repository }} dest=/home/site key_file=/home/oscar/.ssh/id_rsa.pub

Another option is to copy my ~/ssh/id_rsa.pub into each VPS and vagrant, but my problem in this case is to handle with all the different users. Vagrant uses the "vagrant" user and my VPS uses another ones, so I had to put my ssh local key into each of these user?

Hope you can help me. Thank you.

UPDATE:

I've just automated the @leucos answer (Thanks). Copying the private and public rsa keys. I share this link with the implementation.

Answer

Igor Pomaranskiy picture Igor Pomaranskiy · Mar 19, 2016

You don't have to copy your local SSH key to remote servers. Instead, you just create file named ansible.cfg in the directory you are running deployment scripts from, and put the next settings:

[ssh_connection]
ssh_args = -o ForwardAgent=yes

That's it, now your local identity is forwarded to the remote servers you manage with Ansible.