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
asked 21 days ago167 views
2 Answers
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
EXPERT
GK
answered 21 days ago
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
EXPERT
Steve_M
answered 21 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions