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

时间:2019-07-24 13:05:02

标签: ansible

我想强制将特定文件中的文本块从“全局”替换为“ / 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...

您知道如何执行此操作吗?

1 个答案:

答案 0 :(得分:0)

看看blockinfile module

  

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

您将以如下方式使用它:

- 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