使用Ansible删除配置文件中的确切行

时间:2019-06-24 12:33:54

标签: ansible

我想从配置文件中删除一个带有ansible的文本。

- name: Remove Block Of Text
   blockinfile:
     path: /oracle/config/domains/soa_domain/config/config.xml
     block: |
      <file-store>
        <name/>
        <directory/>
        <target>AdminServer</target>
      </file-store>
     state: absent

什么都没发生。希望有人回答。

亲切问候

M

1 个答案:

答案 0 :(得分:0)

- name: Remove Block Of Text
   blockinfile:
     path: /oracle/config/domains/soa_domain/config/config.xml
     marker: ''#{mark} same content you have given while creating block''
     block: |
      <file-store>
        <name/>
        <directory/>
        <target>AdminServer</target>
      </file-store>
     state: absent

注意:marker 是 blockinfile 模块中最重要的参数,用于添加或删除块。实际上marker_begin 和marker_end(marker 中的{mark} 选项)是blockinfile 考虑删除要删除的任何块的标记。因此,无论您想删除什么块,只需在块之前和之后放置 #begin 和 #end 类似于 ansible 在使用 blockinfile 模块在文件中创建块时使用的放置。

例如: 我用这样的 blockinfile 模块创建了块

config:
#BEGIN for template file
- mname: GMLC_NLT
  template: T1
  action: add
  script: change-config.sh
  tte: START/CONFIG/BINARY/LIBRARY/EXECUTE/ANYSCRIPT
#END for template file
- mname: GMLC_NLT
  template: T1
  action: add
  state: RUNNING/STOP
  file_name: ConfigParamSheet.xlsx
  sheet_name: config_sheet
  p_details:
  - p_name: log level
    p_value: INFO
    CLI_Name: loglevel
    EMS_Name: logLevel

任务的样子 - 名称:修改模板文件中的配置 阻止文件: 目标:模板.yaml 备份:是 插入后:“配置:” 标记:“#{mark} 用于模板文件” 内容: | - 姓名:GMLC_NLT 模板:T1 动作:添加 脚本:change-config.sh tte:开始/配置/二进制/库/执行/任意脚本

现在当你必须删除这个块时,你必须使用这个任务

  - name: modify config in template file
    blockinfile:
     state: absent
     path: template.yaml
     marker: "#{mark} for template file"
     content: |
       - mname: GMLC_NLT
         template: T1
         action: add
         script: change-config.sh
         tte: START/CONFIG/BINARY/LIBRARY/EXECUTE/ANYSCRIPT

那么ansible做了什么,它在path中给出的template.yaml文件中搜索#BEGIN寻找模板文件和#END寻找模板文件,并删除带有标记的内容。