Skip to content

Commit

Permalink
Adding the data-plugins into nornir_utils (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktbyers authored Jun 6, 2020
1 parent 553361a commit a8966ff
Show file tree
Hide file tree
Showing 66 changed files with 3,413 additions and 161 deletions.
13 changes: 10 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ _________
Functions
_________

* **print_result** - formats nicely and prints results on stdout
* **print_title** - formats nicely a title and prints it on stdout
* **print_result** - Formats nicely and prints results on stdout.
* **print_title** - Formats nicely a title and prints it on stdout.

Processors
__________

* **PrintResult** - formats nicely and prints results on stdoud as soon as they are made available
* **PrintResult** - Formats nicely and prints results on stdout as soon as they are made available.

Data
__________

* **echo_data** - Dummy task that echoes the data passed to it.
* **load_json** - Load a JSON file.
* **load_yaml** - Load a YAML file.
Binary file modified docs/doctrees/api/functions.doctree
Binary file not shown.
Binary file modified docs/doctrees/api/index.doctree
Binary file not shown.
Binary file modified docs/doctrees/api/inventory.doctree
Binary file not shown.
Binary file modified docs/doctrees/api/processors.doctree
Binary file not shown.
Binary file added docs/doctrees/api/tasks.doctree
Binary file not shown.
Binary file modified docs/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/doctrees/index.doctree
Binary file not shown.
99 changes: 99 additions & 0 deletions docs/doctrees/nbsphinx/tutorials/echo_data.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"# echo_data\n",
"\n",
"echo_data is a data plugin that echoes the data passed into it. It is useful in grouped_tasks debugging.\n",
"\n",
"## Example"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1m\u001b[36mgrouped_task********************************************************************\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[34m* dev1.group_1 ** changed : False **********************************************\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[32mvvvv grouped_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[32m---- dev1.group_1 ** changed : False ------------------------------------------- INFO\u001b[0m\n",
"\u001b[0m{'role': 'www'}\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[32m^^^^ END grouped_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[34m* dev2.group_1 ** changed : False **********************************************\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[32mvvvv grouped_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[32m---- dev2.group_1 ** changed : False ------------------------------------------- INFO\u001b[0m\n",
"\u001b[0m{'role': 'db'}\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[32m^^^^ END grouped_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[34m* dev3.group_2 ** changed : False **********************************************\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[32mvvvv grouped_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[32m---- dev3.group_2 ** changed : False ------------------------------------------- INFO\u001b[0m\n",
"\u001b[0m{'role': 'www'}\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[32m^^^^ END grouped_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[34m* dev4.group_2 ** changed : False **********************************************\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[32mvvvv grouped_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[32m---- dev4.group_2 ** changed : False ------------------------------------------- INFO\u001b[0m\n",
"\u001b[0m{'role': 'db'}\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[32m^^^^ END grouped_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u001b[0m\n",
"\u001b[0m"
]
}
],
"source": [
"from nornir import InitNornir\n",
"from nornir.core.filter import F\n",
"from nornir_utils.plugins.tasks.data import echo_data\n",
"from nornir_utils.plugins.functions import print_result\n",
"\n",
"nr = InitNornir(\n",
" inventory={\n",
" \"plugin\": \"SimpleInventory\",\n",
" \"options\": {\"host_file\": \"data/hosts.yaml\", \"group_file\": \"data/groups.yaml\"},\n",
" }\n",
")\n",
"nr = nr.filter(~F(name=\"dev5.no_group\"))\n",
"\n",
"\n",
"def grouped_task(task):\n",
" task.run(task=echo_data, name=task.host.name, role=task.host[\"role\"])\n",
"\n",
"\n",
"r = nr.run(task=grouped_task)\n",
"print_result(r)"
]
}
],
"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.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"\n",
"nr = InitNornir(\n",
" inventory={\n",
" \"plugin\":\"SimpleInventory\",\n",
" \"plugin\":\"YAMLInventory\",\n",
" \"options\": {\n",
" \"host_file\": \"data/hosts.yaml\",\n",
" \"group_file\": \"data/groups.yaml\",\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# inventory: SimpleInventory\n",
"# inventory: YAMLInventory\n",
"\n",
"\n",
"[SimpleInventory](../api/inventory.rst#nornir_utils.plugins.inventory.SimpleInventory) is an inventory plugin that builds the inventory from a few YAML files.\n",
"[YAMLInventory](../api/inventory.rst#nornir_utils.plugins.inventory.YAMLInventory) is an inventory plugin that builds the inventory from a few YAML files.\n",
"\n",
"## Data\n",
"\n",
Expand Down Expand Up @@ -203,7 +203,7 @@
"source": [
"## Example\n",
"\n",
"Once you have the yaml files, using the plugin is very simple, you just need to specify the plugin you want to use is `SimpleInventory` and where to find the files:"
"Once you have the yaml files, using the plugin is very simple, you just need to specify the plugin you want to use is `YAMLInventory` and where to find the files:"
]
},
{
Expand All @@ -216,7 +216,7 @@
"\n",
"nr = InitNornir(\n",
" inventory={\n",
" \"plugin\":\"SimpleInventory\",\n",
" \"plugin\":\"YAMLInventory\",\n",
" \"options\": {\n",
" \"host_file\": \"data/hosts.yaml\",\n",
" \"group_file\": \"data/groups.yaml\",\n",
Expand Down
99 changes: 99 additions & 0 deletions docs/doctrees/nbsphinx/tutorials/load_json.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"# load_json\n",
"\n",
"Loads a JSON file.\n",
"\n",
"## Example"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"----------------------------------------\n",
"Host: dev1.group_1\n",
"JSON data: ['8.8.8.8', '8.8.4.4', '1.1.1.1', '1.0.0.1']\n",
"----------------------------------------\n",
"\n",
"----------------------------------------\n",
"Host: dev2.group_1\n",
"JSON data: ['8.8.8.8', '8.8.4.4', '1.1.1.1', '1.0.0.1']\n",
"----------------------------------------\n",
"\n",
"----------------------------------------\n",
"Host: dev3.group_2\n",
"JSON data: ['8.8.8.8', '8.8.4.4', '1.1.1.1', '1.0.0.1']\n",
"----------------------------------------\n",
"\n",
"----------------------------------------\n",
"Host: dev4.group_2\n",
"JSON data: ['8.8.8.8', '8.8.4.4', '1.1.1.1', '1.0.0.1']\n",
"----------------------------------------\n",
"\n",
"----------------------------------------\n",
"Host: dev5.no_group\n",
"JSON data: ['8.8.8.8', '8.8.4.4', '1.1.1.1', '1.0.0.1']\n",
"----------------------------------------\n"
]
}
],
"source": [
"from nornir import InitNornir\n",
"from nornir_utils.plugins.tasks.data import load_json\n",
"\n",
"nr = InitNornir(\n",
" inventory={\n",
" \"plugin\": \"SimpleInventory\",\n",
" \"options\": {\"host_file\": \"data/hosts.yaml\", \"group_file\": \"data/groups.yaml\"},\n",
" }\n",
")\n",
"\n",
"r = nr.run(task=load_json, file=\"data/ip_addresses.json\")\n",
"for host, m_result in r.items():\n",
" print()\n",
" print(\"-\" * 40)\n",
" print(f\"Host: {host}\")\n",
" print(f\"JSON data: {m_result.result}\")\n",
" print(\"-\" * 40)"
]
}
],
"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.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
99 changes: 99 additions & 0 deletions docs/doctrees/nbsphinx/tutorials/load_yaml.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"# load_yaml\n",
"\n",
"Loads a YAML file.\n",
"\n",
"## Example"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"----------------------------------------\n",
"Host: dev1.group_1\n",
"YAML data: ['8.8.8.8', '8.8.4.4', '1.1.1.1', '1.0.0.1']\n",
"----------------------------------------\n",
"\n",
"----------------------------------------\n",
"Host: dev2.group_1\n",
"YAML data: ['8.8.8.8', '8.8.4.4', '1.1.1.1', '1.0.0.1']\n",
"----------------------------------------\n",
"\n",
"----------------------------------------\n",
"Host: dev3.group_2\n",
"YAML data: ['8.8.8.8', '8.8.4.4', '1.1.1.1', '1.0.0.1']\n",
"----------------------------------------\n",
"\n",
"----------------------------------------\n",
"Host: dev4.group_2\n",
"YAML data: ['8.8.8.8', '8.8.4.4', '1.1.1.1', '1.0.0.1']\n",
"----------------------------------------\n",
"\n",
"----------------------------------------\n",
"Host: dev5.no_group\n",
"YAML data: ['8.8.8.8', '8.8.4.4', '1.1.1.1', '1.0.0.1']\n",
"----------------------------------------\n"
]
}
],
"source": [
"from nornir import InitNornir\n",
"from nornir_utils.plugins.tasks.data import load_yaml\n",
"\n",
"nr = InitNornir(\n",
" inventory={\n",
" \"plugin\": \"SimpleInventory\",\n",
" \"options\": {\"host_file\": \"data/hosts.yaml\", \"group_file\": \"data/groups.yaml\"},\n",
" }\n",
")\n",
"\n",
"r = nr.run(task=load_yaml, file=\"data/ip_addresses.yaml\")\n",
"for host, m_result in r.items():\n",
" print()\n",
" print(\"-\" * 40)\n",
" print(f\"Host: {host}\")\n",
" print(f\"YAML data: {m_result.result}\")\n",
" print(\"-\" * 40)"
]
}
],
"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.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"\n",
"nr = InitNornir(\n",
" inventory={\n",
" \"plugin\":\"SimpleInventory\",\n",
" \"plugin\":\"YAMLInventory\",\n",
" \"options\": {\n",
" \"host_file\": \"data/hosts.yaml\",\n",
" \"group_file\": \"data/groups.yaml\",\n",
Expand Down
Binary file added docs/doctrees/tutorials/echo_data.doctree
Binary file not shown.
Binary file modified docs/doctrees/tutorials/function_print_result.doctree
Binary file not shown.
Binary file modified docs/doctrees/tutorials/function_print_title.doctree
Binary file not shown.
Binary file modified docs/doctrees/tutorials/index.doctree
Binary file not shown.
Binary file not shown.
Binary file added docs/doctrees/tutorials/load_json.doctree
Binary file not shown.
Binary file added docs/doctrees/tutorials/load_yaml.doctree
Binary file not shown.
Binary file modified docs/doctrees/tutorials/processor_PrintResult.doctree
Binary file not shown.
6 changes: 6 additions & 0 deletions docs/html/_sources/api/tasks.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tasks
=====

.. automodule:: nornir_utils.plugins.tasks.data
:members:
:undoc-members:
Loading

0 comments on commit a8966ff

Please sign in to comment.