在 ansible 块中的每一行前面添加 space

Adding space in front of each line in ansible block

我想在一个文件中添加一个块,前面有 space。我使用的 Ansible 脚本如下。

- name: Disable Apache Directory listing, Symbolic Links, Server side includes and CGI execution 
  blockinfile:
    dest: /etc/apache2/sites-enabled/000-default.conf
    insertbefore: '</VirtualHost>'
    block: |
      Options -Indexes -FollowSymLinks  -Includes -ExecCGI
      LimitRequestBody  10485760

但我得到的输出如下:

<VirtualHost *:80>
# BEGIN ANSIBLE MANAGED BLOCK
Options -Indexes -FollowSymLinks  -Includes -ExecCGI
LimitRequestBody  10485760
# END ANSIBLE MANAGED BLOCK
</VirtualHost>

我期待的是:

<VirtualHost *:80>
    # BEGIN ANSIBLE MANAGED BLOCK
    Options -Indexes -FollowSymLinks  -Includes -ExecCGI
    LimitRequestBody  10485760
    # END ANSIBLE MANAGED BLOCK
</VirtualHost>

我们不能像在 line

中那样在 blockinfile 代码前面使用 space
line: '        Options -Indexes -FollowSymLinks  -Includes -ExecCGI'

大家有什么想法可以做吗?

使用 yaml 块缩进指示器:

- name: Disable Apache Directory listing, Symbolic Links, Server side includes and CGI execution 
  blockinfile:
    dest: testfile.conf
    insertbefore: '</VirtualHost>'
    block: |4
            Options -Indexes -FollowSymLinks  -Includes -ExecCGI
            LimitRequestBody  10485760

这将得到:

<VirtualHost *:80>
# BEGIN ANSIBLE MANAGED BLOCK
    Options -Indexes -FollowSymLinks  -Includes -ExecCGI
    LimitRequestBody  10485760 
# END ANSIBLE MANAGED BLOCK
</VirtualHost>

请注意,块中的缩进对于此功能的正常工作很重要。

https://yaml.org/spec/1.2/spec.html#id2793979