# Getting remote user environment with sudo

**URL:** <https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210>\
**Category:** Ansible Project\
**Created:** [January 18, 2013, 9:22am UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210 "2013-01-18T09:22:30Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michel\_Blanc1](https://avatars.discourse-cdn.com/v4/letter/m/fbc32d/32.png) [@Michel\_Blanc1](https://forum.ansible.com/u/Michel_Blanc1)\
**Post date:** [January 18, 2013, 9:22am UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210/1 "2013-01-18T09:22:30Z")

</div>

Hi all,

It seems I can not get the remote user's environment loaded when using  
sudo in a playbook (or at the command line).

Here is an example : I need to retrieve the installed ruby version with  
rbenv. Ruby is installed for user 'rbuser', and rbenv requires .bashrc  
to be loaded so it can be found.

Trying at the command line (or in a playbook with "sudo: True" and  
"sudo\_user: rbuser") doesn't work :

ws$ ansible all -m shell -a 'rbenv version' -l goat -U rbuser  
\<goat\> EXEC /bin/sh -c 'sudo -k && sudo -p "[sudo snip] password: " -u  
rbuser /bin/sh -c '"'"'/usr/bin/python  
/tmp/ansible-1358499444.98-121478482753266/command; rm -rf  
/tmp/ansible-1358499444.98-121478482753266/ \>/dev/null 2\>&1'"'"''  
goat | FAILED | rc=127 \>\>  
/bin/sh: 1: rbenv: not found

Trying the geenrated python directly from the node unsurprisingly gives  
the same result :

ws$ ssh root@goat  
root@goat:~ sudo -u rbuser /bin/sh -c '/usr/bin/python  
/tmp/ansible-1358499444.98-121478482753266/command'  
{"changed": true, "end": "2013-01-18 09:57:54.312280", "stdout": "",  
"cmd": "rbenv version ", "start": "2013-01-18 09:57:54.302952", "delta":  
"0:00:00.009328", "stderr": "/bin/sh: 1: rbenv: not found", "rc": 127}

However, when trying with an additional (-i) flag (asking sudo to load  
the user's environment), it works fine :

root@goat:~ sudo -iu rbuser /bin/sh -c '/usr/bin/python  
/tmp/ansible-1358499444.98-121478482753266/command'  
{"changed": true, "end": "2013-01-18 09:58:06.176637", "stdout": "system  
(set by /home/rbuser/.rbenv/version)", "cmd": "rbenv version ", "start":  
"2013-01-18 09:58:06.061737", "delta": "0:00:00.114900", "stderr": "",  
"rc": 0}

Am I missing something here ? Should we invoke sudo with -i flag in  
make\_sudo\_command ?

Thanks,

M

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [January 18, 2013, 1:06pm UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210/2 "2013-01-18T13:06:33Z")

</div>

Sudo normally removes most env vars, you should be able to configure it to preserve those you need.

Brian Coca

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [January 18, 2013, 1:07pm UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210/3 "2013-01-18T13:07:56Z")

</div>

Sudo has a -E flag that prevents env filtering, test with that.

Brian Coca

---

<div class="post-metadata">

**Author:** ![Michel\_Blanc1](https://avatars.discourse-cdn.com/v4/letter/m/fbc32d/32.png) [@Michel\_Blanc1](https://forum.ansible.com/u/Michel_Blanc1)\
**Post date:** [January 18, 2013, 1:30pm UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210/4 "2013-01-18T13:30:33Z")

</div>

Hi Brian,

May be I misexplained my problem, but it is not about preserving  
environment; it's about getting the environment of the target user.

For instance, when the playbook is ran with this :

&nbsp;&nbsp;- hosts: ubuntu  
&nbsp;&nbsp;&nbsp;&nbsp;name: Install ruby for the configured ruby user  
&nbsp;&nbsp;&nbsp;&nbsp;sudo: True  
&nbsp;&nbsp;&nbsp;&nbsp;sudo\_user: rubyuser  
&nbsp;&nbsp;&nbsp;&nbsp;# should be ${ruby\_user}, but can't for now because of #1665  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: Gets current ruby version  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: shell rbenv version  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: ruby\_current\_version

The 'rubyuser' environment on the remote node is not loaded when the  
tasks are run. This prevents tools like rvm or rbenv from working,  
because they both need to be initialized and have their path added to  
$PATH, and this setup phase typically takes place in .bash\_profile.

Just to check :

$ ansible all -m shell -a 'echo $HOME' -l somehost -U someuser  
somehost | success | rc=0 \>\>  
/root

What I'd rather get : /home/someuser

So the need here is to be sure that when root SUDOs as someuser, the  
latter gets it's environment properly (AFAIK, -E does the exact oppostite).

And the problem is not doing it by hand, but being able to get the  
user-we-sudo-as environment loaded.

May be I'm missing the point, and loading the target user is a problem,  
but I don't really see why. As a side effect, it would have solved #1864.

M

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [January 18, 2013, 2:37pm UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210/5 "2013-01-18T14:37:39Z")

</div>

Sorry, I had misunderstood.

One thing to note is that ansible uses /bin/sh by default, which does  
not source .bash\_profile, even if you switch to bash it would have to  
do so as a login shell (--login IIRC).

---

<div class="post-metadata">

**Author:** ![Michel\_Blanc1](https://avatars.discourse-cdn.com/v4/letter/m/fbc32d/32.png) [@Michel\_Blanc1](https://forum.ansible.com/u/Michel_Blanc1)\
**Post date:** [January 18, 2013, 2:49pm UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210/6 "2013-01-18T14:49:32Z")

</div>

> Sorry, I had misunderstood.

No problem. It was probably unclear in the first place !

> One thing to note is that ansible uses /bin/sh by default, which does  
> not source .bash\_profile, even if you switch to bash it would have to  
> do so as a login shell (--login IIRC).

Yes, I tried all sort of combinations, e.g.

action: shell bash -lc 'rbenv version'

and none worked. Seems that using '-i' in make\_sudo\_cmd is the best option.

I'll just send a PR and see how it goes.

Thanks,

M

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 18, 2013, 2:54pm UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210/7 "2013-01-18T14:54:23Z")

</div>

I think I'm fine with adding -i to sudo (so that the environment gets  
loaded) unless anyone is running a sudo that doesn't have it.

BSD/Solaris folks, please check?

--Michael

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [January 18, 2013, 2:55pm UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210/8 "2013-01-18T14:55:32Z")

</div>

confirmed -i on freebsd 8.0 and above

---

<div class="post-metadata">

**Author:** ![Marcus\_Philip](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Marcus\_Philip](https://forum.ansible.com/u/Marcus_Philip)\
**Post date:** [September 19, 2013, 1:47pm UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210/9 "2013-09-19T13:47:45Z")

</div>

Has this made it to a released Ansible version yet?

//Marcus

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 19, 2013, 8:57pm UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210/10 "2013-09-19T20:57:00Z")

</div>

It is in the current development branch, and will be in 1.4.

---

<div class="post-metadata">

**Author:** ![Sergei\_Iakhnin](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@Sergei\_Iakhnin](https://forum.ansible.com/u/Sergei_Iakhnin)\
**Post date:** [September 23, 2014, 9:28pm UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210/11 "2014-09-23T21:28:55Z")

</div>

Hi,

I’m running into the same issue the original poster was running into. Based on my cursory search it appears that this change has never made it into a release. Does anyone know why?

Thanks,

Sergei.

---

<div class="post-metadata">

**Author:** ![Sergei\_Iakhnin](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@Sergei\_Iakhnin](https://forum.ansible.com/u/Sergei_Iakhnin)\
**Post date:** [September 23, 2014, 9:40pm UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210/12 "2014-09-23T21:40:32Z")

</div>

I don’t see this change in any current release. Did it get reverted for some reason?

---

<div class="post-metadata">

**Author:** ![Michel\_Blanc](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@Michel\_Blanc](https://forum.ansible.com/u/Michel_Blanc)\
**Post date:** [September 24, 2014, 4:05am UTC](https://forum.ansible.com/t/getting-remote-user-environment-with-sudo/12210/13 "2014-09-24T04:05:53Z")

</div>

Hi Sergei,

The patch was accepted but it turned out it had unwanted side effects,  
with somme connections lingering under some circumptances, so in the  
end, it was removed from the code.

So I now I rap all my shell calls in 'bash -lc' to get the remote user's  
environment set-uip properly. For instance :

- shell: cd; bash -lc "rbenv install {{ ruby\_version }}"

Cheers,

M
