保留缩进和换行符的

保留缩进和换行符的

本文介绍了保留缩进和换行符的 YAML 多行字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

这是一个 Ansible 剧本,使用模板从模板构建 yaml 文件.所以基本上我有一个带有这样一行的 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 多行字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:41