Skip to content

Commit

Permalink
Add new functions
Browse files Browse the repository at this point in the history
- print_stat - Prints results statistic on stdout.
- write_result - Writes results to file.
- write_results - Writes results to files.
  • Loading branch information
timeforplanb123 committed Apr 3, 2022
1 parent f4b0f25 commit bb9a634
Show file tree
Hide file tree
Showing 8 changed files with 2,549 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ _________

* **print_result** - Formats nicely and prints results on stdout.
* **print_title** - Formats nicely a title and prints it on stdout.
* **print_stat** - Prints results statistic on stdout.
* **write_result** - Writes results to file.
* **write_results** - Writes results to files.

Processors
__________
Expand Down
220 changes: 220 additions & 0 deletions docs/source/tutorials/function_print_stat.ipynb
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
}
Loading

0 comments on commit bb9a634

Please sign in to comment.