1
votes

I am using ubuntu WSL as a controller for ansible. Trying to install python dependencies in localhost and then launch an ec2 instance But failing due to below error: "msg": "Failed to connect to the host via ssh: kevin@localhost: Permission denied (publickey,password)."

Things I did to fix: 1.Did ssh-keygen 2.Generating public/private rsa key pair. 3.Enter file in which to save the key (/home/user/.ssh/id_rsa): 4.Finally $ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys

Things Worked:

1.ssh localhost is working for kevin@localhost

2.First I use an existing RHEL ec2 instance with pem file,with this i can run my tasks and launch instances

Things Not Working:

--2 Type of errors--

Playbook Task having same issue

  1. failed: [localhost] (item=webserverA) =>

    {"ansible_loop_var": "item", "item": "webserverA", "msg": "Failed to connect to the host via ssh: kevin@localhost: Permission denied (publickey,password).", "unreachable": true} fatal: [localhost]: UNREACHABLE! => {"changed": false, "msg": "All items completed", "results": [{"ansible_loop_var": "item", "item": "webserverA", "msg": "Failed to connect to the host via ssh: kevin@localhost: Permission denied (publickey,password).", "unreachable": true}]}

  2. <127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER:

    kevin <127.0.0.1> EXEC /bin/sh -c 'echo ~kevin && sleep 0' <127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "echo /home/kevin/.ansible/tmp/ansible-tmp-1590382761.4873009-58188090245831" && echo ansible-tmp-1590382761.4873009-58188090245831="echo /home/kevin/.ansible/tmp/ansible-tmp-1590382761.4873009-58188090245831" ) && sleep 0'

    "msg": "Authentication or permission failure. In some cases, you may have been able to authenticate and did not have permissions on the target directory. Consider changing the remote tmp path in ansible.cfg to a path rooted in \"/tmp\". Failed command was: ( umask 77 && mkdir -p \"echo /home/kevin/.ansible/tmp/ansible-tmp-1590382762.0043557-97164890408687\" && echo ansible-tmp-1590382762.0043557-97164890408687=\"echo /home/kevin/.ansible/tmp/ansible-tmp-1590382762.0043557-97164890408687\" ), exited with result 1, stdout output: ansible-tmp-1590382762.0043557-97164890408687=/home/kevin/.ansible/tmp/ansible-tmp-1590382762.0043557-97164890408687\n",

Clarification: 1.In ansible.cfg file which key should be given for private_key_file .pem file or id_rsa My understanding is to ssh localhost we need id_rsa and to ssh ec2 instance we need .pem file So kindly support on to fix issue 1

2.For issue 2 I tried to give remote_tmp path in ansible.cfg and connection=local but not worked

Inventory file:

hosts-dev

[webservers]
localhost ansible_user=kevin

**Play.yml**
- name: Creates the ec2 instances
    hosts: localhost
    gather_facts: no
    roles:
      - python
    tasks:  
      - name: Launch an EC2 Instance
        ec2_instance: 
//Using the access keys here      

Verbose output:

<localhost> ESTABLISH SSH CONNECTION FOR USER: kevin
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=30m -o 'IdentityFile="/home/kevin/.ssh/id_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="kevin"' -o ConnectTimeout=10 -o ControlPath=/home/kevin/.ansible/cp/9faa73ac6e localhost '/bin/sh 
-c '"'"'echo ~kevin && sleep 0'"'"''
<localhost> (255, b'', b'kevin@localhost: Permission denied (publickey,password).\r\n')
<localhost> ESTABLISH SSH CONNECTION FOR USER: kevin
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=30m -o 'IdentityFile="/home/kevin/.ssh/id_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="kevin"' -o ConnectTimeout=10 -o ControlPath=/home/kevin/.ansible/cp/9faa73ac6e localhost '/bin/sh 
-c '"'"'echo ~kevin && sleep 0'"'"''
1

1 Answers

0
votes

The .pem you get from Amazon is a private key as is id_rsa.

Validate what your Ansible should be doing by performing ssh -i ~/.ssh/id_rsa kevin@localhost.

If that works you fundamentally have all the pieces required and should look at your ansible configuration to determine.

If you cannot perform the following validations:

  • Do you have permissions to access the key? It should be chmod 400 with the owner as the user that you run Ansible as.
  • Is that the correct user on that server?
  • Adding -v to the end will provide more verbose debug, you can keep appending another v (i.e. -vv or -vvv) to get even more verbose debug upto 4 vs.

Your ansible.cfg should look like this

[defaults]
private_key_file = /home/kevin/.ssh/id_rsa