LinuxSysAdmin

Things I don't want to look for twice..

changing PATH for Ansible and Ansible loops

So, I’ve had a problem with trying to run things as ansible and sudo. Of course, password was not required for any of my sudo commands, but I often get “command not found”. Clearly, after running “env” with ansible using sudo on a server, I noticed I was missing /usr/sbin and /sbin from my path, as opposed to root’s PATH with had both folders.

In my case, was a Debian init stript which relied on start-stop-daemon, which is in sbin, and I got the command not found error. Here is how you set it up in a playbook:

 - name: stop monit service
 shell: PATH=$PATH:/usr/sbin:/sbin sudo /etc/init.d/monit stop

Testing showed that:

ansible test-srv -s -m shell -a "PATH=$PATH:/usr/sbin:/sbin env"
test-srv | success | rc=0 >>
PATH=/home/ansible/ansible/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/sbin:/sbin

Today I also discovered loops. I had to run a bunch of commands on a lot of servers, searched a bit and found loops. So, if you need to run 5 commands on your servers on a single task from a playbook, do the following:

- name: multiple commands
  command: "{{item}}"
  with_items:
    - /path/to/command1
    - /path/to/command2
    - command3
    - command4
    - command5

Path requirement is for the case in which the specified command isn’t in user’s PATH by default and you should specify full path for the command itself.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: