使用 Ansible Playbook 修改 XML 文件中的数据不起作用 [英] Modify data in a XML file with Ansible Playbook is not working

查看:18
本文介绍了使用 Ansible Playbook 修改 XML 文件中的数据不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 文件,我需要在其中修改真实的用户名和密码而不是默认值.我试图编写一个小而简单的剧本来完成这项工作.但是它没有替换默认密码,而是在文件末尾添加了一个新行.这是我的 XML 文件和剧本,请注意.

I have an XML file where I need to modify the real username and password instead of default. I have tried to write a small and simple playbook script to do the job. But instead of replacing the default password, it added a new line at the end of the file. Here are my XML file and playbook for your kind attention.

<?xml version='1.0' encoding='utf-8'?>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<Resources cacheMaxSize="100000" />
<Resource name="DataSource" 
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="org.postgresql.Driver"
        url="jdbc:postgresql://localhost:5432/dbNa?ssl=true&amp;sslmode=prefer&amp;protocolVersion=3"
        username="${database.user}"
        password="${database.password}"
        description="crmEpDataSource"
        initialSize="1"
        maxActive="30"
        maxIdle="10"
        minIdle="1"
        minEvictableIdleTimeMillis="10000"
        testOnBorrow="true"
        validationQuery="select 1"
        validationInterval="5000"/>
 </Context>

剧本是这样的

---
- name: Creating/modifying file and start the application
hosts: all
become: true

tasks:
- name: Change username on xml file
  lineinfile:
    path: "/opt/ansible/context.xml"
    regexp: 'username="${database.user}"'
    line: 'username="tamim"'
    state: present
    backup: yes

实际上我想替换${database.user}";与tamim".请建议.

actually i want replace "${database.user}" with "tamim". Kindly suggest.

推荐答案

lineinfile 模块在这种情况下可能是不够的.正如 seshadri-c 所建议的,我会尝试这样的事情:

The lineinfile module is probably not enough in that case. As suggested by seshadri-c, I would try something like that:

---
tasks:
- name: Change username on xml file
  xml:
    path: "/opt/ansible/context.xml"
    xpath: //Resource
    attribute: username
    value: "tamim"
    state: present

请注意,您的 XLM 文件格式不正确,缺少根节点和开头的 标记.我以这种方式对其进行了修改以使其正常工作:

Note that your XLM file is not well formed, it was missing a root node and the opening <Context> tag. I modified it this way to make it work:

<?xml version='1.0' encoding='utf-8'?>
<Context>
  <WatchedResource>WEB-INF/web.xml
  </WatchedResource>
  <WatchedResource>${catalina.base}/conf/web.xml
  </WatchedResource>
    <Resources cacheMaxSize="100000" />
    <Resource name="DataSource"
            auth="Container"
            type="javax.sql.DataSource"
            driverClassName="org.postgresql.Driver"
            url="jdbc:postgresql://localhost:5432/dbNa?ssl=true&amp;sslmode=prefer&amp;protocolVersion=3"
            username="${database.user}"
            password="${database.password}"
            description="crmEpDataSource"
            initialSize="1"
            maxActive="30"
            maxIdle="10"
            minIdle="1"
            minEvictableIdleTimeMillis="10000"
            testOnBorrow="true"
            validationQuery="select 1"
            validationInterval="5000"/>
 </Context>

这篇关于使用 Ansible Playbook 修改 XML 文件中的数据不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