I want to connect ansible to EC2 via ssh. I created a key pair on my local machine(ubuntu) and copied it to my EC2 instances using ssh-copy-id. The copy was successful, but I get the error below when I want to connect to my instance:
Failed to connect to the host via ssh: [email protected]: Permission denied (publickey)
when I inspected the auth.log file on my remote machine I see the following logs:
invalid user usuario from 187.xx.xx.xx port 47988
Here is my etc/ansible/hosts file
[host1]
github ansible_host=13.xxx.xxx.xx ansible_user=root
Any help will be appreciated. Thank you
In general we should use domain credentials not the key based authentication to run playbook from control node to any of you remote server.
But in your case you are trying to authenticate using root user and the key. So follow the below steps:
“/home/ansible/.ssh/authorized_keys”
which you want to manage via ansible control node.Now check the SSH using below command
ssh -i /path/to/private-key ansible@<remoteserverip>
Hope this helps.
Regarding ansible configuration there will one parameter in
/etc/ansible/ansible.cfg
about mentioning private.private_key_file = /path/to/private-key
I needed to set PasswordAuthentication yes in sshd_config file on the remote server and that resolved the problem