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 or custom condition 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 are not updating vital network devices at the same time. It is built on the threaded runner, with added group limits and conditional groups managed internally by semaphores and conditions, allowing tasks to remain idle until the 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", # Use the ConditionalRunner instead of the default
"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,
},
"conditional_group_key": "conditional_groups", # Custom key for conditional groups config in host data
},
},
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 no conditional groups are defined and conditional_group_key
is not provided to the runner, the runner will use the host groups.
host1:
groups:
- core
host2:
groups:
- edge
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 limit is missing in group_limits, defaulting to the global limit.
- Warnings when an invalid or missing conditional_group_key causes a fallback to host groups.
Demo
A demo can be found in the demo/demo.py file.
Demo topology with conditional groups:
Error Handling
- If conditional_group_key is provided but no conditional groups are defined in the host data, the runner will warn you and default to using the host groups as conditional groups.
- If no group_limits are specified for a group, the runner will default to using the global num_workers value as the limit.
- If neither group_limits nor a conditional_group_key are provided, the runner will fall back to using the host groups as conditional groups, with the default limits set to the global num_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
File details
Details for the file nornir_conditional_runner-0.1.5.tar.gz
.
File metadata
- Download URL: nornir_conditional_runner-0.1.5.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e305adef89c538275dcfb94202414e9a241836ccfbc5f077980edec44ddb1ca7 |
|
MD5 | 0c2aa14f15354329f7ab4e3303bca96d |
|
BLAKE2b-256 | ba4a21bf63718068e2f5ab82cfe6f1fed38d0b8da0cfc5c87b0db1a0d42a4bf5 |
File details
Details for the file nornir_conditional_runner-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: nornir_conditional_runner-0.1.5-py3-none-any.whl
- Upload date:
- Size: 5.5 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 | bfc14340ab985053786b074cb7feddd559c4faf96f11dfd8afe5cca96c8fe72f |
|
MD5 | b1b2a9c199031381ae2a2b9d47afb22e |
|
BLAKE2b-256 | dfb9707685b305580d8f162d53a63627f06cf3551957166ffb0b99c9a8d311f5 |