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

Commit 10f7923

Browse files
committed
convert to native netmiko task runner
1 parent 7c6ec3d commit 10f7923

8 files changed

Lines changed: 82 additions & 165 deletions

File tree

netnir/core/inventory.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def __init__(self, **kwargs):
2121
)
2222

2323
def nhosts(self):
24-
from netnir.helpers import device_mapper
2524
from netnir.core.credentials import Credentials
2625
from netnir.constants import (
2726
HOSTVARS,
@@ -47,7 +46,7 @@ def nhosts(self):
4746
"username": host_vars.get("username", creds["username"]),
4847
"password": host_vars.get("password", creds["password"]),
4948
"port": host_vars.get("port", 22),
50-
"platform": device_mapper(host_vars["os"]),
49+
"platform": host_vars["os"],
5150
"groups": host_vars.get("groups", list()),
5251
"data": {
5352
**host_vars,
@@ -61,18 +60,6 @@ def nhosts(self):
6160
),
6261
"mgmt_protocol": host_vars.get("mgmt_protocol", "ssh"),
6362
},
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-
},
7663
}
7764

7865
return data

netnir/core/networking.py

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

netnir/core/tasks/config_plan.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from netnir.helpers.scaffold.command import CommandScaffold
22
from netnir.core.template import CompileTemplate
3-
from netnir.core.networking import Networking
3+
from netnir.plugins.netmiko import netmiko_send_commands
44
from netnir.helpers import output_writer, TextColor
55
from netnir.plugins.hier import hier_host
66
from netnir.constants import OUTPUT_DIR
@@ -72,10 +72,13 @@ def run(self, template_file="main.conf.j2"):
7272
message = TextColor.green("templates compiled for all hosts")
7373
return logging.info(message)
7474

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)
75+
results = self.nr(
76+
task=netmiko_send_commands,
77+
commands="show running",
78+
name="FETCH RUNNING CONFIG",
79+
)
80+
output_writer(nornir_results=results, output_file="running.conf")
81+
print_result(results)
7982

8083
result = self.nr.run(
8184
task=hier_host,
@@ -85,6 +88,7 @@ def run(self, template_file="main.conf.j2"):
8588
compiled_config="compiled.conf",
8689
config_path=OUTPUT_DIR,
8790
load_file=True,
91+
name="RENDER REMEDIATION CONFIG",
8892
)
8993

9094
print_result(result)

netnir/core/tasks/fetch/config.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from netnir.helpers.scaffold.command import CommandScaffold
22
from netnir.helpers.common.args import num_workers
33
from netnir.helpers import output_writer
4-
from netnir.core.networking import Networking
4+
from netnir.plugins.netmiko import netmiko_send_commands
55
from nornir.plugins.functions.text import print_result
66

77
"""fetch remove device configs"""
@@ -28,8 +28,7 @@ def run(self):
2828
"""
2929

3030
self.nr = self._inventory()
31-
networking = Networking(nr=self.nr, num_workers=self.args.workers)
32-
results = networking.fetch(commands="show running")
31+
results = self.nr.run(task=netmiko_send_commands, commands="show running")
3332
output_writer(nornir_results=results, output_file="running.conf")
3433

3534
return print_result(results)

netnir/core/tasks/ssh.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
from netnir.helpers.scaffold.command import CommandScaffold
2-
from netnir.core.networking import Networking
2+
from netnir.plugins.netmiko import netmiko_send_commands, netmiko_send_config
33
from netnir.helpers import output_writer
44
from netnir.helpers.common.args import (
55
output,
6-
num_workers,
76
commands,
87
config,
98
)
109
from nornir.plugins.functions.text import print_result
11-
import sys
1210

1311
"""ssh cli commands"""
1412

@@ -25,7 +23,6 @@ def parser(parser):
2523
"""
2624
CommandScaffold.parser(parser)
2725
output(parser)
28-
num_workers(parser)
2926
commands(parser)
3027
config(parser)
3128

