# Ansible and shell environmental variables

**URL:** <https://forum.ansible.com/t/ansible-and-shell-environmental-variables/21486>\
**Category:** Ansible Project\
**Created:** [May 18, 2016, 10:22pm UTC](https://forum.ansible.com/t/ansible-and-shell-environmental-variables/21486 "2016-05-18T22:22:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_El-Madhoun](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Ahmed\_El-Madhoun](https://forum.ansible.com/u/Ahmed_El-Madhoun)\
**Post date:** [May 18, 2016, 10:22pm UTC](https://forum.ansible.com/t/ansible-and-shell-environmental-variables/21486/1 "2016-05-18T22:22:09Z")

</div>

Hi,

I have been testing ansible along with environment variables, since we utilize environment variables for various deployment reasons, I notice that on ansible will pick up any environment variables defined under .bashrc as thats the file sourced by /bin/login.

But under KSH, the profile file “.profile” is never read, and as such the ENV variable is not read, and as such, app/user environment is not visible within Ansible. Is there any suggestions on properly handing such cases?

Does ansible invoke its own “bash” shell, regardless of the ansible\_user shell? For instance, if you run this against a KSH user:

ansible xxxxxx -i jet/projects/rbm/rbm\_hosts -m shell -a “echo $SHELL”  
{‘\_host’: xxxxxx, ‘\_task’: TASK: command, ‘\_result’: {u’cmd’: u’echo /bin/bash’, u’end’: u’2016-05-18 18:20:37.051111’, ‘\_ansible\_no\_log’: False, u’stdout’: u’/bin/bash’, u’changed’: True, u’start’: u’2016-05-18 18:20:37.047621’, u’delta’: u’0:00:00.003490’, u’stderr’: u’‘, u’rc’: 0, ‘invocation’: {‘module\_name’: u’command’, u’module\_args’: {u’creates’: None, u’executable’: None, u’chdir’: None, u’\_raw\_params’: u’echo /bin/bash’, u’removes’: None, u’warn’: True, u’\_uses\_shell’: True}}, ‘stdout\_lines’: [u’/bin/bash’], u’warnings’: }}  
dukerbdv99 | SUCCESS | rc=0 \>\>  
/bin/bash

It returns /bin/bash, yet the user’s /etc/passwd clearly indicates it is KSH, and when I login, it is KSH.

Can anyone really clarify this for me?

Thanks in advance,  
Ahmed

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [May 19, 2016, 12:46pm UTC](https://forum.ansible.com/t/ansible-and-shell-environmental-variables/21486/2 "2016-05-19T12:46:04Z")

</div>

Try setting the shell in group\_vars or host\_vars or the play:

ansible\_shell\_type: ksh

Maybe this helps.

Johannes

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 19, 2016, 12:55pm UTC](https://forum.ansible.com/t/ansible-and-shell-environmental-variables/21486/3 "2016-05-19T12:55:33Z")

</div>

Ansible by default will invoke /bin/sh, if this is a symlink to bash, that is what it ends up running.

This is done to be able to use a ‘normal’ set of shellisms.

The hostvar ansible\_shell\_type should ONLY be set if ansible\_executable is set to a non sh/bash compatible shell and this var should ONLY be set if /bin/sh is NOT available (really only true in certain very restrictive environments).

As for environment vars, you cannot rely on the user specific files as Ansible attempts to avoid interactive logins when possible, you can use the ‘environment:’ directive at play/task level or set the variables in such a way that non-interactive logins also get affected.
