Created
August 9, 2023 04:37
-
-
Save rohitthakur2590/c65197739471c9ca0f580cd48c7c2281 to your computer and use it in GitHub Desktop.
parse_xml example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xml.data.yaml | |
================== | |
<?xml version="1.0" encoding="UTF-8"?> | |
<rpc-reply message-id="urn:uuid:0cadb4e8-5bba-47f4-986e-72906227007f"> | |
<data> | |
<ntp> | |
<nodes> | |
<node> | |
<node>0/0/CPU0</node> | |
<associations> | |
<is-ntp-enabled>true</is-ntp-enabled> | |
<sys-leap>ntp-leap-no-warning</sys-leap> | |
<peer-summary-info> | |
<peer-info-common> | |
<host-mode>ntp-mode-client</host-mode> | |
<is-configured>true</is-configured> | |
<address>10.1.1.1</address> | |
<reachability>0</reachability> | |
</peer-info-common> | |
<time-since>-1</time-since> | |
</peer-summary-info> | |
<peer-summary-info> | |
<peer-info-common> | |
<host-mode>ntp-mode-client</host-mode> | |
<is-configured>true</is-configured> | |
<address>172.16.252.29</address> | |
<reachability>255</reachability> | |
</peer-info-common> | |
<time-since>991</time-since> | |
</peer-summary-info> | |
</associations> | |
</node> | |
</nodes> | |
</ntp> | |
</data> | |
</rpc-reply> | |
File 2 | |
test.yaml | |
========= | |
--- | |
- name: Parse XML Data | |
hosts: localhost | |
collections: | |
- ansible.netcommon | |
tasks: | |
- name: set xml Data | |
ansible.builtin.set_fact: | |
xml: "{{lookup('ansible.builtin.file', 'xml_data') }}" | |
- name: Parse Data | |
ansible.builtin.set_fact: | |
output: "{{ xml | parse_xml('/home/rothakur/ansible_collections/playbooks/junos/netcommon/specs/parse_xml.yaml') }}" | |
File 3 SPEC | |
parse_xml.yaml | |
=========== | |
--- | |
vars: | |
ntp_peers: | |
address: "{{ item.address }}" | |
reachability: "{{ item.reachability}}" | |
keys: | |
result: | |
value: "{{ ntp_peers }}" | |
top: data/ntp/nodes/node/associations | |
items: | |
address: peer-summary-info/peer-info-common/address | |
reachability: peer-summary-info/peer-info-common/reachability |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment