nornir napalm jinja模板问题

时间:2018-08-13 23:02:43

标签: automation network-programming jinja2

Jinja模板在nornir框架中使用napalm函数时出现问题。

已更新:我的主机是Cisco IOS设备。

我正在python3.6 virtualenv中运行此nornir Jinja模板脚本。我还有其他简单的nornir和napalm代码可以正常运行,这使我们怀疑该问题与我要使用的jinja2模板函数有关。

我收到的错误如下。谁能帮助我发现问题?

具有napalm函数的工作nornir脚本-显示工作环境的示例

from nornir.core import InitNornir
from nornir.plugins.tasks import text
from nornir.plugins.tasks.networking import napalm_get, napalm_configure
from nornir.plugins.functions.text import print_title, print_result
from nornir.core.exceptions import NornirExecutionError
import logging

nr = InitNornir(config_file="config.yaml", dry_run=True)

ctil_net = nr.filter(site="ctil", type="network_device")
core = nr.filter(role="core", type="network_device")

results_napalm_get = nr.run(
task=napalm_get, getters=["facts", "interfaces"]
)
print_result(results_napalm_get)

Nornir脚本导致错误:

from nornir.core import InitNornir
from nornir.plugins.tasks import text
from nornir.plugins.tasks.networking import napalm_get, napalm_configure
from nornir.plugins.functions.text import print_title, print_result
from nornir.core.exceptions import NornirExecutionError
import logging

nr = InitNornir(config_file="config.yaml", dry_run=True)

ctil_net = nr.filter(site="ctil", type="network_device")
core = nr.filter(role="core", type="network_device")

def basic_configuration(task):
    # Transform inventory data to configuration via a template file
    r = task.run(task=text.template_file,
                 name="Base Template Configuration",
                 template="base.j2",  ## modified
                 path=f"templates",
                 severity_level=logging.DEBUG)

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

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

try:
    print_title("Playbook to configure the network")
    result = core.run(task=basic_configuration)
    print_result(result)
except NornirExecutionError:
    print("ERROR!!!")

config.yaml

num_workers: 100
inventory: nornir.plugins.inventory.simple.SimpleInventory
SimpleInventory:
    host_file: "inventory/hosts.yaml"
    group_file: "inventory/groups.yaml"

base.j2(模板文件)

hostname {{ system.hostname }}
ip domain-name {{ site }}.{{ domain }}

base.j2(模板文件)-替代版本(相同的错误消息味精/结果

hostname {{ host.name }}
ip domain-name {{ site }}.{{ domain }}

Hosts.yaml

switch101:
  nornir_host: 192.168.2.101
  site: ctil
  role: core
  groups:
      - lab
  nornir_nos: ios
  type: network_device

switch007:
  nornir_host: 192.168.2.7
  site: ctil
  role: access
  groups:
      - production
  nornir_nos: ios
  type: network_device
我运行时

错误输出:

$ python adv-nornir-example.py
**** Playbook to configure the network *****************************************
basic_configuration*************************************************************
* switch101 ** changed : False *************************************************
vvvv basic_configuration ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv ERROR
Subtask: Base Template Configuration (failed)

---- Base Template Configuration ** changed : False ---------------------------- ERROR
Traceback (most recent call last):
  File "/home/melshman/projects/virtualenv/nornir/lib/python3.6/site-packages/nornir/core/task.py", line 62, in start
    r = self.task(self, **self.params)
  File "/home/melshman/projects/virtualenv/nornir/lib/python3.6/site-packages/nornir/plugins/tasks/text/template_file.py", line 26, in template_file
    **merged
  File "/home/melshman/projects/virtualenv/nornir/lib/python3.6/site-packages/nornir/core/helpers/jinja_helper.py", line 11, in render_from_file
    return template.render(**kwargs)
  File "/home/melshman/projects/virtualenv/nornir/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/home/melshman/projects/virtualenv/nornir/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/melshman/projects/virtualenv/nornir/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/melshman/projects/virtualenv/nornir/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "templates/base.j2", line 2, in top-level template code
    hostname {{ system.hostname }}
  File "/home/melshman/projects/virtualenv/nornir/lib/python3.6/site-packages/jinja2/environment.py", line 430, in getattr
    return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'system' is undefined

^^^^ END basic_configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 个答案:

答案 0 :(得分:1)

您没有将system传递给模板。我认为您要在模板中尝试做的是:

hostname {{ host.name }}
ip domain-name {{ site }}.{{ domain }}

基本上,除非您添加额外的kwarg,否则模板将可以通过host变量访问主机本身,并可以访问清单中指定的所有主机属性。