ansible/ansible — pull request #6636 by iambocai (2014-03-23 14:01:52+00:00)
Title: work around environment problems in sudo mode

# this is repull of #6629
The problem can reproduce like this：
on some of my hosts(take 'fe08' as an example here), I have some tools
that _only_ can be used by user 'work'(for some security reason), so as
a result, I register it in work's .bashrc:
>[work@lg-com-xcache09 ~]$ which ipaddr
~/bin/ipaddr
>[work@lg-com-xcache09 ~]$ cat .bashrc
```bash
# .bashrc
# ------ skip unrelated -----
# User specific environment and startup programs
export PATH=$HOME/bin:$PATH
```

But ansible seems can't find them at all:
>[bob@cs00 ~]$ ansible 'fe08' -m shell -a 'ipaddr' -s -U work
> fe08 | FAILED | rc=127 >>
> /bin/sh: ipaddr: command not found

Later I have found that ansibles sudo mode is in fact a non-login
terminal to run the command：
> [bob@cs00 ~]$ ansible 'fe08' -m shell -a 'shopt login_shell' -s -U
work
> fe08 | FAILED | rc=1 >>
> login_shell off

But according to the [norms](http://linux.die.net/man/1/bash), even
non-login shell also need to source ~ /.bashrc, that's which we did not
do recently:
> [bob@cs00 ~]$ ansible 'fe08' -m shell -a 'echo $PATH' -s -U work

> fe08 | success | rc=0 >>
> /usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin

In fact, we run a lot of programs in production environments which is
in need for environment variables like PYTHONPATH JAVA_HOME and so on.

there is a '-i' option in sudo can do these things, but when I add it
to sudo_flags(in ansible.cfg), ansible crashed:
> [bob@cs00 ~]$ ansible 'fe08' -m shell -a 'whoami' -s -U work
> fe08 | FAILED >> {
> "failed": true,
> "msg": "\r\n",
> "parsed": false
> }

To work around this problem, I add a shell hook, and run souce commands
in it