简体   繁体   中英

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:

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:

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:

[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

[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

install <command> if you want to use it... check for problems with your PATH ... use the correct syntax for your shell

I've setup a small test which I found it just working

test.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

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

whereby the command nc are installed and under 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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