Automation nornir凝固汽油弹jinja模板发布
在nornir框架内使用凝固汽油弹功能时出现Jinja模板问题 更新:我的主机是Cisco IOS设备 我正在python3.6 virtualenv中运行这个nornir Jinja模板脚本。我有其他简单的nornir和napalm代码运行良好,这使我们怀疑问题与我试图使用的jinja2模板函数有关 我收到的错误如下。有人能帮我找出这个问题吗 使用凝固汽油弹功能的nornir工作脚本-显示工作环境的示例 导致错误的Nornir脚本: config.yaml base.j2模板文件 base.j2模板文件-Alt版本完全相同错误消息/结果 亚马尔酒店 运行时输出错误: 您没有将系统传递给模板。我认为您在模板中要做的是:Automation nornir凝固汽油弹jinja模板发布,automation,network-programming,jinja2,Automation,Network Programming,Jinja2,在nornir框架内使用凝固汽油弹功能时出现Jinja模板问题 更新:我的主机是Cisco IOS设备 我正在python3.6 virtualenv中运行这个nornir Jinja模板脚本。我有其他简单的nornir和napalm代码运行良好,这使我们怀疑问题与我试图使用的jinja2模板函数有关 我收到的错误如下。有人能帮我找出这个问题吗 使用凝固汽油弹功能的nornir工作脚本-显示工作环境的示例 导致错误的Nornir脚本: config.yaml base.j2模板文件 base.j
hostname {{ host.name }}
ip domain-name {{ site }}.{{ domain }}
基本上,除非您添加额外的KWARG,否则模板将通过host变量访问主机本身,并访问清单中指定的所有主机属性。就是这样!谢谢
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!!!")
num_workers: 100
inventory: nornir.plugins.inventory.simple.SimpleInventory
SimpleInventory:
host_file: "inventory/hosts.yaml"
group_file: "inventory/groups.yaml"
hostname {{ system.hostname }}
ip domain-name {{ site }}.{{ domain }}
hostname {{ host.name }}
ip domain-name {{ site }}.{{ domain }}
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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
hostname {{ host.name }}
ip domain-name {{ site }}.{{ domain }}