@@ -35,14 +32,19 @@ def run(self):
3532
"""
3633

3734
self.nr = self._inventory()
38-
networking = Networking(nr=self.nr, num_workers=self.args.workers,)
3935

4036
if self.args.config:
41-
results = networking.config(self.args.commands)
42-
elif self.args.commands:
43-
results = networking.fetch(self.args.commands)
37+
results = self.nr.run(
38+
task=netmiko_send_config,
39+
commands=self.args.commands,
40+
name="SSH CONFIG EXECUTION",
41+
)
4442
else:
45-
sys.exit()
43+
results = self.nr.run(
44+
task=netmiko_send_commands,
45+
commands=self.args.commands,
46+
name="SSH COMMAND EXECUTION",
47+
)
4648

4749
if isinstance(results, str):
4850
results = [results]

netnir/plugins/netmiko.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from nornir.core.task import Task, Result
2+
from netmiko import ConnectHandler
3+
from netnir.helpers import device_mapper
4+
5+
"""netmiko (SSH) nornir tasks"""
6+
7+
8+
def netmiko_send_commands(task: Task, commands: list()):
9+
"""send show commands to a device via netmiko
10+
11+
:param task: nornir Task object
12+
:param commands: a list of commands to execute
13+
14+
:returns: nornir Result object
15+
"""
16+
device_params = {
17+
"host": task.host.hostname,
18+
"device_type": device_mapper(os_type=task.host.platform, proto="netmiko"),
19+
"port": task.host.port,
20+
"username": task.host.username,
21+
"password": task.host.password,
22+
"secret": task.host.connection_options["netmiko"]["extras"].get("secret", None),
23+
}
24+
25+
with ConnectHandler(**device_params) as conn:
26+
output = str()
27+
28+
for command in commands:
29+
output += conn.send_command(command)
30+
31+
return Result(host=task.host, result=output)
32+
33+
34+
def netmiko_send_config(task: Task, commands: list()):
35+
"""execute configuration changes on a device via netmiko
36+
37+
:param task: nornir Task object
38+
:param commands: a list of commands to execute
39+
40+
:returns: nornir Result object
41+
"""
42+
device_params = {
43+
"host": task.host.hostname,
44+
"device_type": device_mapper(os_type=task.host.platform, proto="netmiko"),
45+
"port": task.host.port,
46+
"username": task.host.username,
47+
"password": task.host.password,
48+
"secret": task.host.connection_options["netmiko"]["extras"].get("secret", None),
49+
}
50+
51+
with ConnectHandler(**device_params) as conn:
52+
output = conn.send_config_set(commands)
53+
output += conn.save()
54+
55+
return Result(task.host, result=output)

tests/netnir/core/test_inventory.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def test_inventory(initial_setup):
88
"port": 22,
99
"username": os.environ.get("NETNIR_USER"),
1010
"password": os.environ.get("NETNIR_PASS"),
11-
"platform": "cisco_xr",
11+
"platform": "iosxr",
1212
"groups": ["dc1"],
1313
"data": {
1414
"groups": ["dc1"],
@@ -19,23 +19,14 @@ def test_inventory(initial_setup):
1919
"template_path": "./tests/data/templates/provider1/iosxr",
2020
"mgmt_protocol": "ssh",
2121
},
22-
"connection_options": {
23-
"netconf": {
24-
"hostname": "router.dc1.example.net",
25-
"username": os.environ.get("NETNIR_USER"),
26-
"password": os.environ.get("NETNIR_PASS"),
27-
"platform": "iosxr",
28-
"port": 830,
29-
"extras": {"hostkey_verify": False},
30-
},
31-
},
22+
"connection_options": {},
3223
}
3324
router2 = {
3425
"hostname": "router.dc2.example.net",
3526
"port": 22,
3627
"username": os.environ.get("NETNIR_USER"),
3728
"password": os.environ.get("NETNIR_PASS"),
38-
"platform": "cisco_ios",
29+
"platform": "ios",
3930
"groups": ["dc2"],
4031
"data": {
4132
"groups": ["dc2"],
@@ -46,16 +37,7 @@ def test_inventory(initial_setup):
4637
"template_path": "./tests/data/templates/provider2/ios",
4738
"mgmt_protocol": "ssh",
4839
},
49-
"connection_options": {
50-
"netconf": {
51-
"hostname": "router.dc2.example.net",
52-
"username": os.environ.get("NETNIR_USER"),
53-
"password": os.environ.get("NETNIR_PASS"),
54-
"platform": "default",
55-
"port": 830,
56-
"extras": {"hostkey_verify": False},
57-
},
58-
},
40+
"connection_options": {},
5941
}
6042

6143
assert isinstance(inv.hosts, dict) is True

tests/netnir/core/test_networking.py

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

0 commit comments

Comments
 (0)