Ansible Failed to connect to the host via SSH error

0

Hi AWS, I have created a t2.micro EC2 instance to play around with Ansible. I have created the SSH public key using ssh-keygen -t rsa on my local machine and then copied the public SSH key inside the authorized_keys under .ssh folder.

Now while I am running the playbook using command ansible-playbook playbook.yml, I am getting this error below:

PLAY [Set up NTP on all servers] **********************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
fatal: [18.212.22.48]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: arjungoel1995@18.212.22.48: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).", "unreachable": true}

PLAY RECAP ********************************************************************************************************************************************************************************************************
18.212.22.48               : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0

I also tried to run the ansible adhoc command i.e. ansible all -m ping and I am still getting the same error i.e.

18.212.22.48 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: arjungoel1995@18.212.22.48: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).",
    "unreachable": true
}

I have tried several alternatives using Stackoverflow and others but nothing worked out. Could you please help me out?

profile picture
已提問 1 個月前檢視次數 207 次
2 個答案
0

Hi

The error message "Permission denied (publickey)" indicates that Ansible is unable to connect to your EC2 instance using SSH key-based authentication. Here are some troubleshooting steps you can try to fix the issue

Refer to this article: https://repost.aws/knowledge-center/ec2-linux-fix-permission-denied-errors

  1. Manual Connection: a manual SSH test is a great suggestion to diagnose the connection issue you're facing with Ansible.

Run the following command, replacing <your_pem_file> with the path to your private key file (e.g., ~/.ssh/id_rsa) and <public_dns> with the actual public DNS address of your EC2 instance: ssh -i <your_pem_file> arjungoel1995@<public_dns>

If the connection is successful, you'll be logged into your EC2 instance. This suggests the issue might be with Ansible configuration.

  1. SSH Authentication:
  • Verify SG group have port open
  • Verify that key in your local have permission like 400 or 600 chmod 600 ssh_key

Suggestions: : Run ansible-playbook playbook.yml -vvv to get more detailed output in the case of errors Lastly: If you're stuck, you can temporarily allow password authentication for SSH on your EC2 instance (not recommended for production due to security risks). This will help you verify if the issue lies with key-based authentication.

profile picture
專家
GK
已回答 1 個月前
0

Does the user arjungoel1995 exist on the EC2 instance (I'm guessing that's the host 18.212.22.48 ?).

If it's a different username on the target, e.g. the default ec2-user then you need to add -u ec2-user to the ansible-playbook command.

If the usernames are all correct then troubelshoot this as an SSH problem (because that's what it is) and not an Ansible problem. Once you can do passwordless SSH from your local machine to the EC2 instance then it should work with Ansible as well.

profile picture
專家
Steve_M
已回答 1 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南