Unable to execute shell script

216 views
Skip to first unread message

Yogita Patil

unread,
Aug 18, 2024, 7:14:58 AM8/18/24
to Ansible Project
Hi All,

I am simply unable to execute a shell script on the remote host - this is my code (all other tasks run correctly except the execute task)

- name: Power consumption test
  hosts: all
  tasks:

     - name: Transfer the script
       ansible.builtin.copy:
         src: power_consumption.sh
         dest: /usr/src/power_consumption.sh
         mode: '0777'

     - name: Execute the script
#       command: ./usr/src/power_consumption.sh
       command: "{{ item }}"
       args:
         chdir: "/usr/src/"
       with_items:
        - "./power_consumption.sh"
       ignore_errors: true

     - name: Remove shell script from remote server
       file:
         path: /usr/src/power_consumption.sh
         state: absent

     - name: Copy shell script output from remote server
       fetch:
         src: /usr/src/{{ ansible_fqdn }}.txt
         dest: /usr/src
         mode: '0777'
         with_fileglob:
          _ "*.txt"


I have also tried with the shell: , script: sh ways of executing the script and none of them work, can someone tell me what I am doing wrong? TIA.

Dick Visser

unread,
Aug 18, 2024, 12:16:30 PM8/18/24
to ansible...@googlegroups.com
Without any real data it's impossible to tell. 
Can you at a minimum post the error message?

Sent from Gmail Mobile


--
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/7b120440-da9b-483a-965f-98cc9ab3f280n%40googlegroups.com.

alex...@gmail.com

unread,
Aug 19, 2024, 9:47:11 AM8/19/24
to Ansible Project

Create a var for script i.e.: script_path="/some/path/script" and use that instead of hard coding path

Yogita Patil

unread,
Aug 21, 2024, 4:33:21 AM8/21/24
to ansible...@googlegroups.com
Thanks for the responses guys, but these are the errors I see -

