Provides full featured salt.states.xml
Is your feature request related to a problem? Please describe.
I can't add new XML nodes or remove sub trees with salt.states.xml.
Describe the solution you'd like
I would like to be able to:
- add new elements with its value:
add-new-element-with-simple-value: xml.value_present: - name: /tmp/test.xml - xpath: /some/xpath/query - value: some value - remove elements or subtrees:
remove-element: xml.absent: - name: /tmp/test.xml - xpath: /some/xpath/spec - add new elements with its value and attributes (generalisation of the previous one):
add-new-element-with-value-and-attributes: xml.present: - name: /tmp/test.xml - xpath: /some/xpath/query - value: some value - attributes: attr1: val1 attr2: val2 - use a
dictto set values (inspired by xmltodict):{%- set values_dict = {'root': { 'subkey1': 'value1', 'subkey2': { '@attr1': 'val1', '@attr2': 'val2', '#text': 'value2' }, 'subkey3': [ 'value3.1', 'value3.2' ] }} %} add-or-set-elements-from-dict: xml.present: - name: /tmp/test.xml - content: {{ values_dict }}
Describe alternatives you've considered
I'm using salt.states.augeas.change with xml.lens:
simple-xml-value:
augeas.change:
- lens: xml.lns
- context: /files/tmp/test.xml
- changes:
- set some/xpath/query/#text 'value'
Additional context
I would like to provide a formula to manage OPNsense configuration like this ansible role
related to #58245
You can get surprisingly far with the changes that I added, but an 'absent' state is still needed to completely cover this request.
Interesting @Timbus.
So with your PR I need to build the XML fragment from my source dict and then merge it with the rest of XML, right?
Interesting @Timbus.
So with your PR I need to build the XML fragment from my source dict and then merge it with the rest of XML, right?
Yes, unfortunately I didn't have generated/dynamic data in mind when I made my changes.. I had more of a 'I need to merge/add static configs to an XML file' use case. I didn't want to approach the difficult task of mapping yaml or python dicts to XML either.
If you have a dictionary of data to dump, you might want more low-level states.. or some kind of full tree builder, for your needs.. Oh, perhaps the xmlutil in the util folder would help too?
Hi, great addition may I suggest to handle namespaces as well ?