Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible SSH connection fail

I'm trying to run ansible role on multiple servers, but i get an error:

fatal: [192.168.0.10]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}

My /etc/ansible/hosts file looks like this:

192.168.0.10 ansible_sudo_pass='passphrase' ansible_ssh_user=user
192.168.0.11 ansible_sudo_pass='passphrase' ansible_ssh_user=user
192.168.0.12 ansible_sudo_pass='passphrase' ansible_ssh_user=user

I have no idea what's going on - everything looks fine - I can login via SSH, but ansible ping returns the same error.

The log from verbose execution:

<192.168.0.10> ESTABLISH SSH CONNECTION FOR USER: user <192.168.0.10> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=user -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r 192.168.0.10 '/bin/sh -c '"'"'( umask 22 && mkdir -p "echo $HOME/.ansible/tmp/ansible-tmp-1463151813.31-156630225033829" && echo "echo $HOME/.ansible/tmp/ansible-tmp-1463151813.31-156630225033829" )'"'"''

Can you help me somehow? If I have to use ansible in local mode (-c local), then it's useless.

I've tried to delete ansible_sudo_pass and ansible_ssh_user, but it did'nt help.

like image 827
Thomas Avatar asked May 13 '16 15:05

Thomas


People also ask

What is SSH connection in Ansible?

This connection plugin allows Ansible to communicate to the target machines through normal SSH command line. Ansible does not expose a channel to allow communication between the user and the SSH process to accept a password manually to decrypt an SSH key when using this connection plugin (which is the default).

Does Ansible work over SSH?

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.

How do I fix connection refused Error in Linux?

Install an SSH tool such as OpenSSH on the server you want to connect to using the sudo apt install openssh-server command. If your firewall is blocking your SSH connection. Disable the firewall rules blocking your SSH connection by changing the destination port's settings to ACCEPT.

How do I check my ping in Ansible?

The simplest way to run the Ansible ping module is to run a simple AD HOC command in the terminal. The above command starts by calling Ansible, followed by the specific pattern of the host. In this case, we want to ping 'all' hosts. The next part, '-m,' specifies the module that we want to use.


3 Answers

You need to change the ansible_ssh_pass as well or ssh key, for example I am using this in my inventory file:

192.168.33.100 ansible_ssh_pass=vagrant ansible_ssh_user=vagrant

After that I can connect to the remote host:

ansible all -i tests -m ping

With the following result:

192.168.33.100 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Hope that help you.

EDIT: ansible_ssh_pass & ansible_ssh_user don't work in the latest version of Ansible. It has changed to ansible_user & ansible_pass

like image 53
Arbab Nazar Avatar answered Oct 16 '22 07:10

Arbab Nazar


mkdir /etc/ansible
cat > hosts
default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key

Go to your playbook directory and run ansible all -m ping or ansible ping -m "server-group-name"

like image 28
SJP Avatar answered Oct 16 '22 07:10

SJP


I had this issue, but it was for a different reason than was documented in other answers. My host that I was trying to deploy to was only available by going through a jump box. Originally, I thought that it was because Ansible wasn't recognizing my SSH config file, but it was. The solution for me was to make sure that the user that was present in the SSH config file matched the user in the Ansible playbook. That resolved the issue for me.

like image 38
entpnerd Avatar answered Oct 16 '22 08:10

entpnerd