How to add spaces at the beginning of a block in Ansible blockinfile?

I found this blockinfile problem where the user suggested adding a number after "|" in "block: |" line, but gives a syntax error. Basically, I want to use the blockinfile module to add a block of lines to a file, but I want the block to be indented with 6 spaces in the file. Here is 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 to the beginning of lines is not performed. How can i do this?

+6
source share
2 answers

How can i do this?

Refer to this answer

It is generally more canonical to use template files.

0
source

You can use the YAML function called "Block Indent 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 2 after |

Literature:

+12
source

All Articles