32
votes

I am new to Ansible and I am trying to implement it. I tried all the possible ways present on the Internet and also all questions related to it, but still I can't resolve the error. How can I fix it?

I installed Ansible playbook on my MacBook Pro. I created a VM whose IP address is 10.4.1.141 and host IP address is 10.4.1.140.

I tried to connect to my VM using the host via SSH. It connected by the following command:

ssh [email protected]

And I got the shell access. This means my SSH connection is working fine.

Now I tried the following command for Ansible:

ansible all -m ping

And the content in the /etc/ansible/host is 10.4.1.141.

Then it shows the following error:

10.4.1.141 | FAILED => SSH Error: Permission denied (publickey,password).

while connecting to 10.4.1.141:22

It is sometimes useful to rerun the command using -vvvv, which prints SSH debug output to help diagnose the issue.

Then I tried creating the config file in .ssh/ folder on the host machine, but the error is still the same.

The content of the config file is:

  IdentityFile ~/.ssh/id_rsa

which is the path to my private key.

Then I ran the same command ansible all -m ping and got the same error again.

When I tried another command,

ansible all -m ping -u user --ask-pass

Then it asked for the SSH password. I gave it (I am very sure the password is correct), but I got this error:

10.4.1.141 | FAILED => FAILED: Authentication failed.

This is the log using -vvvv:

<10.4.1.141> ESTABLISH CONNECTION FOR USER: rajatg
<10.4.1.141> REMOTE_MODULE ping
<10.4.1.141> EXEC ssh -C -tt -vvv -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/Users/rajatg/.ansible/cp/ansible-ssh-%h-%p-%r" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 10.4.1.141 /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1445512455.7-116096114788007 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1445512455.7-116096114788007 && echo $HOME/.ansible/tmp/ansible-tmp-1445512455.7-116096114788007'
10.4.1.141 | FAILED => SSH Error: Permission denied (publickey,password).
    while connecting to 10.4.1.141:22
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.

I am still not able to figure it out what the problem is. It is my last choice to ask it here after doing my all research. This is the link I referred to.

10
show us how does your /etc/ansible/hosts file looks like .frank
I mentioned it in the description. There is only a single line 10.4.1.141 in /etc/ansible/hostsRajat Gupta
Try running Ansible with verbosity -vvvvv and then editing the result of that back into your question. I'd guess it's using the wrong username but it's hard to say without the extra debug info.ydaetskcoR
I added log in the description.Rajat Gupta
what are the privileges of ssh config? If they will be too open, you will have access denied (chmod should be 600 on ssh)m.aibin

10 Answers

38
votes

I fixed the issue. The problem was in my /etc/ansible/hosts file.

The content written in /etc/ansible/hosts was 10.4.1.141. But when I changed it to [email protected], then the issue got fixed.

10
votes

If you log in with ssh [email protected]:

Option 1

Then make sure that in your hosts file inside etc\ansible you have:

[server01]
10.4.1.141

Then within etc\ansible run:

ansible all -m ping -u user --ask-pass

Option 2

If you want to log in without typing the SSH password then in your hosts file inside etc\ansible you add:

[server01]
10.4.1.141 ansible_ssh_pass=xxx ansible_ssh_user=user

Then within etc\ansible run:

ansible all -m ping

For me it worked both ways.

2
votes

If you execute Ansible with sudo, for example

sudo ansible -m ping all

Please keep in mind that the public key for root has to be on the server you want to reach as well, not only the public key from your non-root-user. Otherwise, you get the error message above as well.

2
votes

Most of the issues happen while connecting Ubuntu machines in hosts.

Solution Ansible required which user want to connect, because Ubuntu doesn't have a default root user.

For the hosts file

[Test-Web-Server]

10.192.168.10 ansible_ssh_pass=foo ansible_ssh_user=foo

2
votes

The previous solutions didn't work for me, unfortunately (DevOps layman here!).

But the below one worked for me.

Change your inventory file to:

[webserver] 10.4.1.141 ansible_user=ubuntu

ansible webserver --private-key pem_file.pem -m ping

Hitting the command with -vvvv helped me to debug it more.

Reference: Failed to connect to the host via ssh: Permission denied (publickey,password) #19584

1
votes

I have fixed this issue as well.

My issue was also in my hosts file, /etc/ansible/hosts.

I changed my hosts file from

172.28.2.101

to

name-of-server-in-ssh-config

I had IP addresses in the hosts file. Since I have SSH configurations already set up for names, I do not need to use a variable or username in front of the hosts.

[name-stg-web]
server-name-stg-web[01:02]
1
votes

The problem lies in the inventory file.

vi /etc/ansible/hosts

It should be:

[webserver]
192.###.###.### ansible_ssh_user=user ansible_ssh_pass=pass
1
votes

My case is I have multiple private keys in my .ssh.

Here is how I fix it by telling ansible to use a certain private key

ansible-playbook -i ../../inventory.ini --private-key=~/.ssh/id_rsa_ansiadmin update.yml 
0
votes

Mentioning the username in /etc/hosts file also can resolve the issue.

#sudo vim /etc/hosts
[test-server]
ip_address ansible_user="remote pc's username" 
0
votes

What first worked for me was to hardcode the target machine root's password in the /etc/ansible/hosts like this:

[load_balancers_front]
loadbalancer1 ansible_host=xxx.xxx.xxx.xxx ansible_user=root ansible_password=root_password_in_target

But it is not recommended to do this of course because of security issues.

Then, I figured out a solutions from the docs by doing:

ssh-agent bash --> read here

and then

ssh-add /my/private/ssh-key

After this, my hosts file looks like this and ansible all -m ping works fine:

[load_balancers_front]
loadbalancer1 ansible_host=xxx.xxx.xxx.xxx ansible_user=root