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

190 views
Skip to first unread message

P Retzel

unread,
Aug 2, 2016, 12:45:12 PM8/2/16
to Ansible Project

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!

Kai Stian Olstad

unread,
Aug 3, 2016, 3:24:09 AM8/3/16
to ansible...@googlegroups.com
On 02.08.2016 18:20, P Retzel wrote:
> 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.

--
Kai Stian Olstad

P Retzel

unread,
Aug 3, 2016, 1:51:57 PM8/3/16
to Ansible Project, ansible-pr...@olstad.com
On Wednesday, August 3, 2016 at 3:24:09 AM UTC-4, Kai Stian Olstad wrote:
 
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.)

Jeff Geerling

unread,
Aug 5, 2016, 11:24:37 PM8/5/16
to Ansible Project, ansible-pr...@olstad.com
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 :)


-Jeff Geerling


P.S. Thanks for buying the book!

Reply all
Reply to author
Forward
0 new messages