A Nornir plugin enforcing concurrency limits based on device groups
Project description
Nornir Conditional Runner
The ConditionalRunner is a custom Nornir runner that enforces concurrency limits based on host groups. It allows you to control task execution by defining limits on the number of simultaneous tasks for specific groups of hosts, ensuring your Nornir tasks do not update vital network devices simultaneously. You can also specify to skip the rest of the group if a certain number of vital tasks fail. It is built on the threaded runner, with added conditional group_limits and group_fail_limits managed internally by a data structure consisting of semaphores, conditions and counters, allowing tasks to remain idle in a waiting state until the start conditions are met.
Installation
pip install nornir-conditional-runner
Usage
Replace the default Nornir runner with ConditionalRunner in your configuration:
from nornir import InitNornir
nr = InitNornir(
runner={
"plugin": "ConditionalRunner", # Add the ConditionalRunner plugin to your nornir config / config.yaml
"options": {
"num_workers": 10, # Maximum number of concurrent tasks
"group_limits": {
"core": 1, # Limit the "core" group to 1 concurrent task
"distribution": 2,
"edge": 3,
},
# Group fail limits for each group (optional) - once exceeded, the still waiting tasks are skipped
"group_fail_limits": {
"core": 1, # Only allow one core device to fail
"edge": 2,
},
"conditional_group_key": "conditional_groups", # Custom key for conditional groups config in host data
"skip_unspecified_group_on_failure": True, # Sets the fail limit to 1 for all groups which do not have a group_fail_limit
},
inventory={
"plugin": "SimpleInventory",
"options": {
"host_file": "demo/inventory/hosts.yaml",
"group_file": "demo/inventory/groups.yaml",
},
},
)
def my_task(task):
return f"Running on {task.host.name}"
result = nr.run(task=my_task)
print(result)
Host Example
Hosts can define custom groups in their data dictionary using the conditional_group_key provided in the runner options. The runner will use these groups to enforce the group_limits.
host1:
data:
conditional_groups:
- core
host2:
data:
conditional_groups:
- distribution
If the conditional_group_key is not provided, the runner will default to using the host groups.
host1:
groups:
- core
host2:
groups:
- edge
Fail Limits Feature
The group_fail_limits option allows you to specify the maximum number of failed tasks for a group before the runner skips the rest of the waiting tasks in a group. This feature is useful when you want to limit the impact of failing tasks on your network. By example, if one core device fails, you may want to skip the rest of the core devices to avoid further issues. The runner will only skip the tasks that are still waiting to run, not the ones that are already running.
The skip_unspecified_group_on_failure option sets the fail limit to 1 for all groups which do not have a group_fail_limit specified. This default behavior can be overridden by specifying skip_unspecified_group_on_failure = False, whitch will cause the runner to not skip the unspecified groups on failure. The specified group_fail_limits will allways be used to skip the group on failure.
Logging
The ConditionalRunner leverages Python's built-in logging system to provide insights into its operation. It logs key events, such as:
- Warnings, when a group is configured on a host but it is missing in
group_limits, defaulting to the global limit. - Warnings, when an invalid or missing
conditional_group_keycauses a fallback to host groups. - Warnings if the
group_fail_limitsfor a group are met or exceeded.
Demo
Three short demos can be found in the demo/demo.py file.
Demo topology with conditional groups:
Error Handling / fallback to default behavior of the threaded runner
- If the
conditional_group_keyis provided but no conditional groups are defined in the host data, the runner will warn you and default to using the host groups as the conditional groups. - If no
group_limitsare specified for a group, the runner will default to using the globalnum_workersvalue as the limit. - If neither
group_limitsnor aconditional_group_keyare provided, the runner will fall back to using the host groups as conditional groups, with the default limits set to the globalnum_workers. -> This behavior then basically mirrors that of the default threaded Nornir runner. - Invalid group limits (i.e., non-positive integers) will result in a ValueError.
Contributing
Contributions are welcome! Feel free to submit issues or feature requests on GitHub.
Enjoy using the Nornir Conditional Runner! 🎉
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nornir_conditional_runner-0.1.6.tar.gz.
File metadata
- Download URL: nornir_conditional_runner-0.1.6.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22fb4cc3c5f7b7c33762bd195ca5c31758203dee1552d57dbf83b435e0df2ac6
|
|
| MD5 |
28c7cffe7cfe856acc0bf4556d275b69
|
|
| BLAKE2b-256 |
1fca0102067158dfb88c9fd2c6f17ddad658854f4e2aad0998b7582e1778038f
|
File details
Details for the file nornir_conditional_runner-0.1.6-py3-none-any.whl.
File metadata
- Download URL: nornir_conditional_runner-0.1.6-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
889f3ba1911f5c0aabbf56d2e55e664db6abfdc46a7a98864e0506e68d098711
|
|
| MD5 |
d4e19d010d5dd9a67ef9a4d7fabb2dc5
|
|
| BLAKE2b-256 |
ac7c282f6e06381aee8324a180d131c63a4544ce3b1759300fbb33b926ff13ab
|