# Losing environment with sudo

**URL:** <https://forum.ansible.com/t/losing-environment-with-sudo/13441>\
**Category:** Ansible Project\
**Created:** [September 10, 2013, 6:28am UTC](https://forum.ansible.com/t/losing-environment-with-sudo/13441 "2013-09-10T06:28:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_Young](https://avatars.discourse-cdn.com/v4/letter/i/e5b9ba/32.png) [@Ian\_Young](https://forum.ansible.com/u/Ian_Young)\
**Post date:** [September 10, 2013, 6:28am UTC](https://forum.ansible.com/t/losing-environment-with-sudo/13441/1 "2013-09-10T06:28:55Z")

</div>

I’ve got some environment variables I need and am running into trouble when trying to access them while sudo’d to another user. Here’s me using the `ansible` tool to look for a var that’s set in /etc/bash.bashrc:

$ ansible foo -i ./ansible\_hosts -u vagrant -m shell -a ‘echo $RBENV\_ROOT’

192.168.222.111 | success | rc=0 \>\>

/usr/local/rbenv

$ ansible foo -i ./ansible\_hosts -u root -U vagrant -m shell -a ‘echo $RBENV\_ROOT’

192.168.222.111 | success | rc=0 \>\>

# Nothing!

You can see the var appears fine when running directly as the vagrant user, just not when sudo’d to it. I can also see it when sudoing manually on the machine, like this: `sudo -u vagrant echo $RBENV_ROOT`. I’ve tried adding `sudo_flags = -H` or `sudo_flags = -i` to my ~/.ansible.cfg file, to no effect.

Any idea what’s going on? Is this ansible’s fault, or a system thing?

---

<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 10, 2013, 7:07am UTC](https://forum.ansible.com/t/losing-environment-with-sudo/13441/2 "2013-09-10T07:07:55Z")

</div>

Most likely this is your sudoers configuration. You need to add any environment variables you wish to allow to be passed on to the env\_keep variable.
