Ansible using root user to ping

Hi,

I am new & started learning ansible. Installed and configured ansible on RHEL 9 EC2 instance. I have defined two instances in the hosts file and trying to ping them but I am getting an error. Seems ansible is using root as a user. How can I try to fix it ?

Just to add, I have also setup password less authentication for this by creating another user and made all possible settings in the sshd_config file.

Thanks,
Piyush

1 Like

Hi Piyush,
Ansible uses the user you have configured (see ansible.builtin.ssh connection – connect via SSH client binary — Ansible Documentation), falling back to what the SSH client chooses when you don’t supply a username.
Also please note that the ping module doesn’t do a regular ping, but instead tries to connect to the host and runs a tiny module there.
Cheers, Felix

3 Likes

to add to what @felixfontein is saying, you are using ad-hoc commands here (not an Ansible Playbook), so it is using your “current user” which you can see from the screenshot is root and has the #. You can set an ansible_user in your inventory or use the -u flag on the command line to set the user

4 Likes

And adding to the above by @felixfontein and @IPvSean, also remember that RHEL 9 disabled remote root access through SSH by default.

The recommended way is to create a dedicated Ansible user in the remote node and use that user as mentioned above. You might also allow additional permissions through sudo for that user and use the Ansible become option in your playbooks if required.

Although it’s not recommended, if you absolutely need remote root access through SSH, make sure you check the following Red Hat article for enabling it in RHEL 9.

2 Likes

Thanks all, it worked !

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.