How neelimapp/nornir-sample-scripts works
Project Type and Purpose
• This is an open-source Python network-automation framework and tutorial repository based on Nornir.
• Its purpose is to demonstrate how to configure and run Nornir workflows against network devices using YAML-based inventories and multiple plugins (napalm, utils).
File Structure Analysis
• Top‐level files:
• README.md, LICENSE, NOTICE – documentation and legal.
• .github/workflows/main.yml – CI pipeline to test samples.
• Sample directories (sample1–sample4): each is a self-contained Nornir project:
• config.yaml or hosts.yaml – defines inventory plugin and/or runner.
• inventory/hosts.yaml, groups.yaml, defaults.yaml – host definitions, group defaults.
• nornir_main.py – the Python entry point that calls InitNornir and runs tasks.
• Patterns: each sample shows different usage modes:
– sample1,3,4 use a separate config file + inventory directory.
– sample2 shows single hosts.yaml fallback.
– sample3/4 demonstrate multi‐task functions and filtering.
README Insights
• Describes Nornir’s components: core engine, inventory plugin, runner plugin (threaded), napalm and utils plugins.
• Outlines how tasks (napalm_get, napalm_cli, napalm_configure, etc.) integrate.
• Emphasizes multithreading: concurrent execution per host.
a. Main Components
1. Local Automation Script (nornir_main.py)
2. Nornir Core Engine
3. Inventory Plugin (SimpleInventory / YAMLInventory)
4. Runner Plugin (threaded)
5. Task Plugins:
– napalm plugins (napalm_get, napalm_cli, napalm_configure, napalm_validate)
– utils plugins (print_result, PrintResult processor, echo_data, load_yaml, write_file)
6. Configuration Files: config.yaml, hosts.yaml, groups.yaml, defaults.yaml
7. External Network Devices (rt1, rt2, …)
8. CI/CD Workflow (.github/workflows)
b. Relationships & Data Flow
• The Automation Script initializes Nornir with config.yaml (inventory + runner settings).
• Nornir Core loads the Inventory Plugin, reads hosts/groups/defaults YAML.
• Nornir Core then applies filters (if any) to select subset of hosts.
• Runner Plugin spins up a thread pool (num_workers) – one worker per host.
• Each thread executes the user-defined Task(s) from napalm or utils plugins.
• Task Plugins open connections (Netmiko/Napalm layers), issue commands or configurations to External Devices, collect results.
• Processor plugins format and print results back to the console.
• CI pipeline runs sample scripts to validate correctness on push.
c. Architectural Patterns & Principles
• Plugin-based modularity: clear separation of inventory, runner, task, and processor plugins.
• Thread-per-host concurrency model for parallel execution.
• YAML-driven configuration for inventory decoupling.
• Functional decomposition: each sample script is a small function (multiple_tasks, etc.).
d. Key Technologies & Libraries
• Nornir core (Python)
• nornir-utils plugin
• nornir-napalm plugin (Napalm connections & tasks)
• YAML for inventory (SimpleInventory plugin)
• Python threading via runner plugin
Details to Capture
• Thread pool size (num_workers) and parallelism.
• Sequence: InitNornir → Inventory load → Filter → run() → task.run() → connection → execution → result.
• Sample variants: direct hosts.yaml vs. config.yaml + inventory directory.
• CI workflow triggering validation of all sample scripts.