Ansible 替换特定文件中的文本块

问题描述 投票:0回答:1

我想强制将特定文件中的文本块从“global”替换为“/global”。

例如: 来自:

SOME DATA...
SOME DATA...
SOME DATA...
<global>
 <white_list>127.0.0.1</white_list>
 <white_list>::1</white_list>
 <white_list>^localhost.localdomain$</white_list>
</global>
SOME DATA...
SOME DATA...
SOME DATA...

致:

SOME DATA...
SOME DATA...
SOME DATA...
<global>
 <white_list>127.0.0.1</white_list>
 <white_list>::1</white_list>
 <white_list>^localhost.localdomain$</white_list>
 <white_list>{{ white_root_vpn }}</white_list>
 <white_list>{{ white_static1 }}</white_list>
</global>
SOME DATA...
SOME DATA...
SOME DATA...

你知道如何做到这一点吗?

ansible
1个回答
2
投票

看看 blockinfile 模块

此模块将插入/更新/删除由可自定义标记线包围的多行文本块。

你会这样使用它:

- ansible.builtin.blockinfile:
    path: /path/to/file
    marker: "{mark}"
    marker_begin: "<global>"
    marker_end: "</global>"
    block: |-
      <white_list>127.0.0.1</white_list>
      <white_list>::1</white_list>
      <white_list>^localhost.localdomain$</white_list>
      <white_list>{{ white_root_vpn }}</white_list>
      <white_list>{{ white_static1 }}</white_list>
    state: present
© www.soinside.com 2019 - 2024. All rights reserved.