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

Commit b081135

Browse files
authored
Merge pull request #20 from netdevops/nornir3
[WIP] nornir3 preparation
2 parents 55cc43d + 8cc346a commit b081135

37 files changed

Lines changed: 198 additions & 337 deletions

netnir/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
netnir will create the default config and folders.
44
"""
55

6-
__version__ = "0.0.16"
6+
__version__ = "0.0.17"

netnir/cli.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
import sys
66

77

8-
"""cli class to build and execute cli commands
9-
"""
10-
11-
128
class Cli:
139
"""
1410
a class object used to setup the netnir cli.

netnir/core/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
"""netnir core initialization
2-
"""

netnir/core/credentials.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
import os
66

77

8-
"""credentials create/fetch/delete class
9-
"""
10-
11-
128
class Credentials:
139
"""
1410
a class to do credentials administration.

netnir/core/inventory.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import yaml
44

55

6-
"""dynamic inventory builder class"""
7-
8-
96
class NornirInventory(Inventory):
107
"""
118
default inventory module to dynamically create inventory objects from host_vars and group_vars
@@ -21,7 +18,6 @@ def __init__(self, **kwargs):
2118
)
2219

2320
def nhosts(self):
24-
from netnir.helpers import device_mapper
2521
from netnir.core.credentials import Credentials
2622
from netnir.constants import (
2723
HOSTVARS,
@@ -47,7 +43,7 @@ def nhosts(self):
4743
"username": host_vars.get("username", creds["username"]),
4844
"password": host_vars.get("password", creds["password"]),
4945
"port": host_vars.get("port", 22),
50-
"platform": device_mapper(host_vars["os"]),
46+
"platform": host_vars["os"],
5147
"groups": host_vars.get("groups", list()),
5248
"data": {
5349
**host_vars,
@@ -61,18 +57,6 @@ def nhosts(self):
6157
),
6258
"mgmt_protocol": host_vars.get("mgmt_protocol", "ssh"),
6359
},
64-
"connection_options": {
65-
"netconf": {
66-
"hostname": f"{host}.{domain}" if domain else host,
67-
"username": host_vars.get("username", creds["username"]),
68-
"password": host_vars.get("password", creds["password"]),
69-
"platform": device_mapper(
70-
os_type=host_vars["os"], proto="netconf"
71-
),
72-
"port": host_vars.get("port", 830),
73-
"extras": {"hostkey_verify": False},
74-
},
75-
},
7660
}
7761

7862
return data

netnir/core/networking.py

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

netnir/core/output.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
import os
33

44

5-
"""ouput read/write/delete class
6-
"""
7-
8-
95
class Output:
106
"""
117
a class for writing, reading, and deleting output data to/from a file

netnir/core/tasks/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
"""built-in tasks initialization"""

netnir/core/tasks/config_plan.py

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
from netnir.helpers.scaffold.command import CommandScaffold
2-
from netnir.core.template import CompileTemplate
3-
from netnir.core.networking import Networking
4-
from netnir.helpers import output_writer, TextColor
2+
from netnir.plugins.template import template_file
3+
from netnir.plugins.netmiko import netmiko_send_commands
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
9-
10-
11-
"""config plan cli commands
12-
"""
138

149

1510
class ConfigPlan(CommandScaffold):
@@ -47,7 +42,7 @@ def parser(parser):
4742
required=False,
4843
)
4944

50-
def run(self, template_file="main.conf.j2"):
45+
def run(self):
5146
"""
5247
cli execution
5348
@@ -56,36 +51,37 @@ def run(self, template_file="main.conf.j2"):
5651
:returns: result string
5752
"""
5853
self.nr = self._inventory()
59-
60-
for host in self.nr.inventory.hosts:
61-
compiled_template = CompileTemplate(
62-
nr=self.nr, host=host, template=template_file
63-
)
64-
output_writer(
65-
nornir_results=compiled_template.render(), output_file="compiled.conf",
66-
)
67-
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)
6862

6963
if self.args.compile:
70-
if self.args.host:
71-
return compiled_template.render()
72-
message = TextColor.green("templates compiled for all hosts")
73-
return logging.info(message)
64+
return results
7465

75-
networking = Networking(nr=self.nr)
76-
running_config = networking.fetch(commands="show running")
77-
output_writer(nornir_results=running_config, output_file="running.conf")
78-
print_result(running_config)
66+
results = self.nr.run(
67+
task=netmiko_send_commands,
68+
commands="show running",
69+
name="FETCH RUNNING CONFIG",
70+
)
71+
output_writer(nornir_results=results, output_file="running.conf")
72+
print_result(results)
7973

80-
result = self.nr.run(
74+
results = self.nr.run(
8175
task=hier_host,
8276
include_tags=self.args.include_tags,
8377
exclude_tags=self.args.exclude_tags,
8478
running_config="running.conf",
8579
compiled_config="compiled.conf",
8680
config_path=OUTPUT_DIR,
8781
load_file=True,
82+
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/fetch/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
from netnir.helpers.scaffold.subcommand import SubCommandParser
22

33

4-
"""fetch subcommands initialization
5-
"""
6-
7-
84
class Fetch(SubCommandParser):
95
"""
106
fetch subcommand parser

0 commit comments

Comments
 (0)