changing PATH for Ansible and Ansible loops
April 19, 2014
Posted by on
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 >>
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
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.