# Analysing large XML objects

**URL:** <https://forum.ansible.com/t/analysing-large-xml-objects/24593>\
**Category:** Ansible Project\
**Created:** [May 29, 2017, 11:41pm UTC](https://forum.ansible.com/t/analysing-large-xml-objects/24593 "2017-05-29T23:41:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [May 29, 2017, 11:41pm UTC](https://forum.ansible.com/t/analysing-large-xml-objects/24593/1 "2017-05-29T23:41:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Daniel\_JD](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Daniel\_JD](https://forum.ansible.com/u/Daniel_JD)\
**Post date:** [May 30, 2017, 5:20am UTC](https://forum.ansible.com/t/analysing-large-xml-objects/24593/2 "2017-05-30T05:20:25Z")

</div>

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

[https://github.com/cmprescott/ansible-xml](https://github.com/cmprescott/ansible-xml)

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 30, 2017, 6:40am UTC](https://forum.ansible.com/t/analysing-large-xml-objects/24593/3 "2017-05-30T06:40:39Z")

</div>

I've used [https://github.com/cmprescott/ansible-xml](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

---

<div class="post-metadata">

**Author:** ![ganeshrn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ganeshrn/32/1021_2.png) [@ganeshrn](https://forum.ansible.com/u/ganeshrn)\
**Post date:** [May 30, 2017, 7:58am UTC](https://forum.ansible.com/t/analysing-large-xml-objects/24593/4 "2017-05-30T07:58:30Z")

</div>

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/equal.yaml)

[https://github.com/ansible/ansible/blob/devel/test/integration/targets/junos\_command/tests/netconf\_xml/greaterthan.yaml](https://github.com/ansible/ansible/blob/devel/test/integration/targets/junos_command/tests/netconf_xml/greaterthan.yaml)

Regards,  
Ganesh

---

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [May 30, 2017, 8:39pm UTC](https://forum.ansible.com/t/analysing-large-xml-objects/24593/5 "2017-05-30T20:39:14Z")

</div>

Hi,

Thank you for your responses.

For now I’ve settled on these two plugins: [https://gist.github.com/danieldbower/7b34c45ad5e39576e2e5](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
