ansible / ansible.netcommon / 6.1.0 / filter / parse_xml The parse_xml filter plugin. | "added in version" 1.0.0 of ansible.netcommon" Authors: Ganesh Nalawade (@ganeshrn)ansible.netcommon.parse_xml (6.1.0) — filter
Install with ansible-galaxy collection install ansible.netcommon:==6.1.0
collections: - name: ansible.netcommon version: 6.1.0
This filter will load the spec file and pass the command output through it, returning JSON output.
The YAML spec file defines how to parse the CLI output.
# Using parse_xml # example_output.xml # <?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> # parse_xml.yml # --- # 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 - name: Facts setup ansible.builtin.set_fact: xml: "{{ lookup('file', 'example_output.xml') }}"
- name: Parse xml invocation ansible.builtin.debug: msg: "{{ xml | ansible.netcommon.parse_xml('parse_xml.yml') }}"
tmpl: description: - The spec file should be valid formatted YAML. It defines how to parse the XML output and return JSON data. - For example C(xml_data | ansible.netcommon.parse_xml(template.yml)), in this case C(xml_data) represents xml data option. type: str output: description: - This source xml on which parse_xml invokes. required: true type: raw