Skip to content
Advertisement

How to run an Ansible task with become and the same $PATH?

I want to run this task but it fails since it does not find the bundle binary because the user I am running it with has a different $PATH in Ansible than when I just sudo su - deploy.

If I SSH into the box as root and then run sudo su - deploy and echo 'whoami' $PATH I get deploy /home/deploy/.rbenv/shims:/home/deploy/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games.

But if I the same command as the shell: part of the ansible task I get deploy /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin.

The rbenv part is set up in /etc/profile.d/rbenv.sh but somehow it is not loaded if I run it with Ansible.

I found a wacky hack to prefix all my shell: params with source /etc/profile.d/rbenv.sh but it feels dirty 🙂

How can I fix this?

PS: I’m using Ansible 2.0.2.0 on my Fedora 23 and it talks to Ubuntu 14.04 servers.

Advertisement

Answer

http://bencane.com/2013/09/16/understanding-a-little-more-about-etcprofile-and-etcbashrc/

The difference is simple, the /etc/profile is executed only for interactive shells and the /etc/bashrc is executed for both interactive and non-interactive shells. In fact in Ubuntu the /etc/profile calls the /etc/bashrc directly.

So include your /etc/profile.d/rbenv.sh in .bashrc or

I found a wacky hack to prefix all my shell: params with source /etc/profile.d/rbenv.sh

Advertisement