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

6,635 views
Skip to first unread message

AJ Daws

unread,
Sep 12, 2014, 8:55:43 PM9/12/14
to ansible...@googlegroups.com
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?

James Cammarata

unread,
Sep 15, 2014, 5:32:43 PM9/15/14
to ansible...@googlegroups.com
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!

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ea53aba8-c5c4-449b-875d-8440606426bc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

AJ Daws

unread,
Sep 15, 2014, 6:51:07 PM9/15/14
to ansible...@googlegroups.com
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..."

Michael DeHaan

unread,
Sep 15, 2014, 6:58:13 PM9/15/14
to ansible...@googlegroups.com
That's because they are escaped newlines, versus real newlines, I'm suspecting.



Michael DeHaan

unread,
Sep 15, 2014, 6:59:29 PM9/15/14
to ansible...@googlegroups.com
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.

AJ Daws

unread,
Sep 15, 2014, 7:08:08 PM9/15/14
to ansible...@googlegroups.com
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"
}


Toshio Kuratomi

unread,
Sep 15, 2014, 7:59:47 PM9/15/14
to ansible...@googlegroups.com
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


AJ Daws

unread,
Sep 15, 2014, 8:27:27 PM9/15/14
to ansible...@googlegroups.com
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 ...

Anatoli Barski

unread,
Apr 20, 2018, 7:41:02 AM4/20/18
to Ansible Project
All those quotes are ugly... Would love to have something a debug module option for this.
Reply all
Reply to author
Forward
0 new messages