简体   繁体   English

Ansible 无法执行脚本

[英]Ansible unable to execute a script

I have a script that run perfectly fine on a server but when I try to run it from ansible it gives me an error:我有一个在服务器上运行得很好的脚本,但是当我尝试从 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

In the server it gives the output as:在服务器中,它给出 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

I saved this as ncat.sh on ansible server and on local server as well but when I runit from ansible I get the below error:我在 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."
    ]
} 

When I am trying to run the script in server through ansible it gives below error, I figured ansible is not giving output to command such as ifconfig, I tried "IP a" but it doesn't work当我尝试通过 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

In respect to your error messages line 1: <command>: command not found and the comments关于您的错误消息line 1: <command>: command not found和注释

install <command> if you want to use it... check for problems with your PATH ... use the correct syntax for your shell如果您想使用它,请安装 <command>... 检查您的PATH是否存在问题...为您的 shell 使用正确的语法

I've setup a small test which I found it just working我设置了一个小测试,我发现它只是工作

test.sh测试.sh

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

when running the local script on remote node after transferring it . 传输后在远程节点上运行本地脚本时

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

Please take note that on the remote host the commands ifconfig , ip are installed and under sbin请注意,在远程主机上,命令ifconfigip安装在sbin

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

whereby the command nc are installed and under bin .其中命令nc已安装并位于bin下。

whereis nc
nc: /usr/bin/nc

Therefore you may need to specify the full path /usr/sbin/ifconfig or /usr/sbin/ip in your script.因此,您可能需要在脚本中指定完整路径/usr/sbin/ifconfig/usr/sbin/ip Otherwise you may run into an error like否则你可能会遇到类似的错误

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

if testing this without full path.如果在没有完整路径的情况下进行测试。

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

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