The full traceback is:
  File "/tmp/ansible_ansible.legacy.command_payload_uk3ys11b/ansible_ansible.legacy.command_payload.zip/ansible/module_utils/basic.py", line 2050, in run_command
    cmd = subprocess.Popen(args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/subprocess.py", line 1024, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.11/subprocess.py", line 1901, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
failed: [100.91.187.42] (item=./power_consumption.sh) => {
    "ansible_loop_var": "item",
    "changed": false,
    "cmd": "./power_consumption.sh",
    "invocation": {
        "module_args": {
            "_raw_params": "./power_consumption.sh",
            "_uses_shell": false,
            "argv": null,
            "chdir": "/usr/src/",
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true
        }
    },
    "item": "./power_consumption.sh",
    "msg": "[Errno 8] Exec format error: b'./power_consumption.sh'",
    "rc": 8,
    "stderr": "",
    "stderr_lines": [],
    "stdout": "",
    "stdout_lines": []
}


I also create a simple foo.sh and tried to execute it like this, but here also I get errors -
foo.sh contents - 
#!/bin/sh
echo "Hello world"

My TestShell script yml looks like this -

- name: Shell script test
  hosts: all
  tasks:

     - name: Run a shell command and register its output as a variable
       ansible.builtin.shell: /usr/src/foo.sh
       register: foo_result
       ignore_errors: true

     - name: Run a shell command using output of the previous task
       ansible.builtin.shell: /usr/src/bar.sh
       when: foo_result.rc == 5


Output of the above yml gives this error - 

fatal: [132.56.197.46]: FAILED! => {
    "changed": true,
    "cmd": "/usr/src/foo.sh",
    "delta": "0:00:00.002043",
    "end": "2024-08-21 08:32:06.317505",
    "invocation": {
        "module_args": {
            "_raw_params": "/usr/src/foo.sh",
            "_uses_shell": true,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true
        }
    },
    "msg": "non-zero return code",
    "rc": 127,
    "start": "2024-08-21 08:32:06.315462",
    "stderr": "/bin/sh: 1: /usr/src/foo.sh: not found",
    "stderr_lines": [
        "/bin/sh: 1: /usr/src/foo.sh: not found"
    ],
    "stdout": "",
    "stdout_lines": []
}


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

Stephen Maher

unread,
Aug 21, 2024, 4:36:50 AM8/21/24
to 'Caroline Kiel' via Ansible Project
Hi,

This if often because your ’shell’ script has the incorrect shebang at the top. If its a bash script then add executable: /bin/bash and try again, or check your script.

Cheers.



Yogita Patil

unread,
Aug 21, 2024, 6:25:39 AM8/21/24
to ansible...@googlegroups.com
The bash was just a trial, the is the script that I really care about running which runs perfectly from the command line -
myScript.yml

- name: Shell script test
  hosts: all
  tasks:

     - name: Run a shell command
       ansible.builtin.shell: power_consumption.sh
       register: foo_result
       args:
         chdir: /usr/src
       ignore_errors: true

     - name: Run a script
       ansible.builtin.script:
         cmd: /usr/src/power_consumption.sh
       ignore_errors: true

     - name: Run a script in another way
       ansible.builtin.script: /usr/src/power_consumption.sh
       ignore_errors: true

     - name: Run a script via command line
       ansible.builtin.command: ./power_consumption.sh
       args:
         chdir: /usr/src
       ignore_errors: true


#     - name: Run a script second way
#       ansible.builtin.script:
#       cmd: /usr/src/power_consumption.sh


     - name: Run a shell command using output of the previous task
       ansible.builtin.shell: /usr/src/bar.sh
       when: foo_result.rc == 5


The contents of the power_consumption.sh script are - 

hostfqdn=$(hostname --fqdn)
time=1
declare T0=($(cat /sys/class/powercap/intel-rapl/*/energy_uj)); sleep $time; declare T1=($(cat /sys/class/powercap/intel-rapl/*/energy_uj))
for i in "${!T0[@]}"; do echo - | awk "{printf \"%.1f W\", $((${T1[i]}-${T0[i]})) / $time / 1e6 }" >> $hostfqdn.txt ; done


I have no idea what I am doing wrong, I have tried to put the script in /usr/bin as well but same result for all tasks -

TASK [Run a script via command line] *
The full traceback is:
  File "/tmp/ansible_ansible.legacy.command_payload_p_z2bc7h/ansible_ansible.legacy.command_payload.zip/ansible/module_utils/basic.py", line 2050, in run_command

    cmd = subprocess.Popen(args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/subprocess.py", line 1024, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.11/subprocess.py", line 1901, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
fatal: [100.91.187.42]: FAILED! => {

    "changed": false,
    "cmd": "./power_consumption.sh",
    "invocation": {
        "module_args": {
            "_raw_params": "./power_consumption.sh",
            "_uses_shell": false,
            "argv": null,
            "chdir": "/usr/src",

            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true
        }
    },
    "msg": "[Errno 2] No such file or directory: b'./power_consumption.sh'",
    "rc": 2,

    "stderr": "",
    "stderr_lines": [],
    "stdout": "",
    "stdout_lines": []
}

TASK [Run a script in another way] and TASK [Run a script]
The full traceback is:
NoneType: None
fatal: [100.91.187.42]: FAILED! => {
    "changed": true,

    "msg": "non-zero return code",
    "rc": 2,
    "stderr": "Shared connection to 100.91.187.42 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 100.91.187.42 closed."
    ],
    "stdout": "/root/.ansible/tmp/ansible-tmp-1724235551.658032-3341659-128056030829200/power_consumption.sh: 3: Syntax error: \"(\" unexpected\r\n",
    "stdout_lines": [
        "/root/.ansible/tmp/ansible-tmp-1724235551.658032-3341659-128056030829200/power_consumption.sh: 3: Syntax error: \"(\" unexpected"
    ]
}

TASK [Run a shell command 
fatal: [100.91.187.42]: FAILED! => {
    "changed": true,
    "cmd": "power_consumption.sh",
    "delta": "0:00:00.001867",
    "end": "2024-08-21 10:19:11.554061",
    "invocation": {
        "module_args": {
            "_raw_params": "power_consumption.sh",
            "_uses_shell": true,
            "argv": null,
            "chdir": "/usr/src",

            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true
        }
    },
    "msg": "non-zero return code",
    "rc": 127,
    "start": "2024-08-21 10:19:11.552194",
    "stderr": "/bin/sh: 1: power_consumption.sh: not found",
    "stderr_lines": [
        "/bin/sh: 1: power_consumption.sh: not found"
    ],
    "stdout": "",
    "stdout_lines": []
}






Stephen Maher

unread,
Aug 21, 2024, 8:07:25 AM8/21/24
to ansible...@googlegroups.com
Hi,

Your bash script must in include ‘#!/bin/bash’ to allow for its execution using bash hence the suggestion to add the executable verb to prove this was the case.

Regards



Yogita Patil

unread,
Aug 21, 2024, 8:12:49 AM8/21/24
to ansible...@googlegroups.com
Yes yes I understand what you mean, and it does work if I add  #!/bin/bash’  at the beginning of my foo.sh 

contents of foo.sh -
#!/bin/bash
echo "Hello world"

However my power consumption script fails. Any idea what I am doing wrong there?

Many thanks for your help!

-Yogita.

Dick Visser

unread,
Aug 21, 2024, 8:42:03 AM8/21/24
to ansible...@googlegroups.com
On Wed, 21 Aug 2024 at 14:12, Yogita Patil <yogini...@gmail.com> wrote:
>
> Yes yes I understand what you mean, and it does work if I add #!/bin/bash’ at the beginning of my foo.sh

Yes, but your power consumption script ALSO needs to have the correct shebang.

Yogita Patil

unread,
Aug 21, 2024, 8:43:51 AM8/21/24
to ansible...@googlegroups.com
I see, ok let me try that! , it runs via cli without it so that is why I hadn't added it. I will try that, thanks guys!

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

Todd Lewis

unread,
Aug 21, 2024, 8:46:36 AM8/21/24
to ansible...@googlegroups.com, uto...@gmail.com
Yogita,

Do us a favor please and change this:
ansible.builtin.command: ./power_consumption.sh
to this:
ansible.builtin.command: stat ./power_consumption.sh
and let us see the results. Thanks.

Todd

Yogita Patil

unread,
Aug 21, 2024, 8:57:43 AM8/21/24
to ansible...@googlegroups.com
Ok thanks Todd.

Also now I am getting a different error, after I cleaned up the yml to keep only 1 task -

 FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "cmd": [
        "stat",
        "./usr/src/power_consumption.sh"
    ],
    "delta": "0:00:00.002128",
    "end": "2024-08-21 16:53:50.376199",
    "invocation": {
        "module_args": {
            "_raw_params": "stat ./usr/src/power_consumption.sh",
            "_uses_shell": false,
            "argv": null,

            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true
        }
    },
    "msg": "non-zero return code",
    "rc": 1,
    "start": "2024-08-21 16:53:50.374071",
    "stderr": "stat: cannot statx './usr/src/power_consumption.sh': No such file or directory",
    "stderr_lines": [
        "stat: cannot statx './usr/src/power_consumption.sh': No such file or directory"
    ],
    "stdout": "",
    "stdout_lines": []
}


Content of my yml are -

- name: Shell script test
  hosts: all
  gather_facts: false
  tasks:


     - name: Run a script in another way
       ansible.builtin.command: stat ./usr/src/power_consumption.sh
       ignore_errors: true

File does exist in the folder /usr/src on the controller node and the ansible host on which I am trying to execute the script both -

image.png


Contents of yml are -


Yogita Patil

unread,
Aug 21, 2024, 9:03:32 AM8/21/24
to ansible...@googlegroups.com
This worked for me ...hooray!!!  Many thanks everyone for all your inputs and support!!!

- name: Shell script test
  hosts: all
  gather_facts: false
  tasks:

  - name: Start application
    shell: "/usr/src/power_consumption.sh"
    args:
      chdir: "/usr/src"
      executable: "/bin/bash"

Todd Lewis

unread,
Aug 21, 2024, 9:13:13 AM8/21/24
to ansible...@googlegroups.com, uto...@gmail.com
The "stat" failed because you have a "." in front of "/usr/src/power_consumption.sh".

Yogita Patil

unread,
Aug 21, 2024, 9:17:10 AM8/21/24
to ansible...@googlegroups.com
Ah ok thanks for letting me know, will fix that.

Reply all
Reply to author
Forward
0 new messages