女巫凝固汽油弹神社模板问题

nornir napalm jinja Template Issue

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

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

我正在 运行在 python3.6 virtualenv 中安装这个 nornir Jinja 模板脚本。我还有其他简单的 nornir 和 napalm 代码 运行ning 没问题,这让我们怀疑这个问题与我正在尝试使用的 jinja2 模板函数有关。

我收到的错误如下。谁能帮我找出问题所在?

具有凝固汽油弹功能的工作 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(模板文件)- Alt 版本(完全相同的错误消息/结果

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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

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

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