From 8000c95551877d4848be667c8c70f38448202b78 Mon Sep 17 00:00:00 2001 From: Jeff Kala Date: Fri, 8 Mar 2024 21:49:23 -0600 Subject: [PATCH 1/3] force the enable call to allow many cisco ios platforms to work --- nornir_nautobot/plugins/tasks/dispatcher/default.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nornir_nautobot/plugins/tasks/dispatcher/default.py b/nornir_nautobot/plugins/tasks/dispatcher/default.py index 44b4747..4ebc1a1 100644 --- a/nornir_nautobot/plugins/tasks/dispatcher/default.py +++ b/nornir_nautobot/plugins/tasks/dispatcher/default.py @@ -459,7 +459,7 @@ def get_config( command = cls.config_command try: - result = task.run(task=netmiko_send_command, command_string=command) + result = task.run(task=netmiko_send_command, command_string=command, enable=True) except NornirSubTaskError as exc: if isinstance(exc.result.exception, NetmikoAuthenticationException): error_msg = f"`E1017:` Failed with an authentication issue: `{exc.result.exception}`" From 1f15aa999175de7363347bac593e4ffa454fb60d Mon Sep 17 00:00:00 2001 From: Jeff Kala Date: Thu, 28 Mar 2024 11:06:45 -0500 Subject: [PATCH 2/3] add enable default os env var to override --- docs/task/task.md | 9 ++++- examples/basic_with_task.py | 35 +++++++++++++++++++ .../plugins/tasks/dispatcher/default.py | 9 +++-- nornir_nautobot/utils/helpers.py | 22 ++++++++++++ 4 files changed, 71 insertions(+), 4 deletions(-) create mode 100755 examples/basic_with_task.py diff --git a/docs/task/task.md b/docs/task/task.md index d8f0d99..71cb9af 100644 --- a/docs/task/task.md +++ b/docs/task/task.md @@ -102,4 +102,11 @@ class DispatcherMixin: if isinstance(config_context, int): return config_context return cls.tcp_port -``` \ No newline at end of file +``` + +## Environment Variables + +| Environment Variable | Explanation | +| ----- | ----------- | +| NORNIR_NAUTOBOT_REVERT_IN_SECONDS | Amount in seconds to revert if a config based method fails. | +| NORNIR_NAUTOBOT_NETMIKO_ENABLE_DEFAULT | Override the default(True) to not automatically call the `enable` function before running commands. | diff --git a/examples/basic_with_task.py b/examples/basic_with_task.py new file mode 100755 index 0000000..a46ed72 --- /dev/null +++ b/examples/basic_with_task.py @@ -0,0 +1,35 @@ +from nornir import InitNornir +from nornir_nautobot.plugins.tasks.dispatcher import dispatcher +from nornir_utils.plugins.functions import print_result + +import logging + +LOGGER = logging.getLogger(__name__) + +my_nornir = InitNornir( + inventory={ + "plugin": "NautobotInventory", + "options": { + "nautobot_url": "http://localhost:8080/", + "nautobot_token": "0123456789abcdef0123456789abcdef01234567", + "filter_parameters": {"location": "Site 1"}, + "ssl_verify": False, + }, + }, +) +my_nornir.inventory.defaults.username = "jeff" +my_nornir.inventory.defaults.password = "cisco" + +for nr_host, nr_obj in my_nornir.inventory.hosts.items(): + network_driver = my_nornir.inventory.hosts[nr_host].platform + result = my_nornir.run( + task=dispatcher, + logger=LOGGER, + method="get_config", + obj=nr_host, + framework="netmiko", + backup_file="./ios.cfg", + remove_lines=None, + substitute_lines=None, + ) + print_result(result) diff --git a/nornir_nautobot/plugins/tasks/dispatcher/default.py b/nornir_nautobot/plugins/tasks/dispatcher/default.py index 4ebc1a1..40c6255 100644 --- a/nornir_nautobot/plugins/tasks/dispatcher/default.py +++ b/nornir_nautobot/plugins/tasks/dispatcher/default.py @@ -23,9 +23,8 @@ 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_nautobot.exceptions import NornirNautobotException -from nornir_nautobot.utils.helpers import make_folder +from nornir_nautobot.utils.helpers import make_folder, is_truthy _logger = logging.getLogger(__name__) @@ -459,7 +458,11 @@ def get_config( command = cls.config_command try: - result = task.run(task=netmiko_send_command, command_string=command, enable=True) + result = task.run( + task=netmiko_send_command, + command_string=command, + enable=is_truthy(os.getenv("NORNIR_NAUTOBOT_NETMIKO_ENABLE_DEFAULT", default="True")), + ) except NornirSubTaskError as exc: if isinstance(exc.result.exception, NetmikoAuthenticationException): error_msg = f"`E1017:` Failed with an authentication issue: `{exc.result.exception}`" diff --git a/nornir_nautobot/utils/helpers.py b/nornir_nautobot/utils/helpers.py index 4912758..d711913 100644 --- a/nornir_nautobot/utils/helpers.py +++ b/nornir_nautobot/utils/helpers.py @@ -31,3 +31,25 @@ def import_string(dotted_path): return getattr(importlib.import_module(module_name), class_name) except (ModuleNotFoundError, AttributeError): return None + + +def is_truthy(arg): + """Convert "truthy" strings into Booleans. + + Args: + arg (str): Truthy string (True values are y, yes, t, true, on and 1; false values are n, no, + f, false, off and 0. Raises ValueError if val is anything else. + + Examples: + >>> is_truthy('yes') + True + """ + if isinstance(arg, bool): + return arg + + val = str(arg).lower() + if val in ("y", "yes", "t", "true", "on", "1"): + return True + if val in ("n", "no", "f", "false", "off", "0"): + return False + return True From 4e89dcf4f26a14f640d63e2670a1d9e65dc976fd Mon Sep 17 00:00:00 2001 From: Jeff Kala Date: Thu, 28 Mar 2024 11:14:28 -0500 Subject: [PATCH 3/3] fix linting and other issues in new example --- examples/basic_with_task.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/examples/basic_with_task.py b/examples/basic_with_task.py index a46ed72..60c5c04 100755 --- a/examples/basic_with_task.py +++ b/examples/basic_with_task.py @@ -1,8 +1,11 @@ +"""Example with a actual dispatcher task.""" + +import logging +import os from nornir import InitNornir -from nornir_nautobot.plugins.tasks.dispatcher import dispatcher from nornir_utils.plugins.functions import print_result +from nornir_nautobot.plugins.tasks.dispatcher import dispatcher -import logging LOGGER = logging.getLogger(__name__) @@ -17,8 +20,8 @@ }, }, ) -my_nornir.inventory.defaults.username = "jeff" -my_nornir.inventory.defaults.password = "cisco" +my_nornir.inventory.defaults.username = os.getenv("NORNIR_USERNAME") +my_nornir.inventory.defaults.password = os.getenv("NORNIR_PASSWORD") for nr_host, nr_obj in my_nornir.inventory.hosts.items(): network_driver = my_nornir.inventory.hosts[nr_host].platform