I'm having issues to get my command output when task timeout is reached.
Here is my task:
- name: test of echo-ing something
ansible.builtin.command:
cmd: "/bin/echo 'it works !'"
timeout: 10
register: command_result
ignore_errors: yes
- name: command_result
ansible.builtin.debug:
var: command_result
When there is no timeout, everything goes well, I have in command_result variable:
ok: [server1] => {
"command_result": {
"changed": true,
"cmd": [
"/bin/echo",
"it works !"
],
"delta": "0:00:00.004629",
"end": "2022-03-24 09:47:12.104234",
"failed": false,
"msg": "",
"rc": 0,
"start": "2022-03-24 09:47:12.099605",
"stderr": "",
"stderr_lines": [],
"stdout": "it works !",
"stdout_lines": [
"it works !"
]
}
}
However, when I make my command triggering the timeout (for example: cmd: "/bin/sleep 40"), I have this :
TASK [test of echo-ing something] ***************************************
fatal: [server1]: FAILED! => {"changed": false, "msg": "The ansible.builtin.command action failed to execute in the expected time frame (10) and was terminated"}
...ignoring
TASK [command_result] ***************************************************
ok: [server1] => {
"command_result": {
"changed": false,
"failed": true,
"msg": "The ansible.builtin.command action failed to execute in the expected time frame (10) and was terminated"
}
}
How to get standard output (stdout) and/or standard error (stderr) from the command when timeout is reached ? On my CI/CD, I need it to know why a command had reached the timeout
Thanks !
[EDIT] Regarding the answer given by @vladimir-botka I found another way to timeout command and not tasks. I'm pasting it here just in case it helps other people:
- name: "exec my command"
ansible.builtin.shell:
cmd: "timeout -v --foreground --signal=SIGINT 600 my_script.sh"
chdir: "{{ work_dir }}"
executable: /bin/bash
register: cmd_output
- name: "command output"
ansible.builtin.debug:
var: cmd_output