# Adding private key to remote host

**URL:** <https://forum.ansible.com/t/adding-private-key-to-remote-host/18789>\
**Category:** Ansible Project\
**Created:** [July 16, 2015, 11:36pm UTC](https://forum.ansible.com/t/adding-private-key-to-remote-host/18789 "2015-07-16T23:36:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eugene\_Romero](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@Eugene\_Romero](https://forum.ansible.com/u/Eugene_Romero)\
**Post date:** [July 16, 2015, 11:36pm UTC](https://forum.ansible.com/t/adding-private-key-to-remote-host/18789/1 "2015-07-16T23:36:32Z")

</div>

Hi all!

In my company, we have playbooks to set up brand new servers in different roles, so that we can quickly configure new servers in case of expansion, emergency, etc.

One of the steps involved in these setups is adding a private key to some of them. Now, this goes great 99% of the time, since it usually involves adding a .ssh/id\_rsa key, and ssh will attempt to use it automatically, (based on its name I believe)

However, the problem is when adding private keys with a non standard name. Say for example a server will hold 2 keys, one “id\_rsa” and the other “id\_rsa\_git”. Normally, the second key will not work until ssh is made aware of it with ssh-add. However, I seem to be unable to run this command with ansible.

Here’s what I have tried:

- command: ssh-add /home/{{ user }}/.ssh/id\_rsa\_git

Does not work, outputs “Could not open a connection to your authentication agent.”. Fair enough.

- shell: eval “$(ssh-agent)” && ssh-add /home/{{ user }}/.ssh/id\_rsa\_git

Seems to work, but doesn’t. Here’s the output I get if I run with the verbose flag:

changed: [REDACTED] =\> {“changed”: true, “cmd”: “eval "$(ssh-agent)" && ssh-add /home/johndoe/.ssh/id\_rsa\_git”, “delta”: “0:00:00.006037”, “end”: “2015-07-16 23:23:28.667177”, “rc”: 0, “start”: “2015-07-16 23:23:28.661140”, “stderr”: “Identity added: /home/johndoe/.ssh/id\_rsa\_git (/home/johndoe/.ssh/id\_rsa\_git)”, “stdout”: “Agent pid 28489”}

Seems about right, right? That would be the output you would get if you ran it directly on the server. However, when I go to the server and I do “ssh-add -l”, I get “The agent has no identities.” Huh?

I thought maybe the user wasn’t right. So I tried:

shell: eval “$(ssh-agent)” && ssh-add /home/{{ user }}/.ssh/id\_rsa\_git && whoami

remote\_user: johndoe

But this of course fails, since johndoe cannot ssh into servers (it’s a system user).

So, my question. Is there some sane way of doing the equivalent of ssh-add that I am not aware of?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 16, 2015, 11:56pm UTC](https://forum.ansible.com/t/adding-private-key-to-remote-host/18789/2 "2015-07-16T23:56:04Z")

</div>

ssh-add relies on environment variables that are not available to  
ansible, so when you run the ssh-agent + ssh-add you are spawning a  
2nd agent, while when you login you seem to be getting the env vars  
for the 1st agent, which did not get the keys added.

You'll have to do something like shell: . ssh\_agent\_env.sh && ssh-add ....

Or you can guess and set the environment vars yourself, the socket  
file is normally in /tmp and owned by your user (i,.e  
/tmp/ssh-7Mk71cc78Qwb/agent.4567) where the last number is the agent's  
pid before forking, normally you can add +1 to get actual agent pid  
and set SSH\_AGENT\_PID=4568  
and SSH\_AUTH\_SOCK=/tmp/ssh-7Mk71cc78Qwb/agent.4567. You can also use  
pgrep to confirm.

---

<div class="post-metadata">

**Author:** ![Eugene\_Romero](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@Eugene\_Romero](https://forum.ansible.com/u/Eugene_Romero)\
**Post date:** [July 17, 2015, 3:33pm UTC](https://forum.ansible.com/t/adding-private-key-to-remote-host/18789/3 "2015-07-17T15:33:37Z")

</div>

Makes sense. As a workaround, and since this is a very specific case, I have decided to explicitly add the key’s path to the only command that depends on it (an rsync job). But this info will come in handy if in the future we need to start working with more private keys.

Thanks for the useful info!

---

<div class="post-metadata">

**Author:** ![Paul\_Markham](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@Paul\_Markham](https://forum.ansible.com/u/Paul_Markham)\
**Post date:** [July 19, 2015, 3:53am UTC](https://forum.ansible.com/t/adding-private-key-to-remote-host/18789/4 "2015-07-19T03:53:30Z")

</div>

You can also use your .ssh/config file to specify one or more keys to use for a target host. Have a look at the IdentityFile option in the ssh\_config man page.
