Nornir and Jinja2 help

Starting automation journey, i want to iterate from interface name on hosts files for configuring some interface on a router but got this error

---- Configuration of interface ** changed : False ----------------------------- ERROR
Traceback (most recent call last):
File “/usr/local/lib/python3.7/dist-packages/nornir/core/task.py”, line 98, in start
r = self.task(self, **self.params)
File “/usr/local/lib/python3.7/dist-packages/nornir_jinja2/plugins/tasks/template_file.py”, line 42, in template_file
text = t.render(host=task.host, **kwargs)
File “/usr/local/lib/python3.7/dist-packages/jinja2/environment.py”, line 1090, in render
self.environment.handle_exception()
File “/usr/local/lib/python3.7/dist-packages/jinja2/environment.py”, line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File “/usr/local/lib/python3.7/dist-packages/jinja2/_compat.py”, line 28, in reraise
raise value.with_traceback(tb)
File “templates/interface.j2”, line 2, in top-level template code
interface {{ intf.name }}
jinja2.exceptions.UndefinedError: ‘dict object’ has no attribute ‘name’

here my host file:

R1:
hostname: 10.255.100.1
groups:
- cisco_ios
data:
interfaces:
- name: loopback0
ip: 172.16.255.1
mask: 255.255.255.255
- name: loopback1
ip: 172.16.255.10
mask: 255.255.255.255
- name: loopback2
ip: 172.16.255.110
mask: 255.255.255.255
- state: shut

here my script file:

from nornir import InitNornir
from nornir_utils.plugins.tasks.data import load_yaml
from nornir_jinja2.plugins.tasks import template_file
from nornir_utils.plugins.functions import print_result, print_title
from nornir_netmiko import netmiko_send_config
from nornir_napalm.plugins.tasks import napalm_cli

nr = InitNornir(config_file=“config.yml”, dry_run=True)
def conf_int(task):
r = task.run(task=template_file,
name=“Configuration of interface”,
template=“interface.j2”,
path=f"templates/")

task.host["config"] = r.result

task.run(task=networking.napalm_configure,
         name="Loading Conf",
         replace=False,
         configuraion=task.host["config"])

#nr.data.dry_run = False
print_title(“Runbook to configure the network”)
result = nr.run(task=conf_int)
print_result(result)

and here the template file:

{% for intf in host[“interfaces”] -%}
interface {{ intf.name }}
ip addr {{ intf.ip }} {{ intf.mask }}
{% endfor %}

PS: Using Nornir3

Thanks for your help

Could you please format your code so we make sure about the indentation (mostly for the yaml file)?

---
R1:
   hostname: 10.255.100.1 
   groups:
      - cisco_ios
  data:
       interfaces:
          - name: loopback0
            ip: 172.16.255.1
            mask: 255.255.255.255
          - name: loopback1
            ip: 172.16.255.10
            mask: 255.255.255.255
          - name: loopback2
            ip: 172.16.255.110
            mask: 255.255.255.255
          - state: shut

Hello,

I think you need to change the iteration in the Jinja template

{% for intf in host[“data”][“interfaces”] -%}

And it looks like there is a space missing before data in the host file and the indentation is off.

Hi Dife,

thanks but still got the same issue

solved error on the environment set up