2

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: root@34.xxx.0x.xxx: 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

waltinator
  • 37,856

2 Answers2

0

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:

  • Create an user named as ansible & Create key at control node ( ansible master server) for user “ansible”.
  • If you created keys for this user will get two files private-key & public-key.
  • Copy the public key content into of remote servers “/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

0

I needed to set PasswordAuthentication yes in sshd_config file on the remote server and that resolved the problem