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": []
}


On Mon, Aug 19, 2024 at 5:47 PM alex...@gmail.com <alexs...@gmail.com>
wrote:

>
> Create a var for script i.e.: script_path="/some/path/script" and use that
> instead of hard coding path
> On Sunday, August 18, 2024 at 7:14:58 AM UTC-4 Yogita Patil wrote:
>
>> 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.
>>
> --
> 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-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/8da4e506-2504-4724-b4e9-c1d45c61800dn%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/8da4e506-2504-4724-b4e9-c1d45c61800dn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAC15M2K3t7%2BtseL5gi%2BX_nE%3D3JtsBjfHU8rdxO48puT_m%2BgV8A%40mail.gmail.com.

Reply via email to