簡體   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