mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-25 14:20:22 -07:00
Add new filter to parse xml output for network use cases (#31562)
* Add new filter to parse xml output for network use cases Fixes #31026 * Add parse_xml filter * Add documentation for parse_xml filter * Edited for clarity. * Fix review comment and add unit tests * Fix unit test CI failure * Fix CI issues * Fix unit test failures * Fix review comments * More copy edits.
This commit is contained in:
parent
50c9f91060
commit
0ddf092ae3
8 changed files with 393 additions and 6 deletions
|
@ -345,8 +345,7 @@ output, use the ``parse_cli`` filter::
|
|||
{{ output | parse_cli('path/to/spec') }}
|
||||
|
||||
The ``parse_cli`` filter will load the spec file and pass the command output
|
||||
through, it returning JSON output. The spec file is a YAML yaml that defines
|
||||
how to parse the CLI output.
|
||||
through it, returning JSON output. The YAML spec file defines how to parse the CLI output.
|
||||
|
||||
The spec file should be valid formatted YAML. It defines how to parse the CLI
|
||||
output and return JSON data. Below is an example of a valid spec file that
|
||||
|
@ -362,7 +361,6 @@ will parse the output from the ``show vlan`` command.::
|
|||
|
||||
keys:
|
||||
vlans:
|
||||
type: list
|
||||
value: "{{ vlan }}"
|
||||
items: "^(?P<vlan_id>\\d+)\\s+(?P<name>\\w+)\\s+(?P<state>active|act/lshut|suspended)"
|
||||
state_static:
|
||||
|
@ -387,7 +385,6 @@ value using the same ``show vlan`` command.::
|
|||
|
||||
keys:
|
||||
vlans:
|
||||
type: list
|
||||
value: "{{ vlan }}"
|
||||
items: "^(?P<vlan_id>\\d+)\\s+(?P<name>\\w+)\\s+(?P<state>active|act/lshut|suspended)"
|
||||
state_static:
|
||||
|
@ -426,6 +423,101 @@ filter::
|
|||
|
||||
Use of the TextFSM filter requires the TextFSM library to be installed.
|
||||
|
||||
Network XML filters
|
||||
```````````````````
|
||||
|
||||
.. versionadded:: 2.5
|
||||
|
||||
To convert the XML output of a network device command into structured JSON
|
||||
output, use the ``parse_xml`` filter::
|
||||
|
||||
{{ output | parse_xml('path/to/spec') }}
|
||||
|
||||
The ``parse_xml`` filter will load the spec file and pass the command output
|
||||
through formatted as JSON.
|
||||
|
||||
The spec file should be valid formatted YAML. It defines how to parse the XML
|
||||
output and return JSON data.
|
||||
|
||||
Below is an example of a valid spec file that
|
||||
will parse the output from the ``show vlan | display xml`` command.::
|
||||
|
||||
---
|
||||
vars:
|
||||
vlan:
|
||||
vlan_id: "{{ item.vlan_id }}"
|
||||
name: "{{ item.name }}"
|
||||
desc: "{{ item.desc }}"
|
||||
enabled: "{{ item.state.get('inactive') != 'inactive' }}"
|
||||
state: "{% if item.state.get('inactive') == 'inactive'%} inactive {% else %} active {% endif %}"
|
||||
|
||||
keys:
|
||||
vlans:
|
||||
value: "{{ vlan }}"
|
||||
top: configuration/vlans/vlan
|
||||
items:
|
||||
vlan_id: vlan-id
|
||||
name: name
|
||||
desc: description
|
||||
state: ".[@inactive='inactive']"
|
||||
|
||||
The spec file above will return a JSON data structure that is a list of hashes
|
||||
with the parsed VLAN information.
|
||||
|
||||
The same command could be parsed into a hash by using the key and values
|
||||
directives. Here is an example of how to parse the output into a hash
|
||||
value using the same ``show vlan | display xml`` command.::
|
||||
|
||||
---
|
||||
vars:
|
||||
vlan:
|
||||
key: "{{ item.vlan_id }}"
|
||||
values:
|
||||
vlan_id: "{{ item.vlan_id }}"
|
||||
name: "{{ item.name }}"
|
||||
desc: "{{ item.desc }}"
|
||||
enabled: "{{ item.state.get('inactive') != 'inactive' }}"
|
||||
state: "{% if item.state.get('inactive') == 'inactive'%} inactive {% else %} active {% endif %}"
|
||||
|
||||
keys:
|
||||
vlans:
|
||||
value: "{{ vlan }}"
|
||||
top: configuration/vlans/vlan
|
||||
items:
|
||||
vlan_id: vlan-id
|
||||
name: name
|
||||
desc: description
|
||||
state: ".[@inactive='inactive']"
|
||||
|
||||
|
||||
The value of ``top`` is the XPath relative to the XML root node.
|
||||
In the example XML output given below, the value of ``top`` is ``configuration/vlans/vlan``,
|
||||
which is an XPath expression relative to the root node (<rpc-reply>).
|
||||
``configuration`` in the value of ``top`` is the outer most container node, and ``vlan``
|
||||
is the inner-most container node.
|
||||
|
||||
``items`` is a dictionary of key-value pairs that map user-defined names to XPath expressions
|
||||
that select elements. The Xpath expression is relative to the value of the XPath value contained in ``top``.
|
||||
For example, the ``vlan_id`` in the spec file is a user defined name and its value ``vlan-id`` is the
|
||||
relative to the value of XPath in ``top``
|
||||
|
||||
Attributes of XML tags can be extracted using XPath expressions. The value of ``state`` in the spec
|
||||
is an XPath expression used to get the attributes of the ``vlan`` tag in output XML.::
|
||||
|
||||
<rpc-reply>
|
||||
<configuration>
|
||||
<vlans>
|
||||
<vlan inactive="inactive">
|
||||
<name>vlan-1</name>
|
||||
<vlan-id>200</vlan-id>
|
||||
<description>This is vlan-1</description>
|
||||
</vlan>
|
||||
</vlans>
|
||||
</configuration>
|
||||
</rpc-reply>
|
||||
|
||||
.. note:: For more information on supported XPath expressions, see `<https://docs.python.org/2/library/xml.etree.elementtree.html#xpath-support>`_.
|
||||
|
||||
.. _hash_filters:
|
||||
|
||||
Hashing filters
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue