Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a netmiko merge_config #170

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nornir_nautobot/plugins/inventory/nautobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def pynautobot_obj(self) -> pynautobot.core.api.Api:
token=self.nautobot_token,
threading=self.enable_threading,
verify=self.ssl_verify,
api_version=2.2,
)
self.api_session.params = {"depth": 1}

Expand Down
54 changes: 53 additions & 1 deletion nornir_nautobot/plugins/tasks/dispatcher/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from nornir_jinja2.plugins.tasks import template_file
from nornir_napalm.plugins.tasks import napalm_configure, napalm_get
from nornir_netmiko.tasks import netmiko_send_command
from nornir_netmiko.tasks import netmiko_send_command, netmiko_send_config

from nornir_nautobot.exceptions import NornirNautobotException
from nornir_nautobot.utils.helpers import make_folder
Expand Down Expand Up @@ -499,3 +499,55 @@ def get_config(
with open(backup_file, "w", encoding="utf8") as filehandler:
filehandler.write(running_config)
return Result(host=task.host, result={"config": running_config})

@classmethod
def merge_config(
cls,
task: Task,
logger,
obj,
config: str,
) -> Result:
"""Send configuration to merge on the device.

Args:
task (Task): Nornir Task.
logger (logging.Logger): Logger that may be a Nautobot Jobs or Python logger.
obj (Device): A Nautobot Device Django ORM object instance.
config (str): The config set.

Raises:
NornirNautobotException: Authentication error.
NornirNautobotException: Timeout error.
NornirNautobotException: Other exception.

Returns:
Result: Nornir Result object with a dict as a result containing what changed and the result of the push.
"""
logger.info("Config merge via netmiko starting", extra={"object": obj})
# Sending None to napalm_configure for revert_in will disable it, so we don't want a default value.

try:
push_result = task.run(
task=netmiko_send_config,
config_commands=config.splitlines(),
enable=True,
)
except NornirSubTaskError as exc:
jeffkala marked this conversation as resolved.
Show resolved Hide resolved
error_msg = f"`E1015:` Failed with an unknown issue. `{exc.result.exception}`"
logger.error(error_msg, extra={"object": obj})
raise NornirNautobotException(error_msg)

logger.info(
f"result: {push_result[0].result}, changed: {push_result.changed}",
extra={"object": obj},
)

if push_result.diff:
logger.info(f"Diff:\n```\n_{push_result.diff}\n```", extra={"object": obj})

logger.info("Config merge ended", extra={"object": obj})
return Result(
host=task.host,
result={"changed": push_result.changed, "result": push_result[0].result},
)