Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline strings with leading spaces

How would one specify the multiline strings that have leading spaces on some lines?

If I define a variable as

multiline_str: |
    foo
      bar
        baz

And then write it to a file using

- name: write multiline string
  copy: content="{{ multiline_str }}" dest="/path/to/file"

Then the target file contents is

foo
bar
baz

What is the trick here?

like image 598
zerkms Avatar asked Aug 27 '15 22:08

zerkms


People also ask

What is a multi-line string in Python?

Python multi line strings mean strings that are in more than one line. It is easy to generate strings in a single line but there are special methods to create multi-line strings. The following is an example of a multi-line string in Python.

How do I write a string across multiple lines in YAML?

YAML allows you to write strings across multiple lines in two ways: verbatim and folded. YAML strips are leading indents in these multiline blocks. YAML first concludes the inclusive indentation level inside a multiline block.

How can I postprocess a multiline string?

If you want to postprocess a multiline string to trim out the parts you don't need, you should consider the textwrap module or the technique for postprocessing docstrings presented in PEP 257:

What are the Python indentation rules for multiline strings?

Python indentation rules are not applicable to multiline strings. All the escape sequences such as newline (n), tab-space (t) are considered as a part of string if the multiline string is created using triple quotes.


1 Answers

Try this.

- name: write multiline string
  copy:
    content: "{{ multiline_str }}"
    dest: /path/to/file
like image 122
yaegashi Avatar answered Sep 21 '22 14:09

yaegashi