Templating
Templating Module¶
templating ¶
Jinja2 template resolution for AVD inventory variables.
This module provides functionality to resolve Jinja2 template variables in AVD inventory YAML data structures, supporting Ansible-style variable references and filters.
TemplateResolver ¶
Resolve Jinja2 templates in AVD inventory data.
This class handles resolution of Jinja2 template variables following Ansible conventions for variable references, hostvars access, and filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Dict[str, Any]
|
Template context containing all variables available for resolution |
required |
Examples:
>>> context = {
... "default_mtu": 9214,
... "hostvars": {"spine01": {"platform": "7050X3"}}
... }
>>> resolver = TemplateResolver(context)
>>> resolver.resolve("{{ default_mtu }}")
'9214'
>>> resolver.resolve("{{ hostvars['spine01']['platform'] }}")
'7050X3'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Dict[str, Any]
|
Variables available for template resolution |
required |
Source code in avd_cli/logics/templating.py
has_template ¶
resolve ¶
Resolve a single template string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template_str
|
str
|
String containing Jinja2 template(s) |
required |
Returns:
| Type | Description |
|---|---|
str
|
Resolved string with templates replaced by their values |
Raises:
| Type | Description |
|---|---|
TemplateError
|
If template resolution fails (undefined variable, syntax error, etc.) |
Examples:
>>> resolver = TemplateResolver({"mtu": 9214})
>>> resolver.resolve("{{ mtu }}")
'9214'
>>> resolver.resolve("{{ mtu | default(1500) }}")
'9214'
Source code in avd_cli/logics/templating.py
resolve_dict ¶
Recursively resolve templates in a dictionary.
Walks through the dictionary structure recursively, resolving templates in all string values while preserving the structure and non-string types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Dictionary containing potential templates |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with all templates resolved |
Examples:
>>> context = {"server": "10.0.0.100", "vrf": "MGMT"}
>>> resolver = TemplateResolver(context)
>>> data = {"radius": {"host": "{{ server }}", "vrf": "{{ vrf }}"}}
>>> resolved = resolver.resolve_dict(data)
>>> resolved["radius"]["host"]
'10.0.0.100'
Source code in avd_cli/logics/templating.py
resolve_list ¶
resolve_recursive ¶
Recursively resolve templates in any data structure.
This method handles dicts, lists, and primitive types, recursively walking through nested structures to resolve all template strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Data structure (dict, list, str, int, bool, None, etc.) |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Data structure with all templates resolved |
Examples:
>>> context = {"mtu": 9214, "vrf": "MGMT"}
>>> resolver = TemplateResolver(context)
>>> data = {
... "interfaces": [
... {"name": "Ethernet1", "mtu": "{{ mtu }}"},
... {"name": "Ethernet2", "mtu": "{{ mtu }}"}
... ],
... "vrf": "{{ vrf }}"
... }
>>> resolved = resolver.resolve_recursive(data)
>>> resolved["interfaces"][0]["mtu"]
'9214'
Source code in avd_cli/logics/templating.py
resolve_value ¶
Resolve templates in a single value (string, int, bool, etc.).
Only string values are processed for templates. Other types are returned unchanged to preserve type information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to resolve (string, int, bool, None, etc.) |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Resolved value (same type as input for non-strings) |
Examples:
>>> resolver = TemplateResolver({"mtu": 9214})
>>> resolver.resolve_value("{{ mtu }}")
'9214'
>>> resolver.resolve_value(123)
123
>>> resolver.resolve_value(True)
True
Source code in avd_cli/logics/templating.py
build_template_context ¶
build_template_context(
global_vars: Dict[str, Any],
group_vars: Dict[str, Dict[str, Any]],
host_vars: Dict[str, Dict[str, Any]],
) -> Dict[str, Any]
Build Jinja2 template context from inventory variables.
Creates a context dictionary suitable for template resolution by merging global variables, group variables, and constructing the hostvars dictionary.
For hostvars, we merge global + all group vars + host vars for each host. This ensures templates like {{ hostvars[‘host’]['group_var'] }} work.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
global_vars
|
Dict[str, Any]
|
Global variables from group_vars/all.yml |
required |
group_vars
|
Dict[str, Dict[str, Any]]
|
All group variables |
required |
host_vars
|
Dict[str, Dict[str, Any]]
|
All host variables |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Template context with all variables and hostvars |
Examples:
>>> global_vars = {"default_mtu": 9214}
>>> group_vars = {"SPINES": {"bgp_as": 65001}}
>>> host_vars = {"spine01": {"platform": "7050X3"}}
>>> context = build_template_context(global_vars, group_vars, host_vars)
>>> context["default_mtu"]
9214
>>> context["hostvars"]["spine01"]["platform"]
'7050X3'