I am attempting to edit configuration files for various servers using Ansible and have run into an XML document that has multiple attributes with the same name where the value needs to be adjusted depending on the server state. The config file shrunken down looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<properties>
<property name="daily.backup.filename">test.data.thin</property>
<property name="admin.ui.allow.manual.backup.download">false</property>
<property name="admin.ui.allow">false</property>
</properties>
</Document>
This config file would have over 50 properties with different names. My aim is to adjust the data within that single property without using lineinfile module.
Currently, I am able to read the specific value I need but the value will change if the order changes.
- hosts: localhost
tasks:
- name: Read an element's attribute values
xml:
path: /tmp/test.xml
xpath: /Document/properties/property
content: text
register: xmlresp
- name: Show an attribute value
debug:
var: xmlresp.matches[2]
Now I just need to be able to write new information for the "admin.ui.allow.manual.backup.download" property. Like changing the content to "true" or "True"
To get the value inside your very specific element you can be much more restrictive in your xpath:
- name: "Read an element's attribute values"
xml:
path: /tmp/test.xml
xpath: /Document/properties/property[@name='admin.ui.allow.manual.backup.download']
content: text
register: xmlresp
Similarly, you can use that same more restrictive (single element) xpath to update the content of your element if needed:
- name: Update element value if needed
xml:
path: /tmp/test.xml
xpath: /Document/properties/property[@name='admin.ui.allow.manual.backup.download']
value: "true"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.