Simple Shell Command not executing

1,071 views
Skip to first unread message

pradipto dasgupta

unread,
Dec 8, 2021, 10:03:10 PM12/8/21
to Ansible Project
 Hello All,

I am new in Ansible and was trying to execute a shell script in one of the remote hosts. The script runs fine when executed directly from the host. It is a simple shell script to create a folder.

The shell script is
=============
#!/bin/sh
sudo mkdir new
==============

The error I am getting is 
======================
fatal: [web1]: FAILED! => {"changed": true, "cmd": "create_new_folder.sh", "delta": "0:00:00.021853", "end": "2021-12-09 02:34:21.464383", "msg": "non-zero return code", "rc": 127, "start": "2021-12-09 02:34:21.442530", "stderr": "/bin/sh: create_new_folder.sh: command not found", "stderr_lines": ["/bin/sh: create_new_folder.sh: command not found"], "stdout": "", "stdout_lines": []}
=======================


The yml file is
=======================
-
 name: Play1
 hosts: web1
 tasks:
  - name: Excute shell script
    shell: create_new_folder_pradipto.sh
==============================

I also tried changing the yml file to

=======================
-
 name: Play1
 hosts: web1
 tasks:
  - name: Excute shell script
    shell: 
      cmd: create_new_folder_pradipto.sh
==============================

Any idea what is wrong here

pradipto dasgupta

unread,
Dec 8, 2021, 10:29:40 PM12/8/21
to Ansible Project
I can get the script run if I use the "command" module in the yml file

=======================
-
 name: Execute a script
 hosts: web1
 tasks:
  - name: Execute a simple script
    command: ./create_new_folder_pradipto.sh
============================

Nico Kadel-Garcia

unread,
Dec 9, 2021, 2:46:53 AM12/9/21
to ansible...@googlegroups.com
That shell script is not in your PATH. If you just created the
script, you should use "cmd: ./create_new_folder_pradipto.sh"
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1027a91e-a3af-465b-afa0-48ba0684d3aan%40googlegroups.com.

sarbajit das

unread,
Dec 10, 2021, 7:11:57 PM12/10/21
to ansible...@googlegroups.com
Hi Pradipto,
As you can see the error says "create_new_folder.sh: command not found".

Just a little addition will resolve this issue.

The yml file is
=======================
-
 name: Play1
 hosts: web1
 tasks:
  - name: Execute shell script
    shell: sh create_new_folder_pradipto.sh
==============================

Try this one.

Regards,
Sarbajit Das

Virus-free. www.avg.com

--

prave...@gmail.com

unread,
Dec 10, 2021, 7:32:23 PM12/10/21
to Ansible Project
Better use script module

  - name: Execute shell script
    script: create_new_folder_pradipto.sh

pradipto dasgupta

unread,
Dec 10, 2021, 7:42:15 PM12/10/21
to Ansible Project
Thank you all. Eventually I realized I should be using "script" instead of "shell"

Dick Visser

unread,
Dec 11, 2021, 3:31:42 AM12/11/21
to ansible...@googlegroups.com
Keep in mind 

  • It is usually preferable to write Ansible modules rather than pushing scripts. Convert your script to an Ansible module for bonus points!


Since your script just creates a directory that can be better done using an ansible file task.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.
Reply all
Reply to author
Forward
0 new messages