我item
在一個元素中有多個元素,如下所示/path/to/content/
:
<item>
<label>SOME_TEXT</label>
<other_elements/>
</item>
使用 ansible 的community.general.xml
模塊如下允許我洗掉所有現有item
元素:
- name: "Remove all items"
become: true
community.general.xml:
path: "{{ path_to_my_xml_file }}"
xpath: /path/to/content/*
pretty_print: yes
state: absent
如何使用community.general.xml
ansible 模塊僅洗掉item
who 的label
值(即SOME_TEXT
)匹配字串label_of_item_to_remove
?
如果不可能使用community.general.xml
其他模塊來實作這一點?
uj5u.com熱心網友回復:
問:“洗掉標簽值(即 SOME_TEXT)與字串 label_of_item_to_remove 匹配的專案”
是的。有可能的。例如,給定檔案
shell> cat /tmp/test.xml
<?xml version='1.0' encoding='UTF-8'?>
<doc>
<item>
<label>SOME_TEXT</label>
<other_elements/>
</item>
<item>
<label>OTHER_TEXT</label>
<other_elements/>
</item>
</doc>
下面的任務將洗掉第一項
- community.general.xml:
path: /tmp/test.xml
xpath: "/doc/item[label='{{ label_of_item_to_remove }}']"
state: absent
vars:
label_of_item_to_remove: SOME_TEXT
運行帶有選項--check --diff顯示的游戲
TASK [community.general.xml] *********************************************
--- before
after
@@ -1,9 1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<doc>
-<item>
- <label>SOME_TEXT</label>
- <other_elements/>
-</item>
<item>
<label>OTHER_TEXT</label>
<other_elements/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/493450.html