{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "nbsphinx": "hidden" }, "outputs": [], "source": [ "# ignore this cell, this is just a helper cell to provide the magic %highlight_file\n", "%run ../highlighter.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Initializing Nornir\n", "\n", "Easiest way of initializing nornir is with the function [InitNornir](../api/nornir/__init__.html#nornir.__init__.InitNornir).\n", "\n", "With `InitNornir` you can initialize nornir with a configuration file, with code or with a combination of both.\n", "\n", "Let's start with [a configuration file](../configuration/index.html):" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
 1---\n",
       " 2inventory:\n",
       " 3    plugin: SimpleInventory\n",
       " 4    options:\n",
       " 5        host_file: "inventory/hosts.yaml"\n",
       " 6        group_file: "inventory/groups.yaml"\n",
       " 7        defaults_file: "inventory/defaults.yaml"\n",
       " 8runner:\n",
       " 9    plugin: threaded\n",
       "10    options:\n",
       "11        num_workers: 100\n",
       "
\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%highlight_file config.yaml" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now to create the [nornir](../api/nornir/init_nornir.html#nornir.init_nornir.InitNornir) object:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from nornir import InitNornir\n", "nr = InitNornir(config_file=\"config.yaml\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also initialize nornir programmatically without a configuration file:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from nornir import InitNornir\n", "nr = InitNornir(\n", " runner={\n", " \"plugin\": \"threaded\",\n", " \"options\": {\n", " \"num_workers\": 100,\n", " },\n", " },\n", " inventory={\n", " \"plugin\": \"SimpleInventory\",\n", " \"options\": {\n", " \"host_file\": \"inventory/hosts.yaml\",\n", " \"group_file\": \"inventory/groups.yaml\"\n", " },\n", " },\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or with a combination of both methods:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from nornir import InitNornir\n", "nr = InitNornir(\n", " config_file=\"config.yaml\",\n", " runner={\n", " \"plugin\": \"threaded\",\n", " \"options\": {\n", " \"num_workers\": 50,\n", " },\n", " },\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "50" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nr.config.runner.options[\"num_workers\"]" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 2 }