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

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

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

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

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

ansible加载的环境变量:

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

本机上面(192.168.20.33):

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

在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

上一篇:深入浅出Node.js(上)


下一篇:前端第二篇---前端基础之CSS