# neelimapp/nornir-sample-scripts architecture

> Architecture diagram of the GitHub repository neelimapp/nornir-sample-scripts, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/neelimapp/nornir-sample-scripts
- Repository: https://github.com/neelimapp/nornir-sample-scripts

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Automation Scripts
    subgraph "Local Automation Scripts"
        SM1["nornir_main.py (sample1)"]:::config
        SM2["nornir_main.py (sample2)"]:::config
        SM3["nornir_main.py (sample3)"]:::config
        SM4["nornir_main.py (sample4)"]:::config
    end

    %% Core Engine
    NCORE["Nornir Core Engine"]:::core

    %% Plugins
    subgraph "Plugins"
        INVPLUGIN["Inventory Plugin"]:::plugin
        RUNPLUGIN["Runner Plugin (threaded)"]:::plugin
        subgraph "Task Plugins"
            NAPALM["Napalm Plugins"]:::plugin
            UTILS["Utils Plugins"]:::plugin
        end
        PROC["Processor Plugin"]:::plugin
    end

    %% Configuration Files
    subgraph "Configuration Files"
        CFG1["config.yaml (sample1)"]:::config
        CFG3["config.yaml (sample3)"]:::config
        CFG4["config.yaml (sample4)"]:::config
        HOSTS2["hosts.yaml (sample2)"]:::config
    end

    %% Inventory Definitions
    subgraph "Inventory Definitions"
        H1["hosts.yaml (sample1)"]:::config
        G1["groups.yaml (sample1)"]:::config
        D1["defaults.yaml (sample1)"]:::config
        H3["hosts.yaml (sample3)"]:::config
        G3["groups.yaml (sample3)"]:::config
        D3["defaults.yaml (sample3)"]:::config
        H4["hosts.yaml (sample4)"]:::config
        G4["groups.yaml (sample4)"]:::config
        D4["defaults.yaml (sample4)"]:::config
    end

    %% External Systems
    subgraph "External Systems"
        subgraph "Libraries"
            LIB1["Napalm/Netmiko Libs"]:::external
        end
        subgraph "Network Devices"
            DEV["rt1, rt2, ..."]:::external
        end
    end

    %% CI/CD Workflow
    CI[".github/workflows/main.yml"]:::ci

    %% Connections
    SM1 -->|"InitNornir"| NCORE
    SM2 -->|"InitNornir"| NCORE
    SM3 -->|"InitNornir"| NCORE
    SM4 -->|"InitNornir"| NCORE

    NCORE -->|"loads"| INVPLUGIN
    NCORE -->|"configures"| RUNPLUGIN

    INVPLUGIN -->|"reads"| H1
    INVPLUGIN -->|"reads"| G1
    INVPLUGIN -->|"reads"| D1
    INVPLUGIN -->|"reads"| H3
    INVPLUGIN -->|"reads"| G3
    INVPLUGIN -->|"reads"| D3
    INVPLUGIN -->|"reads"| H4
    INVPLUGIN -->|"reads"| G4
    INVPLUGIN -->|"reads"| D4
    INVPLUGIN -->|"reads"| HOSTS2

    RUNPLUGIN -->|"spawns threads"| THREADPOOL[/"Thread Pool (num_workers)"/]:::core

    THREADPOOL -->|"executes"| NAPALM
    THREADPOOL -->|"executes"| UTILS

    NAPALM -->|"uses"| LIB1
    LIB1 -->|"connects"| DEV
    DEV -->|"sends results"| PROC
    UTILS -->|"processes results"| PROC
    PROC -->|"outputs to console"| SM1
    PROC -->|"outputs to console"| SM2
    PROC -->|"outputs to console"| SM3
    PROC -->|"outputs to console"| SM4

    CI -->|"triggers"| SM1
    CI -->|"triggers"| SM2
    CI -->|"triggers"| SM3
    CI -->|"triggers"| SM4

    %% Click Events
    click SM1 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample1/nornir_main.py"
    click SM2 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample2/nornir_main.py"
    click SM3 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample3/nornir_main.py"
    click SM4 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample4/nornir_main.py"
    click CFG1 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample1/config.yaml"
    click CFG3 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample3/config.yaml"
    click CFG4 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample4/config.yaml"
    click HOSTS2 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample2/hosts.yaml"
    click H1 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample1/inventory/hosts.yaml"
    click G1 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample1/inventory/groups.yaml"
    click D1 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample1/inventory/defaults.yaml"
    click H3 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample3/inventory/hosts.yaml"
    click G3 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample3/inventory/groups.yaml"
    click D3 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample3/inventory/defaults.yaml"
    click H4 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample4/inventory/hosts.yaml"
    click G4 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample4/inventory/groups.yaml"
    click D4 "https://github.com/neelimapp/nornir-sample-scripts/blob/main/sample4/inventory/defaults.yaml"
    click CI "https://github.com/neelimapp/nornir-sample-scripts/blob/main/.github/workflows/main.yml"
    click README "https://github.com/neelimapp/nornir-sample-scripts/blob/main/README.md"
    click LICENSE "https://github.com/neelimapp/nornir-sample-scripts/tree/main/LICENSE"
    click NOTICE "https://github.com/neelimapp/nornir-sample-scripts/tree/main/NOTICE"
    click CLONE "https://github.com/neelimapp/nornir-sample-scripts/blob/main/CLONE.md"

    %% Styles
    classDef core fill:#ADD8E6,stroke:#000;
    classDef plugin fill:#90EE90,stroke:#000;
    classDef config fill:#FFFF99,stroke:#000;
    classDef external fill:#FFCC99,stroke:#000;
    classDef ci fill:#D3D3D3,stroke:#000;
```
