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?
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.
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.
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:
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.
Try this.
- name: write multiline string
copy:
content: "{{ multiline_str }}"
dest: /path/to/file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With