我正在使用 Ansible 的blockinfile
模块在文件中的特定行之间添加一行代码。该代码块应缩进 8 个空格。但是,当我运行剧本时,这些行会插入到行的开头(没有缩进),忽略我在代码块中添加的空格。这是我正在使用的剧本:
- name: Add lines between specific lines with 8 spaces of indentation
hosts: all
tasks:
- name: Add lines
ansible.builtin.blockinfile:
path: /path/to/my/file
marker: "# {mark} Do not edit manually"
insertbefore: '^\s*the line that exists'
block: |
# first line
second line
预期行为:应像这样插入块,并缩进 8 个空格:
# BEGIN ANSIBLE MANAGED BLOCK
# first line
second line
# END ANSIBLE MANAGED BLOCK
实际行为:块插入时没有缩进:
# BEGIN ANSIBLE MANAGED BLOCK
# first line
second line
# END ANSIBLE MANAGED BLOCK
我尝试直接在块中添加空格,但它们被忽略了。有没有办法让blockinfile
插入的块尊重缩进?