Parsers¶
Parsers are responsible for mapping native configuration/data to a YANG model.
Processing data¶
The first thing you have to know is what type of data you are dealing with and then select the appropiate parser. Each one initializes the data and makes it available in similar ways but you have to be aware of the particularities of each one.
You can select the parser with the metadata field:
---
metadata:
processor: TextTree
execute:
- method: cli
kwargs:
commands: ["show running-config all"]
That block not only specifies which parser to use but how to retrieve the data necessary for the parser to operate for that particular model.
Available processors are:
Rule Directives¶
name | Available on | description |
---|---|---|
when | list, container, leaf | If this field is True the rule will be executed. Otherwise it won't. |
from | list, container, leaf | Under normal circumstances |
path | list, container, leaf | Which attributes to advanced. See examples_lists for details. |
default | list, container, leaf | If ``path`` fails to resolve assign this value to the resolved object |
key | list | Post processing/formatting of the key value |
regexp | list, leaf | On lists regexp will be applied over the key. Use it to filter results and capture extra data.
On leaves, regexp will assign as value the capture group value |
pre | leaf | Pre processing/formatting of the value. Useful when you want to statically set it based on ``when`` conditions or from values extracted previously in parent containers/leaves/lists. |
value | leaf | Post processing/formatting of the value. Useful when you need to combine data extracted in a regular expression or post-process it with a jinja2 filter to transform the value. |
map | leaf | When a map (dictionary) is provided, the value will be *resolved* using it assigning the new corresponding value. |
present | leaf | If present: yes value will be True if path resolves properly. If present: no value
will be True if path can't find the attribute. In the rest of the cases the value
will be False |
gate | leaf, container, leaf | If gate is present with any value we will stop traversing the tree. This is mostly useful in containers
as you can stop processing a branch under conditions where you know you will not find a match. |
attribute | leaf | This attribute is specific to the XMLParser. XML documents can add information in the form of attributes.
You can complement a path with this field to extract the attribute from the element found in path . |
Keys¶
When a list is traversed you will always have available a key with name $(attribute)_key
. In
addition, you will have parent_key
as the key of the immediate parent object. Example.
Bookmarks¶
Bookmarks are points of interest in the configuration. Usually, you will be gathering blocks of
configurations and parsing on those as you progress. However, sometimes the data you need is somewhere else.
For those cases you can use the bookmarks within the from
field to pick the correct block of
configuration.
Bookmarks are created automatically according to these rules:
- At the begining of each model a bookmark of name
root_$first_attribute_name
will point to the list of data that the parser requires. Example - When a container is traversed, a bookmark will be created with name
$attribure_name
- When a list is traveresed, each element of the list will have its own bookmark with name
$attribute_name.$key
.
extra_vars
¶
The regexp
directive lets you capture any arbitary amount of information you want. All captured
groups will be avaible to you inside the extra_vars.$attribute
($attribute
is the attribute where the
additional information was captured). Example.