Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YAML Multiline String While Retaining Indentation and Newlines

Background:

This is an Ansible playbook using templates to CONSTRUCT a yaml file from a template. So basically I have a jinja2 template file with a line as such:

private_key: {{ myvar }}

Ansible uses yaml to define the variables. So I will fill in the myvar value something like this. Here I am using the | special character to define a multiline string:

myvar: |

        - "-----BEGIN PRIVATE KEY-----"
        - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
        - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
        - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
        - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
        - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
        - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
        - "zzzzzzzzzzzzzzzzzz="
        - "-----END PRIVATE KEY-----"

However the output trims off the indentation:

  private_key:
- "-----BEGIN PRIVATE KEY-----"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "zzzzzzzzzzzzzzzzzz="
- "-----END PRIVATE KEY-----"

Since the output file is a yaml itself, I need to retain the indentation. It seems no matter what I'll lose the indent.

I need the end result to look EXACTLY like this:

  private_key:
    - "-----BEGIN PRIVATE KEY-----"
    - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
    - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
    - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
    - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
    - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
    - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
    - "zzzzzzzzzzzzzzzzzz="
    - "-----END PRIVATE KEY-----"
like image 480
emmdee Avatar asked Mar 29 '19 05:03

emmdee


People also ask

Does YAML support multiline string?

Multi-line blocks and long strings. YAML also supports writing strings on multiple lines in your YAML, with two different styles: literal and folded. Those are represented by a | (for literal style) or > (for folded style) header line (see examples below), and we will go into their differences soon.

How do I break a string in YAML over multiple lines?

Use > most of the time: interior line breaks are stripped out, although you get one at the end: key: > Your long string here. Use | if you want those linebreaks to be preserved as \n (for instance, embedded markdown with paragraphs). Use >- or |- instead if you don't want a linebreak appended at the end.

Which allows breaking long lines in YAML?

Literal Style The literal operator is represented by the pipe (“|”) symbol. It keeps our line breaks but reduces empty lines at the end of the string down to a single line break. Next, we'll talk about block chomping and how it gives us more control over starting and ending line breaks.

Does YAML ignore whitespace?

In YAML, you can use single-quoted strings to prevent the YAML parser from interpreting special characters. This can be helpful when you want to include a character like an apostrophe in your string. Leave a blank line to add new lines. Leading whitespace on lines is ignored.


1 Answers

I found the answer in a Google search right after posting the question.

Essentially the yaml string will strip indents, so in this case we have to use Jinja to insert spaces where they were stripped. Luckily this is super easy to do:

In the template file, I replaced this:

private_key: {{ myvar }}

With this:

private_key: {{ myvar | indent( width=4, indentfirst=True) }}

The width field can be adjusted for how many spaces of indentation are needed.

The actual variable declaration is done exactly like I posted in the question. However now with the indent added in the template, I now have the desired output with indentation:

  private_key:
    - "-----BEGIN PRIVATE KEY-----"
    - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
    - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
    - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
    - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
    - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
    - "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
    - "zzzzzzzzzzzzzzzzzz="
    - "-----END PRIVATE KEY-----"
like image 81
emmdee Avatar answered Oct 22 '22 13:10

emmdee