Analysing large XML objects

Hi,

I have to analyse relatively large XML objects (Juniper configs). I’d like to check for presence of certain trees, values and attributes. I could write a lookup plugin but that feels like a relatively complicated way to solve a simple problem. Even just loading it as a var would make it easier, but I don’t think there is a “clean” ansible way of doing it. How one deals with XML in ansible?

kind regards
Pshem

Theres no build-in module as far as i know, but this project looks promising:

https://github.com/cmprescott/ansible-xml

I've used https://github.com/cmprescott/ansible-xml with some success,
but that module has its issues.
I do come across more and more cases where I need to manipulate XML,
so when it works its a clean option.

Dick

Here are some sample examples of applying conditional check for values in xml hierarchy.

https://github.com/ansible/ansible/blob/devel/test/integration/targets/junos_command/tests/netconf_xml/equal.yaml

https://github.com/ansible/ansible/blob/devel/test/integration/targets/junos_command/tests/netconf_xml/greaterthan.yaml

Regards,
Ganesh

Hi,

Thank you for your responses.

For now I’ve settled on these two plugins: https://gist.github.com/danieldbower/7b34c45ad5e39576e2e5 a lookup one and another filter one to turn xml to json that can be further parsed.

So for example to get the config of ge-0/0/0 i can use now this:

  • name: load file
    set_fact:
    rconfig: “{{ lookup(‘xmlfile’, ‘file=/tmp/config.xml xpath=./interfaces’) | xml2json }}”

  • name: show interface
    debug: var=item
    with_items: “{{ rconfig | json_query(query_path) }}”
    vars:
    query_path: “interfaces.interface[?name==‘ge-0/0/0’]”

kind regards
Pshem