首页 > 解决方案 > 使用ansible替换xml文件中的一行

问题描述

请在这里为我提供指导。

我想在一个配置文件中注释一行,并且需要使用 ansible 取消注释另一行。

我的文件内容如下。


    `<include location="conf/basicUserRegistry.xml"/>`  

          this i want to comment 

`<!--include location="conf/ldapUserRegistry.xml"/-->`

         this line i want to uncomment

我尝试使用下面的剧本,即使它没有更改对文件的任何更改,它也没有显示任何错误。

- name: enable LDAP
  hosts: localhost
  tasks:
   - name: disbale basic user-registry
     lineinfile:
       path: /opt/IBM/605CLM/JazzTeamServer/server/liberty/servers/clm/server.xml
       regexp: '^\s*<include location="conf/basicUserRegistry.xml">.*$'
       line: '<!--include location="conf/basicUserRegistry.xml"-->;'
       backrefs: yes
     become: true

- name: enable LDAP USER_REGISTRY
  hosts: localhost
  tasks:
    lineinfile:
      dest: /opt/IBM/605CLM/JazzTeamServer/server/liberty/servers/clm/server.xml
      regexp: '^(.*)<!--include location="conf/ldapUserRegistry.xml"-->(.*)$'
      line: '<include location="conf/ldapUserRegistry.xml">;'
      backrefs: yes

输出:

播放 [启用 LDAP] ********************************************* ****************************************************** *******************************************

任务[收集事实] ************************************************ ****************************************************** ****************************************** 好的:[本地主机]

任务 [禁用基本用户注册表] ****************************************** ****************************************************** ****************************** 好的:[本地主机]

播放回顾 ************************************************ ****************************************************** ************************************************ 本地主机:确定=2 更改=0 无法访问=0 失败=0

标签: ansible

解决方案


可以说解决这个问题的更清洁的方法是使用Ansiblexml模块

示例(假设您的示例显示了完整的文件):

tasks:
  - xml:
      file: server.xml
      xpath: /include
      attribute: location
      value: conf/ldapUserRegistry.xml

推荐阅读