Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automate usage of SSH local key for git deployment with ansible

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.

like image 814
oskargicast Avatar asked Feb 21 '14 05:02

oskargicast


People also ask

How does Ansible use SSH keys?

By default, Ansible assumes you are using SSH keys to connect to remote machines. SSH keys are encouraged, but you can use password authentication if needed with the --ask-pass option. If you need to provide a password for privilege escalation (sudo, pbrun, and so on), use --ask-become-pass .

How do I clone a git repository in Ansible?

Cloning a Git Repository with Ansible playbookEdit the file and add the following entries. In the playbook above, you started by defining a new task and gave it the name “Clone a GitHub repository". Next, you are using the git module to specify the link to the SQLite GitHub repository.

What are the different ways other than SSH by which Ansible can connect to remote hosts?

By default, Ansible ships with several connection plugins. The most commonly used are the paramiko SSH, native ssh (just called ssh), and local connection types. All of these can be used in playbooks and with /usr/bin/ansible to decide how you want to talk to remote machines.


1 Answers

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.

like image 63
Igor Pomaranskiy Avatar answered Oct 06 '22 00:10

Igor Pomaranskiy