问题描述
背景:
这是使用模板从模板构造yaml文件的Ansible剧本.因此,基本上,我有一个jinja2模板文件,其内容如下:
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使用yaml定义变量.因此,我将像这样填写 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-----"
但是输出会减少缩进:
private_key:
- "-----BEGIN PRIVATE KEY-----"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "zzzzzzzzzzzzzzzzzz="
- "-----END PRIVATE KEY-----"
由于输出文件本身是Yaml,因此我需要保留缩进.看来无论如何我都会缩进.
private_key:
- "-----BEGIN PRIVATE KEY-----"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "asdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdfasdfssadf"
- "zzzzzzzzzzzzzzzzzz="
- "-----END PRIVATE KEY-----"
推荐答案
我在发布问题后立即在Google搜索中找到了答案.
I found the answer in a Google search right after posting the question.
基本上,yaml字符串将去除缩进,因此在这种情况下,我们必须使用Jinja在要去除它们的位置插入空格.幸运的是,这很容易做到:
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 }}
与此:
private_key: {{ myvar | indent( width=4, indentfirst=True) }}
width
字段可以根据需要缩进多少空格进行调整.
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-----"
这篇关于保留缩进和换行符时的YAML多行字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!