Failed to connect to the host...permission denied (publickey, password) unreachable
Asked Answered
E

6

6

I'm finding it difficult to run a simple playbook. I already ping target and it was successful. When i run the playbook i get this error:

PLAY [install httpd and start services] ***********************************

TASK [Gathering Facts] ****************************************************
fatal:[192.168.112.66]: UNREACHABLE!=> {"changed": false "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey password)." "unreachable": true}

What's the problem with this?

Empiricism answered 31/3, 2022 at 14:19 Comment(3)
One issue could be that the ssh private key which is present already can't be access by the user from which ansible playbook is run. Make sure that the ansible user configured in ansble.cfg or the host file (with ansible_ssh_private_key_file defined) has permission to access user jay 's ssh key.Olds
Try adding -vvv in the ansible command to get the detailed logsOlds
now i get a different error, saying: failed to connect to the host via ssh: could not resolve hostname target: Temporary failure in name resolution", "unreachable: true"Empiricism
S
4

See my answer here.

Basically, append the content of id_rsa.pub file, into the authorized_keys file.

Seaver answered 10/4, 2023 at 14:4 Comment(1)
Wondering why I got "-1". Appreciate if someone letting me know what was wrong with the solution. It worked for me and I share it to help others.Seaver
T
2

The remote server is denying you the access due your key has a password. Try this before run the playbook:

$ eval `ssh-agent`
$ ssh-add /path/to/your/private/key

Then run the playbook with the options -u and --private-key pointing to the user with access permissions on remote server and the private key you use.

Thalassic answered 31/3, 2022 at 14:39 Comment(8)
i tried everything and now i'm getting a different error message. something like this: {"changed"; false, "msg": invalid/incorrect password: Permission denied please try again." "unreachable": true}Empiricism
Ensure your target server allows ssh login with keys, not just passwords. If it's asking for password maybe you're not allowed to login with ssh keysThalassic
If that should be the case then please how do i ensure my target server allows ssh login with keys instead of passwords?Empiricism
@ Victor - I added the SSH keys to the remote server using; ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] and still yet if i try to run my playbook it shows permission denied (publickey password). why same error?Empiricism
Can you login to remote server only with your key? Your key has a passphrase? Review options 'PubkeyAuthentication' on sshd config of your remote serverThalassic
@ Victor, i'm new to using Ansible, i did not use password to login to the remote server since i already have a sshkey. And on sshd config i set 'PubkeyAuthentication' is no. Is that so?Empiricism
yeah, that's it! you should allow public key authenticationThalassic
finally i got the answer and it worked perfectly. i changed the playbook file permission to execute. chmod +x (playbook.yml)Empiricism
P
2

I am guessing you used a password instead of ssh-key. So at the end of your command, add

--ask-pass

Let's say you're running your playbook. Your command will become:

ansible-playbook playbook.yml --ask-pass
Papal answered 16/11, 2022 at 10:8 Comment(0)
N
1

None of the answers given above solved the issue for me, so I have to come up with something else. The following steps helped me to run an ansible playbook on a remote host:

  1. Generate a ssh key pair using ssh-keygen

  2. Add the generated public key using ssh-copy-id or manually.

  3. Create an inventory file at /etc/ansible/hosts and configure the remote host:

     [testservers]
     <remote-host-ip-address> ansible_user=<user-name> ansible_ssh_private_key_file=<absolute-path-to-your-public-ssh-key>
    
  4. Run your ansible playbook

Nepil answered 22/3 at 8:38 Comment(0)
H
0

This worked for me

ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote-host

Halogenate answered 23/9, 2023 at 1:54 Comment(0)
F
0

In my case I copied the keys the wrong direction. What needs to happen is the ansible master node needs to copy it's key over to the target nodes, like this:

Generate the master node ssh key

ssh-keygen

then copy it to target nodes, assuming you have the password:

ssh-copy-id demo@IP_address

then ansible ping:

@ansible-master:~$ ansible TARGET-IP-ADDRESS -m ping -u USER -vvv

Fenelia answered 27/6 at 22:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.