Ansible and shell environmental variables

446 views
Skip to first unread message

Ahmed El-Madhoun

unread,
May 18, 2016, 6:22:27 PM5/18/16
to Ansible Project
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

Johannes Kastl

unread,
May 19, 2016, 8:46:16 AM5/19/16
to ansible...@googlegroups.com
On 19.05.16 00:22 Ahmed El-Madhoun wrote:

> Does ansible invoke its own "bash" shell, regardless of the ansible_user
> shell? For instance, if you run this against a KSH user:

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

ansible_shell_type: ksh

Maybe this helps.

Johannes


signature.asc

Brian Coca

unread,
May 19, 2016, 8:55:39 AM5/19/16
to ansible...@googlegroups.com
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.

----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages