Ansible: Shows multi-line commands executed in the shell module with line breaks

Request

I want to check the multi-line command executed in the shell module in the subsequent debug module with line breaks.

solution

I couldn't find a suitable one in the filter, but it is possible with splitlines () in python

playbook example

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 }

Execution example

$ 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

Ansible: Shows multi-line commands executed in the shell module with line breaks
Look up the URL in sitemap.xml without too big line breaks with Linux commands
Make the line breaks visible in journalctrl
How to run the Ansible module added in Ansible Tower
Load the module with the same name in another location
Output a character string with line breaks in PyYAML
[Jinja2] Changes in line breaks depending on the hyphen position
Process the files in the folder in order with a shell script
[Python] Create a program to delete line breaks in the clipboard + Register as a shortcut with windows
Run shell commands in python
Process the contents of the file in order with a shell script