【问题标题】:Modify data in a XML file with Ansible Playbook is not working使用 Ansible Playbook 修改 XML 文件中的数据不起作用
【发布时间】:2021-03-18 12:59:11
【问题描述】:

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

<?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

实际上我想用“tamim”替换“${database.user}”。请建议。

【问题讨论】:

  • 我认为regexp 不匹配。这就是context.xml 中的全部内容吗?
  • @seshadri_c 你认为 lininfile 模块可以在 xml 文件中工作吗?或者我需要 xml 模块?但我不知道如何使用 xml 模块!我的任务是替换这个 xml 文件中的用户名......它有 3 个类似的部分(我只放了一部分) Ply help
  • 如果有多个带有username 的XML 部分,那么lineinfile 将不起作用。如果只有 1 次出现,它将起作用。
  • @seshadri_c 我已经尝试过了,但只工作了一部分......任务:-名称:更改 context.xml 文件 xml 上的用户名:路径:“/opt/ansible/context.xml” xpath:/Context/Resource[1] 属性:用户名值:“tamim”属性:密码值:“123456789” 它有效,但仅适用于一个孩子(资源 [1])......我如何将它用于孩子 2 和3
  • 那么你必须使用xml module。查看示例以获取想法。

标签: xml xpath automation ansible yaml


【解决方案1】:

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

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

请注意,您的 XLM 文件格式不正确,它缺少根节点和打开的 &lt;Context&gt; 标记。我以这种方式对其进行了修改以使其工作:

<?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>

【讨论】:

    猜你喜欢
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多