Ansible: Permission denied (publickey, password)

2019-05-28 01:06发布

问题:

I'm not able to connect to a host in Ansible. This is the error:

192.168.1.12 | UNREACHABLE! => { "changed": false, "msg": "ERROR! SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue", "unreachable": true }

This is my hosts file:

[test]
192.168.1.12

And this is the ad-hoc instruction:

ansible all -m ping

I'm able to connect via raw ssh.

回答1:

By default Ansible try to use SSH keys. It seems that you have wrong keys. Try to use Password authentication.

ansible all -m ping --ask-pass --ask-sudo-pass

I Hope it helps.



回答2:

@bigdestroyer, to setup ssh public keys use this playbook

- hosts: all
  remote_user: root
  vars:
    authorized_key_list:
      - name: root
        authorized_keys:
         - key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
           state: present
  roles:
    - { role: GROG.authorized-key }

Execute this playbook with --ask-pass since you'll use it to setup public key authentication.

ansible-playbook setup_ssh.yml --ask-pass

This role will add your current user public key to remote host authorized_keys file.

NOTE

ask-pass works only one time per run so this will only work with hosts that has the same password.

I usually use -limit and execute in batches on hosts that has the same password.

For example, let's assume host1,host2 and host3 has password foo host4 and host5 bar

ansible-playbook setup-ssh.yml --ask-pass -l host1,host2,host3

provide password foo

ansible-playbook setup-ssh.yml --ask-pass -l host4,host5

provide password bar

THEN

ansible -m ping host1,host2,host3,host4,host5

You can read the role documentation here



回答3:

For those that come here running Ansible 2.6, --ask-sudo-pass is now deprecated. The correct syntax is:

ansible all -m ping --ask-pass --ask-become-pass


回答4:

I encountered this issue - my ssh keys weren't set up correctly. I fixed this using the following:

  • Make sure each machine has an ssh keys set up, using the ssh-keygen command.

    ssh-keygen

  • Pass your public key over to the machine, using the ssh-copy-id command.

    ssh-copy-id -i <location of id_rsa.pub> <ip-address of host>

This helped resolve my error, hopefully it helps!



回答5:

I resolved this issue by adding --ask-pass argument



标签: ssh ansible