ansible执行shell模块和command模块报错| FAILED | rc=127 >> /bin/sh: lsof: command not found和| rc=2 >> [Errno 2] No such file or directory

命令:

ansible -i hosts_20 st  -m shell -a 'service zabbix_agentd star'  -K --become

ansible -i hosts_20 st  -m shell -a 'lsof -i:10050'  -K --become

 

在shell模块报错:| FAILED | rc=127 >>
/bin/sh: lsof: command not found

 

在command模块报错:| rc=2 >>
[Errno 2] No such file or directory

 

 google了一下发现ansible使用shell或者command模块加载环境变量变了,

ansible加载的环境变量:

本机上面(192.168.20.33):

 

 

在ansible执行和在本机执行echo $pATH命令结果不一样..........................

ansible的command模块或者shell模块因为没有完全加载环境变量。

 

一个临时的解决方法就是在ansible使用shell或者command的模块的时候,强行改变当前系统环境变量:PAHT="/usr/local/sbin:/usr/sbin:/sbin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin";source /etc/profile;

从ansible -i hosts_20 st  -m shell -a 'lsof -i:10050'  -K --become

变成了ansible -i hosts_20 st  -m shell -a 'PAHT="/usr/local/sbin:/usr/sbin:/sbin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin";source /etc/profile;lsof -i:10050'  -K --become

posted on 2017-08-16 19:09  losbyday  阅读(9143)  评论(0编辑  收藏  举报

导航