Output multi-line string as-is (not JSON format)

I have a large, multi-line string that I would like to output in a role. Currently I’m using the debug module like this:

`

  • name: Cloud-init user_data
    debug:
    msg: “{{ cloudinit_user_data|indent(6) }}”

`

However this generates JSON output which is quite hard to read as newlines are represented as “\n” rather than an actual new line. Is there a simple way to simply output a string as-is?

Hi AJ,

Using “- debug: var=cloudinit_user_data” will show you the pretty-printed version of the var, without any escaped values, though you won’t be able to control the indentation of that data.

Thanks!

Unfortunately that still shows escaped newline characters so isn’t any more readable. For example:

TASK: [ec2-launcher-v2 | debug var=cloudinit_user_data] *********************** ok: [localhost] => { "cloudinit_user_data": "#cloud-config\nusers:\n - name: core\n ssh-authorized-keys:\n - ssh-rsa AAAA..."

That’s because they are escaped newlines, versus real newlines, I’m suspecting.

Let me take that back - it’s because it’s JSON. It would not be valid JSON if it were not.

Sorry, this is what you’re pretty much going to have here, unless we teach debug to have an format=yaml option or something, which I’d be ok with.

But even then, YAML needs hints and might get it wrong.

To simplify out the fact that my multi-line string was YAML, how about this example:

Example string:

`

multi_line_string: |
Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo
consequat.
Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.

`

Task:

`

  • name: Cloud-init user_data
    debug: var=multi_line_string

`

Output:

`

ok: [localhost] => {
“multi_line_string”: “Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt\nut labore et dolore magna aliqua. Ut enim\nad minim veniam, quis nostrud exercitation\nullamco laboris nisi ut aliquip ex ea commodo\nconsequat.\nDuis aute irure dolor in reprehenderit in\nvoluptate velit esse cillum dolore eu fugiat nulla\npariatur. Excepteur sint occaecat cupidatat non proident,\nsunt in culpa qui officia deserunt mollit anim id est laborum.\n”
}

`

If all you want is to get your debug to print something somewhat human readable, perhaps this will work:

Task:

  • name: Cloud-init user_data

debug: var=multi_line_string.split(‘\n’)

-Toshio

Well, it’s a bit hacky but it gives the best result so far, and that’ll do. Thanks!

`

ok: [localhost] => {
“multi_line_string.split(‘\n’)”: [
“Lorem ipsum dolor sit amet,”,
“consectetur adipiscing elit,”,
“sed do eiusmod tempor incididunt”,
“ut labore et dolore magna aliqua. Ut enim”,
“ad minim veniam, quis nostrud exercitation”,
“ullamco laboris nisi ut aliquip ex ea commodo”,
“consequat.”,
“Duis aute irure dolor in reprehenderit in”,
“voluptate velit esse cillum dolore eu fugiat nulla”,
“pariatur. Excepteur sint occaecat cupidatat non proident,”,
“sunt in culpa qui officia deserunt mollit anim id est laborum.”,
“”
]
}

`

`

ok: [localhost] => {
“cloudinit_user_data.split(‘\n’)”: [
#cloud-config”,
“users:”,
" - name: core",
" ssh-authorized-keys:",
" - ssh-rsa …

`

All those quotes are ugly… Would love to have something a debug module option for this.