# Accessing via SSH in Ansible

**URL:** <https://forum.ansible.com/t/accessing-via-ssh-in-ansible/21645>\
**Category:** Ansible Project\
**Created:** [June 2, 2016, 3:35pm UTC](https://forum.ansible.com/t/accessing-via-ssh-in-ansible/21645 "2016-06-02T15:35:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Hanson](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@Tom\_Hanson](https://forum.ansible.com/u/Tom_Hanson)\
**Post date:** [June 2, 2016, 3:35pm UTC](https://forum.ansible.com/t/accessing-via-ssh-in-ansible/21645/1 "2016-06-02T15:35:25Z")

</div>

I am fairly new working with ansible and am operating some playbooks that were built by someone else who is gone. The current playbooks will set up a new server and work fine. It installs several standard packages and will change to all SSH logins , no root login. The first time I will put the ansible\_ssh\_user=root ansible\_ssh\_pass= xxxxx on the inventory line. After that root access no longer works.

the problem is when I need to run a playbook to update the server. I get a failure that looks like this :

_ipaddress_\> ESTABLISH CONNECTION FOR USER: _myusername_  
\<_ipaddress_\> REMOTE\_MODULE setup  
\<_ipaddress_\> EXEC sshpass -d7 ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath=“/home/nnnn/.ansible/cp/ansible-ssh-%h-%p-%r” -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no -o PubkeyAuthentication=no -o ConnectTimeout=10 _ipaddress_ /bin/sh -c ‘mkdir -p $HOME/.ansible/tmp/ansible-tmp-1464038963.41-214242419831580 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1464038963.41-214242419831580 && echo $HOME/.ansible/tmp/ansible-tmp-1464038963.41-214242419831580’  
fatal: [server] =\> SSH Error: Permission denied (publickey).  
while connecting to _ipaddress_:22

I then discovered a small group of servers that use a VPN and back end IP , and the same script ran OK -

\<_ipaddress_\> ESTABLISH CONNECTION FOR USER: _myusername_  
\<_ipaddress_\> REMOTE\_MODULE setup  
\<_ipaddress_\> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath=“/home/_myusername_/.ansible/cp/ansible-ssh-%h-%p-%r” -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 _ipaddress_ /bin/sh -c ‘mkdir -p $HOME/.ansible/tmp/ansible-tmp-1464814366.77-239302668766602 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1464814366.77-239302668766602 && echo $HOME/.ansible/tmp/ansible-tmp-1464814366.77-239302668766602’  
\<_ipaddress_\> PUT /tmp/tmpzk8i1E TO /home/_myusername_/.ansible/tmp/ansible-tmp-1464814366.77-239302668766602/setup  
\<_ipaddress_\> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath=“/home/_myusername_/.ansible/cp/ansible-ssh-%h-%p-%r” -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 _ipaddress_ /bin/sh -c ‘sudo -k && sudo -H -S -p “[sudo via ansible, key=jreeatvlyrwzcjqqsmbdciudhpgwnrxp] password: " -u root /bin/sh -c '”’“‘echo BECOME-SUCCESS-jreeatvlyrwzcjqqsmbdciudhpgwnrxp; LANG=en\_US.UTF-8 LC\_CTYPE=en\_US.UTF-8 /usr/bin/python /home/_myusername_/.ansible/tmp/ansible-tmp-1464814366.77-239302668766602/setup; rm -rf /home/_myusername_/.ansible/tmp/ansible-tmp-1464814366.77-239302668766602/ \>/dev/null 2\>&1’”‘"’’  
ok: [_servername_]

I am hoping there is a simple setting or other change that will allow these playbooks to run, thanks for the help. I put asterisks around some things for privacy / security _myusername_ , etc.

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [June 2, 2016, 7:02pm UTC](https://forum.ansible.com/t/accessing-via-ssh-in-ansible/21645/2 "2016-06-02T19:02:26Z")

</div>

Hi there,

> I am fairly new working with ansible and am operating some playbooks that  
> were built by someone else who is gone. The current playbooks will set up a  
> new server and work fine. It installs several standard packages and will  
> change to all SSH logins , no root login. The first time I will put the  
> &nbsp;&nbsp;ansible\_ssh\_user=root ansible\_ssh\_pass= xxxxx on the inventory line.

Keeping that in a ansible-vault encrypted file is the better option,  
but first things first.

> After that root access no longer works.

I guess your playbook changes the sshd's settings, right?

> the problem is when I need to run a playbook to update the server.

The same playbook run again? Or another playbook? Could you share the  
parts of the playbook that show the errors?

> I get a  
> failure that looks like this :
> 
> \*ipaddress\*\> ESTABLISH CONNECTION FOR USER: \*myusername\*  
> \<\*ipaddress\*\> REMOTE\_MODULE setup  
> \<\*ipaddress\*\> EXEC sshpass -d7 ssh -C -tt -v -o ControlMaster=auto -o  
> ControlPersist=60s -o  
> ControlPath="/home/nnnn/.ansible/cp/ansible-ssh-%h-%p-%r" -o  
> StrictHostKeyChecking=no -o GSSAPIAuthentication=no -o  
> PubkeyAuthentication=no -o ConnectTimeout=10 \*ipaddress\* /bin/sh -c 'mkdir  
> -p $HOME/.ansible/tmp/ansible-tmp-1464038963.41-214242419831580 && chmod  
> a+rx $HOME/.ansible/tmp/ansible-tmp-1464038963.41-214242419831580 && echo  
> $HOME/.ansible/tmp/ansible-tmp-1464038963.41-214242419831580'  
> fatal: [server] =\> SSH Error: Permission denied (publickey).  
> &nbsp;&nbsp;&nbsp;&nbsp;while connecting to \*ipaddress\*:22

Can you login as the destination user if you do it manually? Do you  
have the right ssh keys on your machine?

Johannes

---

<div class="post-metadata">

**Author:** ![Tom\_Hanson](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@Tom\_Hanson](https://forum.ansible.com/u/Tom_Hanson)\
**Post date:** [June 2, 2016, 8:05pm UTC](https://forum.ansible.com/t/accessing-via-ssh-in-ansible/21645/3 "2016-06-02T20:05:35Z")

</div>

####The same playbook run again? Or another playbook? Could you share the  
parts of the playbook that show the errors?

different playbook - this is one to just update the hosts file,

ansible-playbook -vvv update\_hosts.yml -i inventory/inventory  
file update\_hosts.yml

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [June 3, 2016, 7:12am UTC](https://forum.ansible.com/t/accessing-via-ssh-in-ansible/21645/4 "2016-06-03T07:12:51Z")

</div>

[maybe snip the fullquote the next time, makes mails very long...]

> yes, My key works on the servers after they are configured

My guess: Connecting as root the first time works, as you tell ansible  
with which user it should connect. After that, ansible is missing this  
info. And I guess the target username is not the one you have on your  
controller.

What happens if you include the following into your update.yml:

vars:  
&nbsp;&nbsp;ansible\_user: foobar

And replace foobar with the user on the remote machine.

If that does not solve it, then maybe the VPN is the reason, and ssh  
login is only allowed on some IPs or from some IPs. That would be a  
task in the first ansible playbook, maybe have a look.

Johannes

---

<div class="post-metadata">

**Author:** ![Tom\_Hanson](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@Tom\_Hanson](https://forum.ansible.com/u/Tom_Hanson)\
**Post date:** [June 3, 2016, 8:54pm UTC](https://forum.ansible.com/t/accessing-via-ssh-in-ansible/21645/5 "2016-06-03T20:54:47Z")

</div>

What happens if you include the following into your update.yml:

vars:  
ansible\_user: foobar

that seems to have done the trick, thanks for the help

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [June 4, 2016, 9:15am UTC](https://forum.ansible.com/t/accessing-via-ssh-in-ansible/21645/6 "2016-06-04T09:15:36Z")

</div>

I guess when you tried to log into the machine manually, you specified  
a user:  
ssh user@machine  
or  
ssh user@ip-address

So your ssh-client knew as which user to connect. Ansible did not know  
that. And you told it only in your first step with the  
ansible\_user=root part.

You might want to read up on ssh connections.  
[https://docs.ansible.com/ansible/intro\_getting\_started.html#remote-connection-information](https://docs.ansible.com/ansible/intro_getting_started.html#remote-connection-information)

Johannes
