簡體   English   中英

使用 Ansible Playbook 修改 XML 文件中的數據不起作用

[英]Modify data in a XML file with Ansible Playbook is not working

我有一個 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}”。 請建議。

在這種情況下, 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 文件格式不正確,它缺少根節點和開始<Context>標記。 我以這種方式修改它以使其工作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM