# Environment variables seen with gather\_facts but not seen in playbooks

**URL:** <https://forum.ansible.com/t/environment-variables-seen-with-gather-facts-but-not-seen-in-playbooks/18250>\
**Category:** Ansible Project\
**Created:** [May 18, 2015, 11:56am UTC](https://forum.ansible.com/t/environment-variables-seen-with-gather-facts-but-not-seen-in-playbooks/18250 "2015-05-18T11:56:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adrian\_Paraschiv](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Adrian\_Paraschiv](https://forum.ansible.com/u/Adrian_Paraschiv)\
**Post date:** [May 18, 2015, 11:56am UTC](https://forum.ansible.com/t/environment-variables-seen-with-gather-facts-but-not-seen-in-playbooks/18250/1 "2015-05-18T11:56:57Z")

</div>

Hello,

I have a script file with 40-50 variables (some are something like this: ENV=`hostname | awk -F- '{print $1}' | sed 's/ACE//'`) and I deploy this file to all my machines in /etc/profile.d/some\_script.sh

I created a new user with which I only run deployments with ansible and have modified the /etc/sudoers file like this:

Defaults !env\_reset #notice the “!” this does not allow sudo to reset the env. This way when I run something with sudo I don’t lose environment variables  
ansible ALL=(ALL) NOPASSWD: ALL

Something goes wrong with the command below I can see the env variables  
[ansible@ACEPR-ANSIREBOND-01 ansible]$ ansible ACEPP-LM-01 -m setup | grep ENV  
“ENV”: “PP”,  
“ENV\_PROP”: “preprod”,  
“ENV\_PROP\_PREFIX”: “preprod”,

But when I run this playbook:

- name: prereq  
hosts: ACEPP-LM-01  
remote\_user: ansible  
sudo: True  
tasks:
- shell: echo {{ ansible\_env.ENV }}

It gives error: fatal: [ACEPP-LM-01] =\> One or more undefined variables: ‘dict’ object has no attribute ‘ENV’  
Why is ansible seeing the env variables (gathering\_facts is ok) but it can’t resolve {{ ansible\_env.ENV }}

[ansible@ACEPP-LM-01 ~]$ sudo echo $ENV  
PP  
[ansible@ACEPP-LM-01 ~]$ env | grep ENV  
ENV=PP  
ENV\_PROP=preprod  
ENV\_PROP\_PREFIX=preprod

With root and no sudo everything is ok. Is it the sudo ?

---

<div class="post-metadata">

**Author:** ![Adrian\_Paraschiv](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Adrian\_Paraschiv](https://forum.ansible.com/u/Adrian_Paraschiv)\
**Post date:** [May 20, 2015, 5:32am UTC](https://forum.ansible.com/t/environment-variables-seen-with-gather-facts-but-not-seen-in-playbooks/18250/2 "2015-05-20T05:32:36Z")

</div>

I have modified ~/.bashrc for root and ansible user to include : alias sudo=“sudo -i” and also modified ansible.cfg : executable = /bin/bash

[ansible@ACEPP-LM-01 ~]$ sudo env | grep ENV  
ENV=PP  
ENV\_PROP=preprod  
ENV\_PROP\_PREFIX=preprod  
[ansible@ACEPP-LM-01 ~]$ sudo env | grep SUDO  
SUDO\_USER=ansible  
SUDO\_UID=30001  
SUDO\_COMMAND=/bin/bash -c env  
SUDO\_GID=30001  
[ansible@ACEPP-LM-01 ~]$ env | grep ENV  
ENV=PP  
ENV\_PROP=preprod  
ENV\_PROP\_PREFIX=preprod

and when I run a “shell: echo $ENV && env && whoami && cat ~/.bashrc”  
I get this:

stdout:

TERM=xterm  
SHELL=/bin/bash  
USER=root  
SUDO\_USER=ansible  
SUDO\_UID=30001  
USERNAME=root  
MAIL=/var/mail/ansible  
PATH=/usr/bin:/bin  
PWD=/users/ansible  
LANG=C  
HOME=/root  
SUDO\_COMMAND=/bin/bash -c echo SUDO-SUCCESS-gkqnfqfgysvzciwpzfzhjksuidabfmha; LANG=C LC\_CTYPE=C /usr/bin/python /users/ansible/.ansible/tmp/ansible-tmp-1432099297.2-178454781677599/command; rm -rf /users/ansible/.ansible/tmp/ansible-tmp-1432099297.2-178454781677599/ \>/dev/null 2\>&1  
SHLVL=2  
LOGNAME=root  
LC\_CTYPE=C  
SUDO\_GID=30001  
\_=/usr/bin/env  
root  
[output omitted]  
…  
alias sudo=“sudo -i”

So sudo uses now /bin/bash but it ignors ~/.bashrc alias. Why ? What am I missing ?

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [May 20, 2015, 5:46am UTC](https://forum.ansible.com/t/environment-variables-seen-with-gather-facts-but-not-seen-in-playbooks/18250/3 "2015-05-20T05:46:47Z")

</div>

in your ansible.cfg have you set tried setting “sudo\_flags=-H -i” ? this would make the sudo invoke a login shell

- Benno

---

<div class="post-metadata">

**Author:** ![Adrian\_Paraschiv](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Adrian\_Paraschiv](https://forum.ansible.com/u/Adrian_Paraschiv)\
**Post date:** [May 20, 2015, 5:51am UTC](https://forum.ansible.com/t/environment-variables-seen-with-gather-facts-but-not-seen-in-playbooks/18250/4 "2015-05-20T05:51:10Z")

</div>

@benno joy  
I didn’t have that set ! It’s working ! Thanks a lot ! Put up a paypal/donate - i’ll send you a beer! 🙂
