I want to check the multi-line command executed in the shell module in the subsequent debug module with line breaks.
I couldn't find a suitable one in the filter, but it is possible with splitlines () in python
playbook.yml
---
- hosts: all
gather_facts: false
tasks:
- shell: |
echo a
echo b
echo c
register: r
- debug: { var: r.cmd } # <=Hard to read if output as it is
- debug: { var: r.cmd.splitlines() } # <= splitlines()Separated at
- debug: { var: r.stdout_lines }
- debug: { var: r.stderr_lines }
$ ansible-playbook -i localhost, -c local playbook.yml
PLAY [all] *******************************************************************************************************
TASK [shell] *****************************************************************************************************
changed: [localhost]
TASK [debug] *****************************************************************************************************
ok: [localhost] => {
"r.cmd": "echo a\n echo b\necho c\n" # <=Example of output as it is
}
TASK [debug] *****************************************************************************************************
ok: [localhost] => {
"r.cmd.splitlines()": [
"echo a", # <=Example of output separated by lines
" echo b", # <=Example of output separated by lines
"echo c" # <=Example of output separated by lines
]
}
TASK [debug] *****************************************************************************************************
ok: [localhost] => {
"r.stdout_lines": [
"a",
"b",
"c"
]
}
TASK [debug] *****************************************************************************************************
ok: [localhost] => {
"r.stderr_lines": []
}
PLAY RECAP *******************************************************************************************************
localhost : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Note that the command module (not the shell module) cannot be used because it is not passed as a string to the registry variable. It would be nice if it could be displayed separately by line even when it fails.
Recommended Posts