Accessing data items in inventory

Hi,

I have the following Jinja2 template that I can not make it work deseperatly :

{% for vlan in host.vlans %}
vlan {{ vlan.id }}
name {{ vlan.name }}
{% endfor %}

And the following python code:

from nornir import InitNornir
from nornir.plugins.tasks import networking, text
from nornir.plugins.functions.text import print_title, print_result
from tqdm import tqdm

def service_L2(task):
# Transform inventory data to configuration via a template file
r = task.run(task=text.template_file,
name=“Generate L2 services configuration for devices”,
template=“service_L2.j2”,
path=f"templates/{task.host.platform}")

# Save the compiled configuration into a host variable
task.host["config"] = r.result

# Deploy that configuration to the device using NAPALM
task.run(task=networking.napalm_configure,
         name="Loading L2 services configuration on devices",
         replace=False,
         configuration=task.host["config"])

nr = InitNornir(config_file=“config.yml”, dry_run=False)
ACH_leaf = nr.filter(site=“ACH”, type=“network_device”, role=“leaf”)

result1 = ACH_leaf.run(task=service_L2)
print_result(result1)

But I’ve got this error when parsing the jinja2 template :


> File “templates/eos/service_L2.j2”, line 6, in top-level template code
> {% for vlan in host.vlans %}
> jinja2.exceptions.UndefinedError: ‘nornir.core.inventory.Host object’ has no attribute ‘vlans’
>
> ^^^^ END service_L2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Thank you for your help !
C

Finally, I found the error. The filters I used pointed to other devices in the inventory file without the required keywords.
C