New to Ansible - Facing issue with ping to target host

Ansible Control Machine (ip = 1.1.1.1):

NAME=“Ubuntu”

VERSION=“18.04.3 LTS (Bionic Beaver)”

Target Node (ip = 2.2.2.2):

NAME=“Amazon Linux”

VERSION=“2”

I am able to ssh from Ansible Control Machine to Target node if I manually type the SSH commands (Which means the keys from Ansible Control machine are properly copied to Target node already)

ubuntu@ip-1.1.1.1:~/ansible$ ssh ec2-user@2.2.2.2

Last login: Mon Mar 16 02:13:42 2020 from ip-1.1.1.1.us-west-2.compute.internal

__| _| )

_| ( / Amazon Linux 2 AMI

|_|__|

https://aws.amazon.com/amazon-linux-2/

[ec2-user@ip-2.2.2.2 ~]$

But when I run the ansible command to ping target node, I get error:

ubuntu@ip-1.1.1.1:~/ansible$ ansible -i hosts -m ping all -vvv

ansible 2.5.1

config file = /home/ubuntu/ansible/ansible.cfg

configured module search path = [u’/home/ubuntu/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]

ansible python module location = /usr/lib/python2.7/dist-packages/ansible

executable location = /usr/bin/ansible

python version = 2.7.17 (default, Nov 7 2019, 10:07:09) [GCC 7.4.0]

Using /home/ubuntu/ansible/ansible.cfg as config file

Parsed /home/ubuntu/ansible/hosts inventory source with ini plugin

META: ran handlers

Using module file /usr/lib/python2.7/dist-packages/ansible/modules/system/ping.py

<1.1.1.1> ESTABLISH SSH CONNECTION FOR USER: ubuntu

<1.1.1.1> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/297a4ed557 1.1.1.1 ‘/bin/sh -c ‘"’“‘echo ~ && sleep 0’”’"‘’

<1.1.1.1> (255, ‘’, ‘ubuntu@1.1.1.1: Permission denied (publickey).\r\n’)

1.1.1.1 | UNREACHABLE! => {

“changed”: false,

“msg”: “Failed to connect to the host via ssh: ubuntu@1.1.1.1: Permission denied (publickey).\r\n”,

“unreachable”: true

}

“hosts” file

[all]

2.2.2.2

"ansible.cfg: file

[default]

inventory = hosts

ansible.cfg (END)

Please suggest how to resolve the issue.

You're able to connect to "ec2-user@2.2.2.2". The same "REMOTE_USER" must be
specified in Ansible. For example

  $ ansible -i hosts -m ping -vvv -u ec2-user all

See

"Hosts and Users" (remote_user)
https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#hosts-and-users

"Adding variables to inventory" (ansible_user)
https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#adding-variables-to-inventory

"Ansible remote_user vs ansible_user"
https://stackoverflow.com/questions/36668756/ansible-remote-user-vs-ansible-user

HTH,

  -vlado