Having trouble with Ansible Ad-hoc commands (page 28 of 'Ansible for DevOps' book)

From page 28 in the book “Ansible for DevOps”, the author says to try the following examples:

$ 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"

Unfortunately, they each fail with the message:

myservername | FAILED | rc=127 >>
/bin/sh: service: command not found

Now, if I change the commands 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"

Is the author's example broken? Or am I doing something wrong?

Naturally, I don't want to memorize (or type) the full path of each command that I'm issuing ad-hoc. I think that's the point of ad-hoc commands. It's supposed to be like sitting at the command line of the server(s) in question. Right?

How do I get the PATH included in my environment such that I can get these ad-hoc commands to work without including the full path in each command? Is there a way to set the path in an inventory file or the ansible.cfg file?

Any advice would be appreciated. Thanks!

From page 28 in the book "Ansible for DevOps", the author says to try the
following examples:

$ 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"
Unfortunately, they each fail with the message:

myservername | FAILED | rc=127 >>
/bin/sh: service: command not found

Is the author's example broken? Or am I doing something wrong?

The commands is correct, it's you environment that needs a change.

Naturally, I don't want to memorize (or type) the full path of each
command that I'm issuing ad-hoc. I think that's the point of ad-hoc
commands. It's supposed to be like sitting at the command line of the
server(s) in question. Right?

How do I get the PATH included in my environment such that I can get
these ad-hoc commands to work without including the full path in each
command? Is there a way to set the path in an inventory file or the
ansible.cfg file?

Any advice would be appreciated. Thanks!

Ansible will log in to the host with a user and in your case do a sudo.
This user don't have the correct path set and you will need to write full path until you sort that out.

Is there a reason why Ansible uses /bin/sh? (per the error message above.)

When I SSH into the machine, it launches /bin/bash as my shell. Is there a way to tell Ansible to use /bin/bash instead? (which should then source /etc/profile which in turn sets the full path.)

Just wanted to summarize some of the other discussion that we had recently in the ansible channel:

P Retzel noted that placing the following at the top of his ~/.bashrc file allowed commands to be executed properly:

export PATH=$PATH:/usr/local/sbin:/usr/sbin:/sbin

Note that on most of the servers I’ve provisioned—mostly using the standard minimal base images for CentOS, Debian, or Ubuntu, on different cloud providers or via Vagrant, the $PATH seems to be configured correctly out of the box to include the ‘service’ command and other paths, so I’m guessing something else in the environment on P Retzel’s servers may be custom that caused the path to not work correctly (I believe he mentioned using CentOS 6.8 somewhere).

Just wanted to summarize the info here for the benefit of anyone else coming to this thread :slight_smile:

-Jeff Geerling

P.S. Thanks for buying the book!