Losing environment with sudo

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?

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.