Ansible 被管理机需要使用代理上网时yum不能联网的问题(login shell 和 non-login shell),ansiblenon-login


被管理服务器需要使用http和https代理才能上网,如在/etc/profile中设置了
export http_proxy=http://172.16.16.80:8080
export https_proxy=http://172.16.16.80:8080

此时通过ssh连接到服务器,执行yum,会走这个代理,安装软件没有问题。
但是在ansible服务器端,通过ansible在客户端执行yum会发现,命令执行时yum连接不到外网,导致命令执行失败。
#ansible app -m yum -a “name=iftop state=present”

此时需要在客户端 /etc/bashrc 文件添加代理配置
export http_proxy=http://172.16.16.80:8080

之后执行成功。

原因
login shell 和 non-login shell

我们登录执行的是login shell,会加载/etc/profile,~/.bash_profile
ansible这类ssh远程执行是non-login shell,不会加载etc/profile,/.bash_profile,而是加载etc/bashrc和/.bashrc

login shell:取得bash时需要完整的登入流程的,就称为login shell。举例来说,你要由tty1~tty6登入,需要输入用户的账号和密码,此时取得的bash就称为『login shell』啰;
non-login shell:取得bash接口的方法不需要重复登入的举动,举例来说,(1)你以Xwindow登入Linux后,再以X的图形化接口启动终端机,此时那个终端接口并没有需要再次的输入账号和密码,那个bash的环境就称为non-login shell了。(2)你在原本的bash环境下再次下达bash这个命令,同样的也没有输入账号密码,那第二个bash (子程序)也是non-login shell 。
结论
ansible这类远程执行的non-login shell 并不会加载/etc/profile和/.bash_profile下的环境变量,只是加载/.bashrc和/etc/bashrc
如果需要在ansible中执行需要特定环境变量的命令,可以在执行前source一下~/.bash_profile, 或者将环境变量写在~/.bashrc 。

相关内容

    暂无相关文章