Jinja Filters
nautobot_design_builder.jinja_filters
¶
Useful jinja2 filters for designs.
ip_network(input_str)
¶
Jinja2 filter to convert a string to an IPNetwork object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_str
|
str
|
String correctly formatted as an IP Address |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IPNetwork |
IPNetwork
|
object that represents the input string |
Source code in nautobot_design_builder/jinja_filters.py
network_offset(prefix, offset)
¶
Jinja2 filter to compute an IPNetwork based off of a prefix and offset.
Example
from design_builder.jinja2 import network_offset network_offset("1.1.0.0/16", "0.0.1.1") IPNetwork('1.1.1.1/16')
from design_builder.jinja2 import network_offset network_offset("1.1.0.0/16", "0.0.1.0/24") IPNetwork('1.1.1.0/24')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
Prefix string in the form x.x.x.x/x |
required |
offset
|
str
|
Prefix string in the form x.x.x.x/x |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IPNetwork |
IPNetwork
|
Returns an IPNetwork that is the result of prefix + offset. The |
IPNetwork
|
returned network object's prefix will be set to the longer prefix length |
|
IPNetwork
|
between the two inputs. |
Source code in nautobot_design_builder/jinja_filters.py
network_string(network, attr='')
¶
Jinja2 filter to convert the IPNetwork object to a string.
If an attribute is supplied, first lookup the attribute on the IPNetwork object, then convert the returned value to a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
IPNetwork
|
Object to convert to string |
required |
attr
|
str
|
Optional attribute to retrieve from the IPNetwork prior to converting to a string. Defaults to "". |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Converted object |
Source code in nautobot_design_builder/jinja_filters.py
to_json(value)
¶
Jinja2 filter to render a value to properly formatted JSON.
This method will render a value to proper JSON. If the value is part of a Design
Builder render context, then the correct type encoding (dictionary, list, etc) is
used. The Nautobot render_json method does not handle UserDict or UserList
which are the primary collection types for Design Builder contexts. This implementation
will unwrap those types and render the contained data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to be encoded as JSON. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
JSON encoded value. |
Source code in nautobot_design_builder/jinja_filters.py
to_yaml(value, **kwargs)
¶
Jinja2 filter to render a value to properly formatted YAML.
This method will render a value to proper YAML. If the value is part of a Design
Builder render context, then the correct type encoding (dictionary, list, etc) is
used. The Nautobot render_yaml method does not handle UserDict or UserList
which are the primary collection types for Design Builder contexts. This implementation
will unwrap those types and render the contained data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to be rendered as YAML. |
required |
kwargs
|
Any
|
Any additional options to pass to the yaml.dump method. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
YAML encoded value. |