Ansible and shell environmental variables

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

Try setting the shell in group_vars or host_vars or the play:

ansible_shell_type: ksh

Maybe this helps.

Johannes

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.