Workflow Service Run Task¤
task api name:
run
Runs a workflow defined as a YAML file reference or an inline dictionary. Workflow steps call other NorFab services and collect the per-step job results under the workflow name.
Inputs¤
| Parameter | Required | Description |
|---|---|---|
workflow |
Yes | Workflow definition dictionary or URL to a YAML workflow file |
Output¤
Returns workflow execution results keyed by workflow name and step name:
{
"workflow_1": {
"step1": {
"nornir-worker-1": {
"result": {"ceos-spine-1": {"show version": "..."}},
"failed": False,
"errors": [],
},
},
"step2": {
"nornir-worker-2": {
"result": {"ceos-leaf-1": {"show hostname": "..."}},
"failed": False,
"errors": [],
},
},
},
}
Workflow File Example¤
Workflow service run task uses YAML files to execute workflow steps:
name: workflow_1
description: Sample workflow with two steps.
step1:
service: nornir
task: cli
kwargs:
FC: spine
commands:
- show version
- show ip int brief
step2:
service: nornir
task: cli
kwargs:
FC: leaf
commands:
- show hostname
- show ntp status
Store the file on the broker, for example as nf://workflow/workflow-1.yaml, before running the workflow.
Examples¤
Run a workflow from a broker file:
nf#workflow run workflow nf://workflow/workflow-1.yaml
Run with a longer timeout:
nf#workflow run workflow nf://workflow/workflow-1.yaml timeout 900
Context manager - run from a broker file:
import pprint
from norfab.core.nfapi import NorFab
with NorFab(inventory="./inventory.yaml") as nf:
client = nf.make_client()
result = client.run_job(
service="workflow",
task="run",
workers="any",
kwargs={
"workflow": "nf://workflow/workflow-1.yaml",
},
)
pprint.pprint(result)
Direct lifecycle - run an inline workflow:
import pprint
from norfab.core.nfapi import NorFab
workflow = {
"name": "inline_workflow",
"description": "Collect basic command output from lab devices.",
"show_version": {
"service": "nornir",
"task": "cli",
"kwargs": {
"FC": "spine",
"commands": ["show version"],
},
},
"show_hostname": {
"service": "nornir",
"task": "cli",
"kwargs": {
"FC": "leaf",
"commands": ["show hostname"],
},
},
}
nf = NorFab(inventory="./inventory.yaml")
try:
nf.start()
client = nf.make_client()
result = client.run_job(
service="workflow",
task="run",
workers="any",
kwargs={"workflow": workflow},
)
pprint.pprint(result)
finally:
nf.destroy()
NORFAB Workflow Run Command Shell Reference¤
NorFab shell supports these command options for Workflow run task:
nf# man tree workflow
root
└── workflow: Workflow service
└── run: Run workflows
├── timeout: Job timeout
├── workers: Filter worker to target, default 'all'
├── workflow: Workflow to run
└── progress: Display progress events, default 'True'
nf#
Python API Reference¤
Executes a workflow defined by a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
NorFab Job object containing relevant metadata. |
required |
workflow
|
Union[str, Dict]
|
The workflow to execute. This can be a URL to a YAML file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Result
|
A dictionary containing the results of the workflow execution. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the workflow is not a valid URL or dictionary. |
Source code in norfab\workers\workflow_worker\workflow_worker.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |