There is a xml
module in https://docs.ansible.com/ansible/latest/collections/community/general/xml_module.html and there is an example to retrieve a xml value from an XML file
# Consider the following XML file:
#
# <business type="bar">
# <name>Tasty Beverage Co.</name>
# <beers>
# <beer>Rochefort 10</beer>
# <beer>St. Bernardus Abbot 12</beer>
# <beer>Schlitz</beer>
# </beers>
# <rating subjective="true">10</rating>
# <website>
# <mobilefriendly/>
# <address>http://tastybeverageco.com</address>
# </website>
# </business>
# Retrieve and display the number of nodes
- name: Get count of 'beers' nodes
community.general.xml:
path: /foo/bar.xml
xpath: /business/beers/beer
count: yes
register: hits
- ansible.builtin.debug:
var: hits.count
# ...
# How to read an attribute value and access it in Ansible
- name: Read an element's attribute values
xml:
path: /foo/bar.xml
xpath: /business/website/validxhtml
content: attribute
register: xmlresp
- name: Show an attribute value
debug:
var: xmlresp.matches[0].validxhtml.validatedon
You can adopt that for your needs. In Ansible 2.9 use xml
instead of community.general.xml
and be aware of installing the necessary libs (lxml)