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”:
}