如何在 Ansible 中通过文字块标量打破字符串

问题描述 投票:0回答:1

我在 yaml 脚本中有一个这样的变量:

variable: |
  This is really long long line ---------------------------------------------------------------------------
  And this line is not really long ---------
  And i am just chilling here

我想通过 yamllint 验证我的所有脚本,所以我必须将第一行分成两部分而不更改其内容。 我该怎么做?

如果我尝试从字面上定义它,我会得到这样的结果:

variable: "\
  This is really long long line ---------------------\
  ------------------------------------------------------\n
  And this line is not really long ---------\n
  And i am just chilling here"

我得到这样的信息(注意第二两行中的空格):

  This is really long long line ---------------------------------------------------------------------------
   And this line is not really long ---------
   And i am just chilling here

其他选项根本不起作用。在这种情况下我如何遵守 yamllint 规则?

ansible yaml multiline
1个回答
0
投票

您看到此行为是因为您显式添加换行符,然后为文本的其余部分添加回车符。

您可以通过多种方式解决这个问题:

  1. 在显式换行符之前添加回车符: variable: "\ This is really long long line ---------------------\ ------------------------------------------------------ \nAnd this line is not really long --------- \nAnd i am just chilling here"
    
    
  2. 使用空行添加换行符:
  3. variable: "\ This is really long long line ---------------------\ ------------------------------------------------------ And this line is not really long --------- And i am just chilling here"
    
    
  4. 使用 Jinja 注释来
  5. 控制空格variable: | This is really long long line ---------------------{#- -#} ------------------------------------------------------ And this line is not really long --------- And i am just chilling here
    
    
以上所有内容都会给您带来预期的结果:

This is really long long line --------------------------------------------------------------------------- And this line is not really long --------- And i am just chilling here
    
© www.soinside.com 2019 - 2024. All rights reserved.