Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_template_file_error_broken_file(self, nornir):
results = nornir.run(text.template_file, template="broken.j2", path=data_dir)
processed = False
for result in results.values():
processed = True
assert isinstance(result.exception, TemplateSyntaxError)
assert processed
nornir.data.reset_failed_hosts()
def test_template_file(self, nornir):
result = nornir.run(text.template_file, template="simple.j2", path=data_dir)
assert result
for h, r in result.items():
assert h in r.result
if h == "host3.group_2":
assert "my_var: comes_from_all" in r.result
if h == "host4.group_2":
assert "my_var: comes_from_host4.group_2" in r.result
def render_configurations(task):
bgp = task.run(
task=text.template_file, template="bgp.j2", path="nxos/", **task.host
)
intf = task.run(
task=text.template_file, template="routed_int.j2", path="nxos/", **task.host
)
task.host["bgp_config"] = bgp.result
task.host["intf_config"] = intf.result
def render_configurations(task):
try:
task.run(task=text.template_file, template="loopback.j2", path=".", **task.host)
except NornirSubTaskError:
task.results.pop()
msg = "Encountered Jinja2 error"
return Result(
changed=False,
diff=None,
result=msg,
host=task.host,
failed=False,
exception=None,
)
def my_task(task):
result = task.run(
task=text.template_file, template="interfaces.j2", path=".", **task.host
)
print(result[0].result)
def render_configurations(task):
bgp = task.run(
task=text.template_file, template="bgp.j2", path="nxos/", **task.host
)
intf = task.run(
task=text.template_file, template="routed_int.j2", path="nxos/", **task.host
)
task.host["bgp_config"] = bgp.result
task.host["intf_config"] = intf.result
"""
Nornir Task
:param task:
:param arg:
:return:
"""
# Define the Jinja2 template file we will use to build our custom commands for each device
j2template = 'vlan_updates.j2'
# Generate a unique text file of commands for each device in our inventory
filename = "cfg-{}.txt".format(task.host)
task.host["rendered_cfg"] = task.run(task=template_file, template=j2template, path='', info=arg)
with open(filename,"w") as cfg_file:
cfg_file.write(str(task.host['rendered_cfg'][0]))
print("\nCreated Configuration file {} for device {} in local directory...".format(task.host,filename))
def render_configs(task):
"""Generate BGP configs from Jinja2 template."""
template = "bgp.j2"
result = task.run(task=text.template_file, template=template, path=".", **task.host)
rendered_config = result[0].result
task.host["rendered_config"] = rendered_config
if DEBUG:
print()
print("*" * 40)
print(task.host.name)
print("-" * 8)
print(rendered_config)
print("*" * 40)
print()
Args:
task: nornir task object
"""
prefix_list_config = task.run(
task=template_file, path="templates", template="prefix_lists.j2", **task.host
)
task.host["prefix_list_config"] = prefix_list_config.result
route_map_config = task.run(
task=template_file, path="templates", template="route_maps.j2", **task.host
)
task.host["route_map_config"] = route_map_config.result
bgp_config = task.run(
task=template_file, path="templates", template="bgp.j2", **task.host
)
task.host["bgp_router_config"] = bgp_config.result
def render_configurations(task):
try:
task.run(task=text.template_file, template="loopback.j2", path=".", **task.host)
except NornirSubTaskError:
return "Templating error occurred, but its okay...."