# Environment variables unavaiable

**URL:** <https://forum.ansible.com/t/environment-variables-unavaiable/27114>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [March 26, 2018, 9:31am UTC](https://forum.ansible.com/t/environment-variables-unavaiable/27114 "2018-03-26T09:31:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Simovic](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@Martin\_Simovic](https://forum.ansible.com/u/Martin_Simovic)\
**Post date:** [March 26, 2018, 9:31am UTC](https://forum.ansible.com/t/environment-variables-unavaiable/27114/1 "2018-03-26T09:31:06Z")

</div>

Hi,

I am observing following behaviour using ansible on set of Ubuntu-1604, SLES-12 and CentOS-7 hosts.

Environment variables, normally avaiable for login shell (/bin/bash) are not avaiable via ansible “setup” or “shell” modules for Ubuntu-16.04. Yet the same variables (defined in /etc/profile) are available for both these modules for SLES and CentOS distributions.

Seems as if ansible run shell as login shell for Sles and Centos, and non-login shell for Ubuntu. This inconsistency is killing me. Anyone came across this? Possible solution?

Many Thanks  
Martin.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 26, 2018, 10:02am UTC](https://forum.ansible.com/t/environment-variables-unavaiable/27114/2 "2018-03-26T10:02:18Z")

</div>

This is normal in Debian and its derivatives.  
Ansible is using /bin/sh that is symlink to dash.

The dash shell has to mode like most other shell, interactive and non-interactive and the /etc/profile is only read in interactive shell mode.  
But since Ansible is not logging in, it just run a command it uses non-interactive that file is not read.

For more information check man dash search for Invocation.

You need to use /etc/environment but remember that this file doesn't support variable expansion.

---

<div class="post-metadata">

**Author:** ![Martin\_Simovic](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@Martin\_Simovic](https://forum.ansible.com/u/Martin_Simovic)\
**Post date:** [March 26, 2018, 10:10am UTC](https://forum.ansible.com/t/environment-variables-unavaiable/27114/3 "2018-03-26T10:10:06Z")

</div>

Thank you Kai for your quick answer.

However, this is happening when I use command module with args: executable=/bin/bash too. This kind of rules the dash theory out.  
It is almost like /etc/profile is ignored in any case.

Still, on Centos/Sles it is sourced regardless of /bin/sh or /bin/bash use (I know that /bin/sh is symlink to /bin/bash on those distributions)

?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 26, 2018, 11:44am UTC](https://forum.ansible.com/t/environment-variables-unavaiable/27114/4 "2018-03-26T11:44:22Z")

</div>

> However, this is happening when I use command module with args:  
> executable=/bin/bash too. This kind of rules the dash theory out.  
> It is almost like /etc/profile is ignored in any case.

bash working the same as dash in this aspect.

> Still, on Centos/Sles it is sourced regardless of /bin/sh or /bin/bash use  
> (I know that /bin/sh is symlink to /bin/bash on those distributions)

bash has a posix mode is uses when bash is started with name sh, this mode behave differently.  
It's documented in the man pages.

---

<div class="post-metadata">

**Author:** ![Martin\_Simovic](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@Martin\_Simovic](https://forum.ansible.com/u/Martin_Simovic)\
**Post date:** [March 26, 2018, 1:24pm UTC](https://forum.ansible.com/t/environment-variables-unavaiable/27114/5 "2018-03-26T13:24:27Z")

</div>

Yes, still according to man pages /bin/sh started as login shell should source /etc/profile (as it does on e.g. CENTOS)

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 26, 2018, 2:40pm UTC](https://forum.ansible.com/t/environment-variables-unavaiable/27114/6 "2018-03-26T14:40:52Z")

</div>

As I wrote in my first reply since Ansible is just executing a command(s), it doesn't use login shell/interactive shell, it uses non-interactive shell.

The same as this that will run env in a non-interactive shell  
ssh user@host env

So that is why non of the profile files is being read, so you need to use /etc/environment or make a hack yourself.

What Centos does I don't know since I don't use it.
