@ -359,74 +359,85 @@ Network CLI filters
To convert the output of a network device CLI command into structured JSON
To convert the output of a network device CLI command into structured JSON
output, use the `` parse_cli `` filter::
output, use the `` parse_cli `` filter::
.. code-block :: yaml
{{ output | parse_cli('path/to/spec') }}
{{ output | parse_cli('path/to/spec') }}
The `` parse_cli `` filter will load the spec file and pass the command output
The `` parse_cli `` 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.
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
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
output and return JSON data. Below is an example of a valid spec file that
will parse the output from the `` show vlan `` command.::
will parse the output from the `` show vlan `` command.
---
.. code-block :: yaml
vars:
vlan:
---
vlan_id: "{{ item.vlan_id }}"
vars:
name: "{{ item.name }}"
vlan:
enabled: "{{ item.state != 'act/lshut' }}"
vlan_id: "{{ item.vlan_id }}"
state: "{{ item.state }}"
name: "{{ item.name }}"
enabled: "{{ item.state != 'act/lshut' }}"
keys:
state: "{{ item.state }}"
vlans:
value: "{{ vlan }}"
keys:
items: "^(?P<vlan_id>\\d+)\\s+(?P<name>\\w+)\\s+(?P<state>active|act/lshut|suspended)"
vlans:
state_static:
value: "{{ vlan }}"
value: present
items: "^(?P<vlan_id>\\d+)\\s+(?P<name>\\w+)\\s+(?P<state>active|act/lshut|suspended)"
state_static:
value: present
The spec file above will return a JSON data structure that is a list of hashes
The spec file above will return a JSON data structure that is a list of hashes
with the parsed VLAN information.
with the parsed VLAN information.
The same command could be parsed into a hash by using the key and values
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
directives. Here is an example of how to parse the output into a hash
value using the same `` show vlan `` command.::
value using the same `` show vlan `` command.
---
.. code-block :: yaml
vars:
vlan:
---
key: "{{ item.vlan_id }}"
vars:
values:
vlan:
vlan_id: "{{ item.vlan_id }}"
key: "{{ item.vlan_id }}"
name: "{{ item.name }}"
values:
enabled: "{{ item.state != 'act/lshut' }}"
vlan_id: "{{ item.vlan_id }}"
state: "{{ item.state }}"
name: "{{ item.name }}"
enabled: "{{ item.state != 'act/lshut' }}"
keys:
state: "{{ item.state }}"
vlans:
value: "{{ vlan }}"
keys:
items: "^(?P<vlan_id>\\d+)\\s+(?P<name>\\w+)\\s+(?P<state>active|act/lshut|suspended)"
vlans:
state_static:
value: "{{ vlan }}"
value: present
items: "^(?P<vlan_id>\\d+)\\s+(?P<name>\\w+)\\s+(?P<state>active|act/lshut|suspended)"
state_static:
value: present
Another common use case for parsing CLI commands is to break a large command
Another common use case for parsing CLI commands is to break a large command
into blocks that can be parsed. This can be done using the `` start_block `` and
into blocks that can be parsed. This can be done using the `` start_block `` and
`` end_block `` directives to break the command into blocks that can be parsed.::
`` end_block `` directives to break the command into blocks that can be parsed.
---
.. code-block :: yaml
vars:
interface:
---
name: "{{ item[0].match[0] }}"
vars:
state: "{{ item[1].state }}"
interface:
mode: "{{ item[2].match[0] }}"
name: "{{ item[0].match[0] }}"
state: "{{ item[1].state }}"
keys:
mode: "{{ item[2].match[0] }}"
interfaces:
value: "{{ interface }}"
keys:
start_block: "^Ethernet.*$"
interfaces:
end_block: "^$"
value: "{{ interface }}"
items:
start_block: "^Ethernet.*$"
- "^(?P<name>Ethernet\\d\\/\\d*)"
end_block: "^$"
- "admin state is (?P<state>.+),"
items:
- "Port mode is (.+)"
- "^(?P<name>Ethernet\\d\\/\\d*)"
- "admin state is (?P<state>.+),"
- "Port mode is (.+)"
The example above will parse the output of `` show interface `` into a list of
The example above will parse the output of `` show interface `` into a list of
@ -457,54 +468,59 @@ The spec file should be valid formatted YAML. It defines how to parse the XML
output and return JSON data.
output and return JSON data.
Below is an example of a valid spec file that
Below is an example of a valid spec file that
will parse the output from the `` show vlan | display xml `` command.::
will parse the output from the `` show vlan | display xml `` command.
---
.. code-block :: yaml
vars:
vlan:
---
vlan_id: "{{ item.vlan_id }}"
vars:
name: "{{ item.name }}"
vlan:
desc: "{{ item.desc }}"
vlan_id: "{{ item.vlan_id }}"
enabled: "{{ item.state.get('inactive') != 'inactive' }}"
name: "{{ item.name }}"
state: "{% if item.state.get('inactive') == 'inactive'%} inactive {% else %} active {% endif %}"
desc: "{{ item.desc }}"
enabled: "{{ item.state.get('inactive') != 'inactive' }}"
keys:
state: "{% if item.state.get('inactive') == 'inactive'%} inactive {% else %} active {% endif %}"
vlans:
value: "{{ vlan }}"
keys:
top: configuration/vlans/vlan
vlans:
items:
value: "{{ vlan }}"
vlan_id: vlan-id
top: configuration/vlans/vlan
name: name
items:
desc: description
vlan_id: vlan-id
state: ".[@inactive='inactive']"
name: name
desc: description
state: ".[@inactive='inactive']"
The spec file above will return a JSON data structure that is a list of hashes
The spec file above will return a JSON data structure that is a list of hashes
with the parsed VLAN information.
with the parsed VLAN information.
The same command could be parsed into a hash by using the key and values
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
directives. Here is an example of how to parse the output into a hash
value using the same `` show vlan | display xml `` command.::
value using the same `` show vlan | display xml `` command.
---
.. code-block :: yaml
vars:
vlan:
---
key: "{{ item.vlan_id }}"
vars:
values:
vlan:
vlan_id: "{{ item.vlan_id }}"
key: "{{ item.vlan_id }}"
name: "{{ item.name }}"
values:
desc: "{{ item.desc }}"
vlan_id: "{{ item.vlan_id }}"
enabled: "{{ item.state.get('inactive') != 'inactive' }}"
name: "{{ item.name }}"
state: "{% if item.state.get('inactive') == 'inactive'%} inactive {% else %} active {% endif %}"
desc: "{{ item.desc }}"
enabled: "{{ item.state.get('inactive') != 'inactive' }}"
keys:
state: "{% if item.state.get('inactive') == 'inactive'%} inactive {% else %} active {% endif %}"
vlans:
value: "{{ vlan }}"
keys:
top: configuration/vlans/vlan
vlans:
items:
value: "{{ vlan }}"
vlan_id: vlan-id
top: configuration/vlans/vlan
name: name
items:
desc: description
vlan_id: vlan-id
state: ".[@inactive='inactive']"
name: name
desc: description
state: ".[@inactive='inactive']"
The value of `` top `` is the XPath relative to the XML root node.
The value of `` top `` is the XPath relative to the XML root node.