【发布时间】: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&sslmode=prefer&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