diff --git a/.gitignore b/.gitignore index f5259f1..0f43244 100644 --- a/.gitignore +++ b/.gitignore @@ -73,3 +73,7 @@ dmypy.json # macos stuff .DS_Store */.DS_Store + +# nornir (or any other) log files from testing +.log +*/.log diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..3f05804 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,3 @@ +# Contributing + +Contributions are not expected, but are very welcome! Feel free to open PRs or Issues as needed. Any contributions would need to at a minimum successfully pass the GitHub Actions commit build (which basically just runs tox). diff --git a/Makefile b/Makefile index 6391d9c..989822e 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -test_unit: +tests: python -m pytest \ --cov=nornsible \ --cov-report html \ diff --git a/README.md b/README.md index dfe18d3..bb5b653 100644 --- a/README.md +++ b/README.md @@ -8,3 +8,131 @@ nornsible ======= Wrap Nornir with Ansible-like host/group limits and tagging! + +The idea behind nornsible is to allow for Nornir scripts to be written to operate on an entire environment, and then limited to a subset of host(s) based on simple command line arguments. Of course you can simply do this yourself, but why not let nornsible handle it for you! + +Nornsible provides the ability to -- via command line arguments -- filter Nornir inventories by hostname or group name, or both. There is also a handy flag to set the number of workers; quite useful for setting workers to 1 for troubleshooting purposes. + +Lastly nornsible supports the concept of tags. Tags correspond to the name of *custom* tasks and operate very much like tags in Ansible. Provide a list of tags to execute, and Nornsible will ensure that Nornir only runs those tasks. Provide a list of tags to skip, and Nornsible will ensure that Nornir only runs those not in that list. Easy peasy. + + +# How does nornsible work? + +Nornsible accepts an instantiated Nornir object as an argument and returns a slightly Nornir object. Nornsible sets the desired number of workers if applicable, and addts an attribute for "run_tags" and "skip_tags" based on your command line input. + +To take advantage of the tags feautre Nornsible provides a decorator that you can use to decorate your custom tasks. This decorator inspects the task being ran and checks the task name against the lists of run and skip tags. If the task is allowed, Nornsible simply allows the task to run as per normal, if it is *not* allowed, Nornsible will print a pretty message and move on. + + +# Caveats + +Nornsible breaks some things! Most notably it breaks "normal" Nornir filtering *after* the Nornir object is "nornsible-ified". This can probably be fixed but at the moment it doesn't seem like that big a deal, so I'm not bothering! + +If you want to do "normal" Nornir filtering -- do this *before* passing the nornir object to Nornsible. + +Nornsible, at the moment, can only wrap custom tasks. This can probably be imporved upon as well, but at the moment the decorator wrapping custom tasks solution seems to work pretty well. + + +# Installation + +Installation via pip: + +``` +pip install nornsible +``` + +To install from this repository: + +``` +pip install git+https://github.com/carlmontanari/nornsible +``` + +To install from source: + +``` +git clone https://github.com/carlmontanari/nornsible +cd nornsible +python setup.py install +``` + + +# Usage + +Import stuff: + +``` +from nornsible import InitNornsible, nornsible_task +``` + +Decorate custom tasks with `nornsible_task` if desired: + +``` +@nornsible_task +def my_custom_task(task): +``` + +Create your Nornir object and then pass it through InitNornsible: + +``` +nr = InitNornir(config_file="config.yaml") +nr = Init_Nornsible(nr) +``` + +Run a custom task wrapped by `nornsible_task`: + +``` +nr.run(task=my_custom_task) +``` + +Run your script with any of the following command line switches: + +| Purpose | Short Flag | Long Flag | Allowed Options +| -----------------|---------------|------------|-------------------| +| set num_workers | -w | --workers | integer | +| limit host(s) | -l | --limit | comma sep string | +| limit group(s) | -g | --groups | comma sep string | +| run tag(s) | -t | --tags | comma sep string | +| skip tag(s) | -s | --skip | comma sep string | + +To set number of workers to 1 for troubleshooting purposes: + +``` +python my_nornir_script.py -w 1 +``` + +To limit to the "sea" group (from your Nornir inventory): + +``` +python my_nornir_script.py -g sea +``` + +To run only the tasks named "create_configs" and "deploy_configs" (assuming you've wrapped all of your tasks with `nornsible_task`!): + +``` +python my_nornir_script.py -t create_configs,deploy_configs +``` + +To run only the tasks named "create_configs" and "deploy_configs" against the "sea-eos-1" host: + +``` +python my_nornir_script.py -t create_configs,deploy_configs -l sea-eos-1 +``` + + +# FAQ + +TBA, probably things though! + +# Linting and Testing + +## Linting + +This project uses [black](https://github.com/psf/black) for auto-formatting. In addition to black, tox will execute [pylama](https://github.com/klen/pylama), and [pydocstyle](https://github.com/PyCQA/pydocstyle) for linting purposes. I've also added docstring linting with [darglint](https://github.com/terrencepreilly/darglint) which has been quite handy! + +## Testing + +I broke testing into two main categories -- unit and integration. Unit is what you would expect -- unit testing the code. Integration testing is for things that test more than one "unit" (generally function) at a time. + + +# To Do + +- Add handling for "not" in host/group limit; i.e.: "-t !localhost" to run against all hosts *not* localhost. diff --git a/docs/nornsible/index.html b/docs/nornsible/index.html index 06121e3..dd32831 100644 --- a/docs/nornsible/index.html +++ b/docs/nornsible/index.html @@ -28,16 +28,24 @@
nornsible
-def Init_Nornsible(nr)
+
+def InitNornsible(nr)
-
Patch nornir object based on cli arguments
@@ -82,7 +90,7 @@ Raises
def Init_Nornsible(nr):
+def InitNornsible(nr):
"""
Patch nornir object based on cli arguments
@@ -109,6 +117,105 @@ Raises
return nr
+def nornsible_task(wrapped_func)
+
Decorate an "operation" – execute or skip the operation based on tags
+wrapped_func
tag_wrapper
N
/A
+# noqa
def nornsible_task(wrapped_func):
+ """
+ Decorate an "operation" -- execute or skip the operation based on tags
+
+ Args:
+ wrapped_func: function to wrap in tag processor
+
+ Returns:
+ tag_wrapper: wrapped function
+
+ Raises:
+ N/A # noqa
+
+ """
+
+ def tag_wrapper(task, *args, **kwargs):
+ if {wrapped_func.__name__}.intersection(task.nornir.skip_tags):
+ msg = f"---- {task.host} skipping task {wrapped_func.__name__} "
+ nornsible_task_message(msg)
+ return Result(host=task.host, result="Task skipped!", failed=False, changed=False)
+ if not task.nornir.run_tags:
+ return wrapped_func(task, *args, **kwargs)
+ if {wrapped_func.__name__}.intersection(task.nornir.run_tags):
+ return wrapped_func(task, *args, **kwargs)
+ msg = f"---- {task.host} skipping task {wrapped_func.__name__} "
+ nornsible_task_message(msg)
+ return Result(host=task.host, result="Task skipped!", failed=False, changed=False)
+
+ tag_wrapper.__name__ = wrapped_func.__name__
+ return tag_wrapper
+
+def nornsible_task_message(msg)
+
Handle printing pretty messages for nornsible_task decorator
+msg
N
/A
N
/A
+# noqa
def nornsible_task_message(msg):
+ """
+ Handle printing pretty messages for nornsible_task decorator
+
+ Args:
+ msg: message to beautifully print to stdout
+
+ Returns:
+ N/A
+
+ Raises:
+ N/A # noqa
+
+ """
+ LOCK.acquire()
+ try:
+ print(f"{Style.BRIGHT}{Back.CYAN}{Fore.WHITE}{msg}{'-' * (80 - len(msg))}")
+ finally:
+ LOCK.release()
+
def parse_cli_args(args)
-def process_tags(wrapped_func)
-
Decorate an "operation" – execute or skip the operation based on tags
-wrapped_func
tag_wrapper
N
/A
-# noqa
def process_tags(wrapped_func):
- """
- Decorate an "operation" -- execute or skip the operation based on tags
-
- Args:
- wrapped_func: function to wrap in tag processor
-
- Returns:
- tag_wrapper: wrapped function
-
- Raises:
- N/A # noqa
-
- """
-
- def tag_wrapper(task, *args, **kwargs):
- if set([wrapped_func.__name__]).intersection(task.nornir.skip_tags):
- msg = f"---- {task.host} skipping task {wrapped_func.__name__} "
- process_tags_messages(msg)
- return Result(host=task.host, result="Task skipped!", failed=False, changed=False)
- if not task.nornir.run_tags:
- return wrapped_func(task, *args, **kwargs)
- if set([wrapped_func.__name__]).intersection(task.nornir.run_tags):
- return wrapped_func(task, *args, **kwargs)
- msg = f"---- {task.host} skipping task {wrapped_func.__name__} "
- process_tags_messages(msg)
- return Result(host=task.host, result="Task skipped!", failed=False, changed=False)
-
- tag_wrapper.__name__ = wrapped_func.__name__
- return tag_wrapper
-nornsible.nornsible
import argparse
-import logging
import sys
import threading
-from colorama import Fore, Style, init
+from colorama import Back, Fore, init, Style
from nornir.core.task import Result
-session_log = logging.getLogger(__name__)
-
init(autoreset=True, strip=False)
LOCK = threading.Lock()
@@ -150,9 +147,9 @@ Module nornsible.nornsible
return conf
-def process_tags_messages(msg):
+def nornsible_task_message(msg):
"""
- Handle printing pretty messages for process_tags decorator
+ Handle printing pretty messages for nornsible_task decorator
Args:
msg: message to beautifully print to stdout
@@ -166,12 +163,12 @@ Module nornsible.nornsible
"""
LOCK.acquire()
try:
- print("{}{}{}{}".format(Style.BRIGHT, Fore.CYAN, msg, "-" * (80 - len(msg))))
+ print(f"{Style.BRIGHT}{Back.CYAN}{Fore.WHITE}{msg}{'-' * (80 - len(msg))}")
finally:
LOCK.release()
-def process_tags(wrapped_func):
+def nornsible_task(wrapped_func):
"""
Decorate an "operation" -- execute or skip the operation based on tags
@@ -187,23 +184,23 @@ Module nornsible.nornsible
"""
def tag_wrapper(task, *args, **kwargs):
- if set([wrapped_func.__name__]).intersection(task.nornir.skip_tags):
+ if {wrapped_func.__name__}.intersection(task.nornir.skip_tags):
msg = f"---- {task.host} skipping task {wrapped_func.__name__} "
- process_tags_messages(msg)
+ nornsible_task_message(msg)
return Result(host=task.host, result="Task skipped!", failed=False, changed=False)
if not task.nornir.run_tags:
return wrapped_func(task, *args, **kwargs)
- if set([wrapped_func.__name__]).intersection(task.nornir.run_tags):
+ if {wrapped_func.__name__}.intersection(task.nornir.run_tags):
return wrapped_func(task, *args, **kwargs)
msg = f"---- {task.host} skipping task {wrapped_func.__name__} "
- process_tags_messages(msg)
+ nornsible_task_message(msg)
return Result(host=task.host, result="Task skipped!", failed=False, changed=False)
tag_wrapper.__name__ = wrapped_func.__name__
return tag_wrapper
-def Init_Nornsible(nr):
+def InitNornsible(nr):
"""
Patch nornir object based on cli arguments
@@ -237,8 +234,8 @@ Module nornsible.nornsible
Functions
-
-def Init_Nornsible(nr)
+
+def InitNornsible(nr)
-
Patch nornir object based on cli arguments
@@ -260,7 +257,7 @@ Raises
Source code
-def Init_Nornsible(nr):
+def InitNornsible(nr):
"""
Patch nornir object based on cli arguments
@@ -287,6 +284,105 @@ Raises
return nr
+
+def nornsible_task(wrapped_func)
+
+
+Decorate an "operation" – execute or skip the operation based on tags
+Args
+
+wrapped_func
+- function to wrap in tag processor
+
+Returns
+
+tag_wrapper
+- wrapped function
+
+Raises
+
+N
/A
+# noqa
+-
+
+
+Source code
+def nornsible_task(wrapped_func):
+ """
+ Decorate an "operation" -- execute or skip the operation based on tags
+
+ Args:
+ wrapped_func: function to wrap in tag processor
+
+ Returns:
+ tag_wrapper: wrapped function
+
+ Raises:
+ N/A # noqa
+
+ """
+
+ def tag_wrapper(task, *args, **kwargs):
+ if {wrapped_func.__name__}.intersection(task.nornir.skip_tags):
+ msg = f"---- {task.host} skipping task {wrapped_func.__name__} "
+ nornsible_task_message(msg)
+ return Result(host=task.host, result="Task skipped!", failed=False, changed=False)
+ if not task.nornir.run_tags:
+ return wrapped_func(task, *args, **kwargs)
+ if {wrapped_func.__name__}.intersection(task.nornir.run_tags):
+ return wrapped_func(task, *args, **kwargs)
+ msg = f"---- {task.host} skipping task {wrapped_func.__name__} "
+ nornsible_task_message(msg)
+ return Result(host=task.host, result="Task skipped!", failed=False, changed=False)
+
+ tag_wrapper.__name__ = wrapped_func.__name__
+ return tag_wrapper
+
+
+
+def nornsible_task_message(msg)
+
+
+Handle printing pretty messages for nornsible_task decorator
+Args
+
+msg
+- message to beautifully print to stdout
+
+Returns
+
+N
/A
+-
+
+Raises
+
+N
/A
+# noqa
+-
+
+
+Source code
+def nornsible_task_message(msg):
+ """
+ Handle printing pretty messages for nornsible_task decorator
+
+ Args:
+ msg: message to beautifully print to stdout
+
+ Returns:
+ N/A
+
+ Raises:
+ N/A # noqa
+
+ """
+ LOCK.acquire()
+ try:
+ print(f"{Style.BRIGHT}{Back.CYAN}{Fore.WHITE}{msg}{'-' * (80 - len(msg))}")
+ finally:
+ LOCK.release()
+
+
def parse_cli_args(args)
@@ -473,105 +569,6 @@ Raises
return inv
-def process_tags(wrapped_func)
-
Decorate an "operation" – execute or skip the operation based on tags
-wrapped_func
tag_wrapper
N
/A
-# noqa
def process_tags(wrapped_func):
- """
- Decorate an "operation" -- execute or skip the operation based on tags
-
- Args:
- wrapped_func: function to wrap in tag processor
-
- Returns:
- tag_wrapper: wrapped function
-
- Raises:
- N/A # noqa
-
- """
-
- def tag_wrapper(task, *args, **kwargs):
- if set([wrapped_func.__name__]).intersection(task.nornir.skip_tags):
- msg = f"---- {task.host} skipping task {wrapped_func.__name__} "
- process_tags_messages(msg)
- return Result(host=task.host, result="Task skipped!", failed=False, changed=False)
- if not task.nornir.run_tags:
- return wrapped_func(task, *args, **kwargs)
- if set([wrapped_func.__name__]).intersection(task.nornir.run_tags):
- return wrapped_func(task, *args, **kwargs)
- msg = f"---- {task.host} skipping task {wrapped_func.__name__} "
- process_tags_messages(msg)
- return Result(host=task.host, result="Task skipped!", failed=False, changed=False)
-
- tag_wrapper.__name__ = wrapped_func.__name__
- return tag_wrapper
-
-def process_tags_messages(msg)
-
Handle printing pretty messages for process_tags decorator
-msg
N
/A
N
/A
-# noqa
def process_tags_messages(msg):
- """
- Handle printing pretty messages for process_tags decorator
-
- Args:
- msg: message to beautifully print to stdout
-
- Returns:
- N/A
-
- Raises:
- N/A # noqa
-
- """
- LOCK.acquire()
- try:
- print("{}{}{}{}".format(Style.BRIGHT, Fore.CYAN, msg, "-" * (80 - len(msg))))
- finally:
- LOCK.release()
-