Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: Permission denied (publickey, password)

Tags:

ssh

ansible

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.

like image 355
Héctor Avatar asked Dec 02 '22 13:12

Héctor


2 Answers

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.

like image 100
Sapikelio Avatar answered Dec 05 '22 12:12

Sapikelio


@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

like image 20
Bernardo Vale Avatar answered Dec 05 '22 13:12

Bernardo Vale