我有一個檔案confluence.cfg.xml具有以下結構
<?xml version='1.0' encoding='UTF-8'?>
<confluence-configuration>
<setupStep>setupstart</setupStep>
<setupType>custom</setupType>
<buildNumber>0</buildNumber>
<properties>
<property name="confluence.database.choice">postgresql</property>
<property name="confluence.database.connection.type">database-type-standard</property>
<property name="webwork.multipart.saveDir">${localHome}/temp</property>
<property name="attachments.dir">${confluenceHome}/attachments</property>
</properties>
</confluence-configuration>
我想添加和洗掉多個<property>
元素。但是我沒有得到正確的語法。在這里,我試圖洗掉兩個元素(代碼運行但洗掉了每個<property>
元素,而不僅僅是我想要的兩個):
- name: "Remove elements"
xml:
path: "{{ atl_product_home }}/confluence.cfg.xml"
xpath: /confluence-configuration/properties/property
attribute: name
value:
- confluence.database.choice
- confluence.database.connection.type
state: absent
在這里,我試圖添加一個單一的<property name="foo">bar</property>
元素。
- name: "Add elements"
xml:
path: "{{ atl_product_home }}/confluence.cfg.xml"
xpath: /confluence-configuration/properties
add_children:
- property: bar
- property:
name: foo
上面給了我兩個元素(<property>bar</property>
和<property name="foo"/>
),但我似乎無法同時指定兩者。一旦我可以正確指定一個元素,我希望能夠添加它們的串列。
uj5u.com熱心網友回復:
為了洗掉節點,您需要有正確的 XPath,value
不會考慮,此引數適用于state: present
.
也就是說,XPath 支持通過屬性選擇節點,也支持or
運算式,因此,洗掉節點的任務可以寫成:
- name: Remove elements
xml:
path: "{{ atl_product_home }}/confluence.cfg.xml"
xpath: >-
/confluence-configuration
/properties
/property[
@name='confluence.database.choice'
or @name='confluence.database.connection.type'
]
state: absent
關于節點的添加,再一次,您的屬性可以是 XPath 的一部分,如果此 XPath 不存在,那么 Ansible 將創建它并分配所需的值:
- name: Add <property name="foo">bar</property>
xml:
path: "{{ atl_product_home }}/confluence.cfg.xml"
xpath: /confluence-configuration/properties/property[@name='foo']
value: bar
鑒于劇本
- hosts: localhost
gather_facts: no
vars:
atl_product_home: .
xml: |-
<?xml version='1.0' encoding='UTF-8'?>
<confluence-configuration>
<setupStep>setupstart</setupStep>
<setupType>custom</setupType>
<buildNumber>0</buildNumber>
<properties>
<property name="confluence.database.choice">
postgresql
</property>
<property name="confluence.database.connection.type">
database-type-standard
</property>
<property name="webwork.multipart.saveDir">
${localHome}/temp
</property>
<property name="attachments.dir">
${confluenceHome}/attachments
</property>
</properties>
</confluence-configuration>
tasks:
- name: Create configuration file
copy:
content: "{{ xml }}"
dest: "{{ atl_product_home }}/confluence.cfg.xml"
- name: Remove elements
xml:
path: "{{ atl_product_home }}/confluence.cfg.xml"
xpath: >-
/confluence-configuration
/properties
/property[
@name='confluence.database.choice'
or @name='confluence.database.connection.type'
]
state: absent
- name: Add <property name="foo">bar</property>
xml:
path: "{{ atl_product_home }}/confluence.cfg.xml"
xpath: /confluence-configuration/properties/property[@name='foo']
value: bar
- debug:
var: lookup('file', atl_product_home ~ '/confluence.cfg.xml')
這產生:
PLAY [localhost] **************************************************************
TASK [Create configuration file] **********************************************
changed: [localhost]
TASK [Remove elements] ********************************************************
changed: [localhost]
TASK [Add <property name="foo">bar</property>] ********************************
changed: [localhost]
TASK [debug] ******************************************************************
ok: [localhost] =>
lookup('file', atl_product_home ~ '/confluence.cfg.xml'): |-
<?xml version='1.0' encoding='UTF-8'?>
<confluence-configuration>
<setupStep>setupstart</setupStep>
<setupType>custom</setupType>
<buildNumber>0</buildNumber>
<properties>
<property name="webwork.multipart.saveDir">
${localHome}/temp
</property>
<property name="attachments.dir">
${confluenceHome}/attachments
</property>
<property name="foo">bar</property></properties>
</confluence-configuration>
PLAY RECAP ********************************************************************
localhost : ok=4 changed=3
uj5u.com熱心網友回復:
謝謝你,β.εηοιτ.βε!
唯一缺少的是一次添加多個元素的能力。以您為例,我的解決方案是:
- name: "Remove elements from {{ atl_product_home }}/confluence.cfg.xml"
xml:
path: "{{ atl_product_home }}/confluence.cfg.xml"
xpath: >-
/confluence-configuration
/properties
/property[
@name='confluence.database.choice'
or @name='confluence.database.connection.type'
]
state: absent
- name: "Add elements to {{ atl_product_home }}/confluence.cfg.xml"
xml:
path: "{{ atl_product_home }}/confluence.cfg.xml"
xpath: "/confluence-configuration/properties/property[@name='{{ item.attribute }}']"
value: "{{ item.value }}"
pretty_print: true
loop:
- { attribute: 'access.mode', value: 'READ_WRITE' }
- { attribute: 'admin.ui.allow.daily.backup.custom.location', value: 'false' }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/489243.html
上一篇:XSLT1.0-帶有選擇變數的for-each沒有給出正確的結果
下一篇:如何序列化完整串列而不是逐行