# getting error when i set ssh-agent for root user

**URL:** <https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685>\
**Category:** Ansible Project\
**Created:** [September 29, 2018, 11:05am UTC](https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685 "2018-09-29T11:05:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![pranay\_varankar](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pranay\_varankar](https://forum.ansible.com/u/pranay_varankar)\
**Post date:** [September 29, 2018, 11:05am UTC](https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685/1 "2018-09-29T11:05:30Z")

</div>

Hi,

I need to add ssh-agent for root user. so I use below command through ansible task but getting an error.

**eval `ssh-agent -s` && ssh-add ~/.ssh/id\_rsa"**

please give me task to perform below command on my target machine.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Lozada\_De\_L](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@Jonathan\_Lozada\_De\_L](https://forum.ansible.com/u/Jonathan_Lozada_De_L)\
**Post date:** [September 29, 2018, 7:33pm UTC](https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685/2 "2018-09-29T19:33:19Z")

</div>

what error are you getting ?

---

<div class="post-metadata">

**Author:** ![pranay\_varankar](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pranay\_varankar](https://forum.ansible.com/u/pranay_varankar)\
**Post date:** [September 29, 2018, 9:03pm UTC](https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685/3 "2018-09-29T21:03:56Z")

</div>

Hi Jonathan,

actually i m not getting error when i run playbook but after i will login to client machine and check using “ssh-add -l” command then i get “Could not open a connection to your authentication agent” error.

Can you send me task code to perform this operation.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Lozada\_De\_L](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@Jonathan\_Lozada\_De\_L](https://forum.ansible.com/u/Jonathan_Lozada_De_L)\
**Post date:** [September 29, 2018, 9:23pm UTC](https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685/4 "2018-09-29T21:23:25Z")

</div>

try

ssh-agent bash

then

ssh-add

---

<div class="post-metadata">

**Author:** ![pranay\_varankar](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pranay\_varankar](https://forum.ansible.com/u/pranay_varankar)\
**Post date:** [September 29, 2018, 10:02pm UTC](https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685/5 "2018-09-29T22:02:48Z")

</div>

Hi Jonathan,

But which module I use to run **eval `ssh-agent -s` or ssh-add /root/bashrc.**

**Shell/command/action/raw module to create task for above command**

---

<div class="post-metadata">

**Author:** ![Jonathan\_Lozada\_De\_L](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@Jonathan\_Lozada\_De\_L](https://forum.ansible.com/u/Jonathan_Lozada_De_L)\
**Post date:** [September 29, 2018, 11:41pm UTC](https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685/6 "2018-09-29T23:41:03Z")

</div>

what exactly are you trying to accomplish?

---

<div class="post-metadata">

**Author:** ![pranay\_varankar](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pranay\_varankar](https://forum.ansible.com/u/pranay_varankar)\
**Post date:** [September 30, 2018, 10:52am UTC](https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685/7 "2018-09-30T10:52:18Z")

</div>

Hi Jonathan,

I use below task in my playbook,

- name: Add and load private key to ssh-agent  
shell: “eval `ssh-agent -s` && ssh-add ~/.ssh/id\_rsa”

When i run playbook no error found,

 ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/6/6973903df2d5f27c832066b667bcff5d7f8973f8.png)

But I log in to my client machine and type “ssh-add -l” command then I get below error

![image.png](https://us1.discourse-cdn.com/ansible/original/2X/8/864a674f79414f50a9fa0c09a02b01318d7184b0.png)

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [September 30, 2018, 6:15pm UTC](https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685/8 "2018-09-30T18:15:33Z")

</div>

The “ssh-agent” and “ssh-add” are usually run on a system as a specific user to add an SSH key to a temporary ‘keychain’ so any subsequent SSH commands can automatically (and without prompting) use a specific ssh key to connect to a second system.

When you run them in the Ansible “shell” module, it’s running those on the remote system and once that module completes, the ssh keychain process (“ssh-agent”) is closed and it goes away.

As Johnathan asked, what are you trying to get setup by running these steps? It looks like you’re trying to get it setup so you can SSH into the “ubuntu-target” system as root using the “id\_rsa” key on “ubuntu” . (But since your ansible-playbook command doesn’t prompt for credentials, it appears you already have the shared ssh key setup, so I’m confused what larger problem you’re trying to solve…)

---

<div class="post-metadata">

**Author:** ![pranay\_varankar](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pranay\_varankar](https://forum.ansible.com/u/pranay_varankar)\
**Post date:** [September 30, 2018, 7:25pm UTC](https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685/9 "2018-09-30T19:25:18Z")

</div>

Hi Dan,

Thanks for providing information.

Actually, I have already add key file to client machine.

I have a question …The task i wrote in playbook in correct or not ? if it wrong can you send me the correct task to perform those command on client machine.

- name: Add and load private key to ssh-agent

shell: “eval `ssh-agent -s` && ssh-add ~/.ssh/id\_rsa”

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [September 30, 2018, 8:56pm UTC](https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685/10 "2018-09-30T20:56:36Z")

</div>

Hi Pranay,

You asked, is “task i wrote in playbook in correct or not ?”

Based on the output from your playbook provided earlier, the tasks/commands (“ssh-agent” and “ssh-add”) ARE being executed on the remote machine without error - the yellow “changed:” response notes that the “shell:” module ran and the two commands did not return an error.

Back to the question we asked earlier, what are you trying to get setup by running these steps?

Until you can tell us what you’re trying to do exactly by running these commands, we can’t really help you further. You’ve setup the “shell:” module properly and the commands run without error. Until we know what you’re trying to do later we can’t provide any further assistance.

---

<div class="post-metadata">

**Author:** ![pranay\_varankar](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pranay\_varankar](https://forum.ansible.com/u/pranay_varankar)\
**Post date:** [September 30, 2018, 10:47pm UTC](https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685/11 "2018-09-30T22:47:28Z")

</div>

Hi Dan,

Actually we install some of my on-premises software on remote machine but we required ssh-agent configured on that remote machine to install this software.

without authenticate with that keys we can not able to install this software on my remote machine.

is their any way to check the below task was running properly on my remote machine.

- name: Add and load private key to ssh-agent

shell: “eval `ssh-agent -s` && ssh-add ~/.ssh/id\_rsa”

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [October 1, 2018, 1:37am UTC](https://forum.ansible.com/t/getting-error-when-i-set-ssh-agent-for-root-user/28685/12 "2018-10-01T01:37:43Z")

</div>

If the ssh-agent must be running for your installer to complete, then you’ll probably have to run it after the ssh-add:

- name: Add and load private key to ssh-agent **and start installer**

shell: “eval `ssh-agent -s` && ssh-add ~/.ssh/id\_rsa **&& /path/to/installer** ”

My gut feeling is that there’s something missing that would make your task a lot easier and not need the ssh-agent/ssh-add processes.
