繁体   English   中英

Ansible 无法执行脚本

[英]Ansible unable to execute a script

我有一个在服务器上运行得很好的脚本,但是当我尝试从 ansible 运行它时,它给了我一个错误:

for i in {1..5}; do
    echo "quit" | nc -s $(ifconfig $(echo "eth1:$i") |
    grep 'inet' | awk '{print $2}' | xargs) 2x.1x.13x.21x 25
done

在服务器中,它给出 output 为:

220 ns1.mywebsite.ng ESMTP Service() ready Fri, 17 Dec 2021 16:01:31 +0900 (JST)
221 ns1.mywebsite.ng
220 ns2.mywebsite.ng ESMTP Service() ready Fri, 17 Dec 2021 16:01:32 +0900 (JST)
221 ns2.mywebsite.ng
220 ns3.mywebsite.ng ESMTP Service() ready Fri, 17 Dec 2021 16:01:32 +0900 (JST)
221 mgspc03.cybermail.jp
220 ns2.mywebsite.ng ESMTP Service() ready Fri, 17 Dec 2021 16:01:32 +0900 (JST)
221 ns2.mywebsite.ng
220 mgspc03.cybermail.jp ESMTP Service() ready Fri, 17 Dec 2021 16:01:33 +0900 (JST)
221 mgspc03.cybermail.jp

我在 ansible 服务器和本地服务器上将其保存为 ncat.sh,但是当我从 ansible 运行时,我收到以下错误:

[ansingh@jumpbox ansible_scripts]$ ansible all -i bemta_server -m script -a /home/ansingh/ansible_scripts/ncat.sh
server.net| FAILED! => {
    "changed": true,
    "msg": "non-zero return code",
    "rc": 2,
    "stderr": "Shared connection to server.netclosed.\r\n",
    "stderr_lines": [
        "Shared connection to server.netclosed."
    ],
    "stdout": "/home/ansingh/.ansible/tmp/ansible-tmp-1639731843.01-102907769344857/ncat.sh: line 1: ip: command not found\r\nNcat: You must specify a host to connect to. QUITTING.\r\n/home/ansingh/.ansible/tmp/ansible-tmp-1639731843.01-102907769344857/ncat.sh: line 1: ip: command not found\r\nNcat: You must specify a host to connect to. QUITTING.\r\n/home/ansingh/.ansible/tmp/ansible-tmp-1639731843.01-102907769344857/ncat.sh: line 1: ip: command not found\r\nNcat: You must specify a host to connect to. QUITTING.\r\n/home/ansingh/.ansible/tmp/ansible-tmp-1639731843.01-102907769344857/ncat.sh: line 1: ip: command not found\r\nNcat: You must specify a host to connect to. QUITTING.\r\n/home/ansingh/.ansible/tmp/ansible-tmp-1639731843.01-102907769344857/ncat.sh: line 1: ip: command not found\r\nNcat: You must specify a host to connect to. QUITTING.\r\n",
    "stdout_lines": [
        "/home/ansingh/.ansible/tmp/ansible-tmp-1639731843.01-102907769344857/ncat.sh: line 1: ip: command not found",
        "Ncat: You must specify a host to connect to. QUITTING.",
        "/home/ansingh/.ansible/tmp/ansible-tmp-1639731843.01-102907769344857/ncat.sh: line 1: ip: command not found",
        "Ncat: You must specify a host to connect to. QUITTING.",
        "/home/ansingh/.ansible/tmp/ansible-tmp-1639731843.01-102907769344857/ncat.sh: line 1: ip: command not found",
        "Ncat: You must specify a host to connect to. QUITTING.",
        "/home/ansingh/.ansible/tmp/ansible-tmp-1639731843.01-102907769344857/ncat.sh: line 1: ip: command not found",
        "Ncat: You must specify a host to connect to. QUITTING.",
        "/home/ansingh/.ansible/tmp/ansible-tmp-1639731843.01-102907769344857/ncat.sh: line 1: ip: command not found",
        "Ncat: You must specify a host to connect to. QUITTING."
    ]
} 

当我尝试通过 ansible 在服务器中运行脚本时,它给出了以下错误,我认为 ansible 没有给 output 提供命令,例如 ifconfig,我尝试了“IP a”

[ansingh@jumpbox ansible_scripts]$ ansible all -i bemta_server -m command -a 'sh /home/ansingh/ncat.sh'
server.net| FAILED | rc=2 >>
/home/ansingh/ncat.sh: line 1: ifconfig: command not found
Ncat: You must specify a host to connect to. QUITTING.
/home/ansingh/ncat.sh: line 1: ifconfig: command not found
Ncat: You must specify a host to connect to. QUITTING.
/home/ansingh/ncat.sh: line 1: ifconfig: command not found
Ncat: You must specify a host to connect to. QUITTING.
/home/ansingh/ncat.sh: line 1: ifconfig: command not found
Ncat: You must specify a host to connect to. QUITTING.
/home/ansingh/ncat.sh: line 1: ifconfig: command not found
Ncat: You must specify a host to connect to. QUITTING.non-zero return code

[ansingh@jumpbox ansible_scripts]$ ansible all -i bemta_server -m shell -a 'sh /home/ansingh/ncat.sh'
server.net| FAILED | rc=2 >>
/home/ansingh/ncat.sh: line 1: ifconfig: command not found
Ncat: You must specify a host to connect to. QUITTING.
/home/ansingh/ncat.sh: line 1: ifconfig: command not found
Ncat: You must specify a host to connect to. QUITTING.
/home/ansingh/ncat.sh: line 1: ifconfig: command not found
Ncat: You must specify a host to connect to. QUITTING.
/home/ansingh/ncat.sh: line 1: ifconfig: command not found
Ncat: You must specify a host to connect to. QUITTING.
/home/ansingh/ncat.sh: line 1: ifconfig: command not found
Ncat: You must specify a host to connect to. QUITTING.non-zero return code

关于您的错误消息line 1: <command>: command not found和注释

如果您想使用它,请安装 <command>... 检查您的PATH是否存在问题...为您的 shell 使用正确的语法

我设置了一个小测试,我发现它只是工作

测试.sh

for i in {0..0}; do
    /usr/sbin/ifconfig $(echo "eth${i}") | grep 'inet'
    nc -vz $(hostname) 8081
done

传输后在远程节点上运行本地脚本时

ansible test --ask-pass -m script --args "test.sh"

请注意,在远程主机上,命令ifconfigip安装在sbin

whereis ifconfig
ifconfig: /usr/sbin/ifconfig
whereis ip
ip: /usr/sbin/ip

其中命令nc已安装并位于bin下。

whereis nc
nc: /usr/bin/nc

因此,您可能需要在脚本中指定完整路径/usr/sbin/ifconfig/usr/sbin/ip 否则你可能会遇到类似的错误

stdout": "/home/test/.ansible/tmp/ansible-tmp-<...>/test.sh: line 2: ifconfig: command not found\r\nNcat: Version 7.5 ..."

如果在没有完整路径的情况下进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM