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-----"
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.
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.
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.
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.
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-----"
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