如何在 Ansible 的 blockinfile 中的块开头添加空格? [英] How to add spaces at beginning of block in Ansible's blockinfile?

查看:30
本文介绍了如何在 Ansible 的 blockinfile 中的块开头添加空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了这个 blockinfile 问题,其中一位用户建议添加一个|"后面的数字在块:|"行,但给出了语法错误.基本上,我想使用 blockinfile 模块在文件中添加一个行块,但我希望该块在文件中缩进 6 个空格.任务来了

I found this blockinfile issue, where a user suggested adding a number after the "|" in the "block: |" line, but gives a syntax error. Basically, I want to use blockinfile module to add a block of lines in a file, but I want the block to be indented 6 spaces in the file. Here's the task

- name: Added a block of lines in the file
  blockinfile:
  dest: /path/some_file.yml
  insertafter: 'authc:'
  block: |
    line0
      line1
      line2
      line3
        line4

我期待

  authc:
    line0
      line1
      line2
      line3
        line4

但是得到

  authc:
line0
  line1
  line2
  line3
    line4

在行首添加空格不会这样做.我怎样才能做到这一点?

Adding spaces in the beginning of the lines does not do it. How can I accomplish this?

推荐答案

您可以使用名为Block Indentation Indicator"的 YAML 功能:

You can use a YAML feature called "Block Indentation Indicator":

- name: Added a block of lines in the file
  blockinfile:
  dest: /path/some_file.yml
  insertafter: 'authc:'
  block: |2
      line0
        line1
        line2
        line3
          line4

都是关于|

参考文献:

这篇关于如何在 Ansible 的 blockinfile 中的块开头添加空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