-
-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new functions and update print_result #21
Open
timeforplanb123
wants to merge
8
commits into
nornir-automation:master
Choose a base branch
from
timeforplanb123:update_functions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ff43dd0
Add new functions
timeforplanb123 8ddad99
Fix new functions
timeforplanb123 334e5a3
Merge branch 'master' into update_functions
timeforplanb123 ea3e4ec
Change new functions logic
timeforplanb123 624992f
Merge pull request #1 from nornir-automation/master
timeforplanb123 6619b68
Update type hints in print_result.py
timeforplanb123 88ca0f2
Update type hints in write_result.py and write_results.py
timeforplanb123 760902b
Add print_stat.py black reformatted
timeforplanb123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "57c50027", | ||
"metadata": {}, | ||
"source": [ | ||
"# functions: print_stat.py\n", | ||
"\n", | ||
"[print_stat](../api/functions.rst#nornir_utils.plugins.functions.print_stat) is a function that prints result statistic:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "846f5194", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from nornir import InitNornir\n", | ||
"\n", | ||
"def dummy_task(task):\n", | ||
" return \"hi!!!\"\n", | ||
"\n", | ||
"nr = InitNornir(\n", | ||
" inventory={\n", | ||
" \"plugin\":\"YAMLInventory\",\n", | ||
" \"options\": {\n", | ||
" \"host_file\": \"data/hosts.yaml\",\n", | ||
" \"group_file\": \"data/groups.yaml\",\n", | ||
" \"defaults_file\": \"data/defaults.yaml\",\n", | ||
" }\n", | ||
" }\n", | ||
")\n", | ||
"\n", | ||
"result = nr.run(task=dummy_task)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "eaf219ba", | ||
"metadata": {}, | ||
"source": [ | ||
"Now we could print the result statistic easily with the `print_stat` function: " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "ab8f58ca", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"\u001b[1m\u001b[36mdummy_task**********************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev1.group_1 *****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev2.group_1 *****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev3.group_2 *****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev4.group_2 *****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev5.no_group ****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[32mOK : 5\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[33mCHANGED : 0\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[31mFAILED : 0\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from nornir_utils.plugins.functions import print_stat\n", | ||
"\n", | ||
"print_stat(result)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "03d245e5", | ||
"metadata": {}, | ||
"source": [ | ||
"If the task returns changed or failed results, you can see it:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "d6fa941a", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"\u001b[1m\u001b[36mdummy_task**********************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev1.group_1 *****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[33mdummy_task ok=1 changed=1 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev2.group_1 *****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[31mdummy_task ok=0 changed=0 failed=1 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev3.group_2 *****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev4.group_2 *****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[31mdummy_task ok=0 changed=0 failed=1 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev5.no_group ****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[32mOK : 3\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[33mCHANGED : 1\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[31mFAILED : 2\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"result[\"dev1.group_1\"][0].changed=True\n", | ||
"result[\"dev2.group_1\"][0].failed=True\n", | ||
"result[\"dev4.group_2\"][0].failed=True\n", | ||
"\n", | ||
"print_stat(result)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "948feecb", | ||
"metadata": {}, | ||
"source": [ | ||
"If `MultiResult` object contains multiple `Result` objects:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "0d811189", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"\u001b[1m\u001b[36msmart_task**********************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev1.group_1 *****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[31msmart_task ok=0 changed=0 failed=1 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[31mdummy_task ok=0 changed=0 failed=1 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev2.group_1 *****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[31msmart_task ok=0 changed=0 failed=1 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[31mdummy_task ok=0 changed=0 failed=1 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev3.group_2 *****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[31msmart_task ok=0 changed=0 failed=1 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[31mdummy_task ok=0 changed=0 failed=1 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev4.group_2 *****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[31msmart_task ok=0 changed=0 failed=1 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[31mdummy_task ok=0 changed=0 failed=1 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[34m* dev5.no_group ****************************************************************\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[31msmart_task ok=0 changed=0 failed=1 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[32mdummy_task ok=1 changed=0 failed=0 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m \u001b[1m\u001b[31mdummy_task ok=0 changed=0 failed=1 \u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[32mOK : 15\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[33mCHANGED : 0\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m\u001b[1m\u001b[31mFAILED : 10\u001b[0m\u001b[0m\u001b[0m\n", | ||
"\u001b[0m\u001b[0m\u001b[0m" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def smart_task(task):\n", | ||
" task.run(task=dummy_task)\n", | ||
" task.run(task=dummy_task)\n", | ||
" task.run(task=dummy_task)\n", | ||
" task.run(task=dummy_task, failed=True)\n", | ||
" return \"hihi!!!\"\n", | ||
"\n", | ||
"\n", | ||
"smart_task_result = nr.run(task=smart_task)\n", | ||
"\n", | ||
"print_stat(smart_task_result)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see a test printing all kind of objects directly. Right now you are printing an
AggregatedResult
in all the examples in here so it'd be nice to print directly the other types there (i.e.print_stats(results["my_device"])
andprint_stats(results["my_device"][0])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
MultiResult
andResult
objects tofunction_print_stat.ipynb
tutorial