Why does ‘ansible -a “env”‘ return a different environment PATH than the user

ansibleenvironment-variablespath

When I ssh into a remote server and run env I get back the following PATH:

PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/myusername/bin

In contrast, when I execute the ansible command ansible -a "env", I get the following PATH:

PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin

As can be imagined, this causes problems when trying to run
sbin commands like service, ntpdate, etc. because it requires typing out the full path to the sbin command. (Yes, I know there are ansible modules that can do this, but I'm trying to solve why the PATH is getting shortened/truncated.)

Anyone know why this is happening?

EDIT: The reason I'm asking about this is that I'm following the examples in the book Ansible for DevOps and some of the examples aren't working because of this reason.

The example I'm trying to get to work is from page 28 in the book:

$ ansible multi -s -a "service ntpd stop"
$ ansible multi -s -a "ntpdate -q 0.rhel.pool.ntp.org"
$ ansible multi -s -a "service ntpd start"

These commands always raise an error. But if I change them to include the full path, they work.

$ ansible multi -s -a "/sbin/service ntpd stop"
$ ansible multi -s -a "/usr/sbin/ntpdate -q 0.rhel.pool.ntp.org"
$ ansible multi -s -a "/sbin/service ntpd start"

Naturally, I don't want to have to type in the full path for each and every ad-hoc command I issue. (I don't know the paths of every command.)

Is there any way to get the normal path that's in my shell included with ansible? Is there a path variable that I can add to the inventory file or ansible.cfg file that will enable this to happen?

Best Answer

Any time i used ansible it ran as a different user..under a different shell. should be able to see what user by doing:

ansible -a "id"

you can then see the user shell defined in: cat /etc/passwd

Best practice to add to the path would be by adding a line in: /home/<username>/.profile or ~/.bash_profile