How to add spaces at beginning of block in Ansible's blockinfile?

百般思念 提交于 2019-12-12 07:44:41

问题


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

I expect

  authc:
    line0
      line1
      line2
      line3
        line4

but get

  authc:
line0
  line1
  line2
  line3
    line4

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


回答1:


How can I accomplish this?

Refer to this answer

Generally it's more canonical to use template files.




回答2:


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

It's all about the 2 after the |

References:

  • https://groups.google.com/forum/#!topic/ansible-project/mmXvhTh6Omo
  • In YAML, how do I break a string over multiple lines?
  • http://www.yaml.org/spec/1.2/spec.html#id2793979


来源:https://stackoverflow.com/questions/39731999/how-to-add-spaces-at-beginning-of-block-in-ansibles-blockinfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!