Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit bc25993

Browse files
committed
refactor for to prepare for nornir3
1 parent 83730fd commit bc25993

15 files changed

Lines changed: 76 additions & 92 deletions

File tree

netnir/core/tasks/config_plan.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from netnir.helpers.scaffold.command import CommandScaffold
2-
from netnir.core.template import CompileTemplate
2+
from netnir.plugins.template import template_file
33
from netnir.plugins.netmiko import netmiko_send_commands
4-
from netnir.helpers import output_writer, TextColor
54
from netnir.plugins.hier import hier_host
5+
from netnir.helpers import output_writer
66
from netnir.constants import OUTPUT_DIR
77
from nornir.plugins.functions.text import print_result
8-
import logging
98

109

1110
class ConfigPlan(CommandScaffold):
@@ -43,7 +42,7 @@ def parser(parser):
4342
required=False,
4443
)
4544

46-
def run(self, template_file="main.conf.j2"):
45+
def run(self):
4746
"""
4847
cli execution
4948
@@ -52,31 +51,27 @@ def run(self, template_file="main.conf.j2"):
5251
:returns: result string
5352
"""
5453
self.nr = self._inventory()
55-
56-
for host in self.nr.inventory.hosts:
57-
compiled_template = CompileTemplate(
58-
nr=self.nr, host=host, template=template_file
59-
)
60-
output_writer(
61-
nornir_results=compiled_template.render(), output_file="compiled.conf",
62-
)
63-
print_result(compiled_template.render())
54+
results = self.nr.run(
55+
task=template_file,
56+
template_file="main.conf.j2",
57+
output_file="compiled.conf",
58+
name="COMPILE TEMPLATES",
59+
)
60+
output_writer(nornir_results=results, output_file="compiled.conf")
61+
print_result(results)
6462

6563
if self.args.compile:
66-
if self.args.host:
67-
return compiled_template.render()
68-
message = TextColor.green("templates compiled for all hosts")
69-
return logging.info(message)
64+
return results
7065

71-
results = self.nr(
66+
results = self.nr.run(
7267
task=netmiko_send_commands,
7368
commands="show running",
7469
name="FETCH RUNNING CONFIG",
7570
)
7671
output_writer(nornir_results=results, output_file="running.conf")
7772
print_result(results)
7873

79-
result = self.nr.run(
74+
results = self.nr.run(
8075
task=hier_host,
8176
include_tags=self.args.include_tags,
8277
exclude_tags=self.args.exclude_tags,
@@ -86,6 +81,7 @@ def run(self, template_file="main.conf.j2"):
8681
load_file=True,
8782
name="RENDER REMEDIATION CONFIG",
8883
)
84+
output_writer(nornir_results=results, output_file="remediation.conf")
85+
print_result(results)
8986

90-
print_result(result)
91-
return result
87+
return results

netnir/core/tasks/ssh.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,9 @@ def run(self):
4444
name="SSH COMMAND EXECUTION",
4545
)
4646

47-
if isinstance(results, str):
48-
results = [results]
47+
if self.args.output:
48+
output_writer(nornir_results=results, output_file=self.args.output)
4949

50-
for task in results:
51-
if self.args.output:
52-
output_writer(nornir_results=task, output_file=self.args.output)
53-
54-
print_result(task)
50+
print_result(results)
5551

5652
return results

netnir/core/template.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

netnir/helpers/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,12 @@ def plugins_import(tasks: dict, subparsers: object):
169169
plugin.parser(cmdparser)
170170

171171
return loaded_plugins
172+
173+
174+
def merge_two_dicts(x, y):
175+
try:
176+
z = x.copy()
177+
except AttributeError:
178+
z = dict(x)
179+
z.update(y)
180+
return z

netnir/helpers/common/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
"""common helpers
2-
"""

netnir/plugins/hier.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from netnir.constants import HIER_DIR
2-
from netnir.core.output import Output
32
from nornir.core.task import Task, Result
43
from hier_config import Host
54
import logging
@@ -67,10 +66,8 @@ def hier_host(
6766
)
6867

6968
host.load_remediation()
70-
71-
output = Output(host=task.host.name, output_file="remediation.conf")
72-
output.write(
73-
host.filter_remediation(include_tags=include_tags, exclude_tags=exclude_tags)
69+
results = host.filter_remediation(
70+
include_tags=include_tags, exclude_tags=exclude_tags
7471
)
7572

76-
return Result(host=task.host, result=output.read())
73+
return Result(host=task.host, result=results)

netnir/plugins/netmiko.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from netnir.helpers import device_mapper
44

55

6-
def netmiko_send_commands(task: Task, commands: list()):
6+
def netmiko_send_commands(task: Task, commands: list()) -> Result:
77
"""send show commands to a device via netmiko
88
99
:param task: nornir Task object
@@ -29,7 +29,7 @@ def netmiko_send_commands(task: Task, commands: list()):
2929
return Result(host=task.host, result=output)
3030

3131

32-
def netmiko_send_config(task: Task, commands: list()):
32+
def netmiko_send_config(task: Task, commands: list()) -> Result:
3333
"""execute configuration changes on a device via netmiko
3434
3535
:param task: nornir Task object

netnir/plugins/template.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from nornir.core.task import Task, Result
2+
from netnir.helpers import merge_two_dicts
3+
from jinja2 import Environment, FileSystemLoader, StrictUndefined
4+
5+
6+
def template_file(
7+
task: Task, template_file: str, jinja_filters: dict = {}, **kwargs: dict,
8+
) -> Result:
9+
"""compile a jinja2 template and write it to a file.
10+
11+
:param task: nornir task object
12+
:param template_file: template file name
13+
:param jinja_filters: jinja2 filters
14+
:param kwargs: key/value pairs to render templates
15+
16+
:returns: nornir result object
17+
"""
18+
template_path = task.host.data["template_path"]
19+
env = Environment(
20+
loader=FileSystemLoader(template_path),
21+
undefined=StrictUndefined,
22+
trim_blocks=True,
23+
)
24+
env.filters.update(jinja_filters)
25+
template = env.get_template(template_file)
26+
merged_dicts = merge_two_dicts(kwargs, task.host)
27+
result = template.render(**merged_dicts)
28+
29+
return Result(host=task.host, result=result)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
hostname {{ name }}
2+
os {{ os }}
3+
bb_asn {{ bb_asn }}
4+
dc_asn {{ dc_asn }}

tests/data/templates/provider1/iosxr/template.j2

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)