Table of Contents
- Overview
- Inventory Structure
- Example: SimpleInventory Files
- Inventory Plugin Options
- Key Recommendations
- Conclusion
Nornir Automation: Inventory Overview
What Is Nornir Inventory?
Nornir is a Python automation framework built for network engineers, and its inventory component is the brain behind all Nornir automations. The inventory is a structured database or configuration set that tells Nornir which devices you have, what they are, how to connect to them, and any metadata you need for effective management. It’s the central repository of all device and environment details your automation needs.
Why Is the Inventory So Important?
- Targeting Devices: The inventory defines every device you want to manage—routers, switches, firewalls—so you can automate tasks on all or any subset with surgical precision.
- Reducing Complexity: Instead of hardcoding connection info or device details into scripts, the inventory makes your automation modular, scalable, and easier to maintain.
- Consistency & Organization: Using structured formats (like YAML, CSV, or dynamic backends like NetBox), you ensure that device info is organized, standardized, and always accessible.
- Automation Flexibility: With metadata (e.g., locations, roles, environment tags), you can run automation for specific groups, sites, or use-cases, all within the same framework.
How Does Nornir Inventory Work?
- Layers: Nornir inventory is separated into multiple hierarchical layers—hosts, groups, and defaults:
- Hosts define individual devices and their connection parameters.
- Groups are collections of hosts sharing traits like platform, site, or role, allowing you to avoid redundant entries.
- Defaults cover global fallback values for settings used network-wide.
- Formats & Plugins:
- The most common format is YAML, using files like
hosts.yaml
,groups.yaml
, anddefaults.yaml
. - For dynamic environments, plugins integrate with sources like NetBox, CSV files, or Ansible inventories.
- The most common format is YAML, using files like
- Integration in Automation:
- When you run a Nornir script, it loads the inventory.
- Tasks are filtered and dispatched based on inventory data: you can select only Cisco routers in a certain site or all devices missing a compliance check.
- You can update inventory files or dynamic databases, and the changes take effect immediately in your automations.
- Security and Adaptability:
- Centralizing device credentials and secrets in the inventory enhances security and makes auditing easier.
- The modular design means new devices or configuration changes are reflected in inventory first, streamlining onboarding or migration projects.
In essence, the Nornir inventory is the foundation of reliable, flexible, and secure network automation. Mastering it means you dictate exactly how, where, and on what devices your automation runs, making your scripts—and your infrastructure—resilient and future-proof.
Inventory Structure
The Nornir inventory system is built on three layers that make up its hierarchical structure. Each layer contributes to how devices are defined, grouped, and managed in an automated environment.
-
Hosts:
These represent individual network devices (e.g., routers, switches, firewalls). Every host entry includes attributes like IP address, platform, credentials, and any custom metadata required for automation tasks. -
Groups:
Groups allow you to categorize multiple hosts with shared characteristics (like OS type, location, or vendor). Variables defined here are inherited by member hosts and may be overridden at the host level. -
Defaults:
This is a global configuration layer where fallback variables are defined for all hosts. If a host or group doesn’t specify a variable, Nornir pulls it from this layer.
The inventory is typically stored using the SimpleInventory
plugin in YAML format and structured like this:
inventory/
hosts.yaml
groups.yaml
defaults.yaml
Each file serves a different layer of the inventory. Here’s a breakdown:
- hosts.yaml: Contains the actual devices to manage, along with their credentials and device-specific data.
- groups.yaml: Defines collections of devices that share common characteristics such as vendors or locations.
- defaults.yaml: Stores reusable default values like usernames, passwords, or global platform definitions.
This structure provides a scalable, modular way to organize network devices for automation — whether you are managing ten devices or thousands across multiple data centers.
Example: SimpleInventory Files
Below is an example of how to build a SimpleInventory
directory using YAML. This is a common inventory format for small to medium-sized deployments and is perfect for getting started with Nornir.
Step 1: Create the Inventory Directory Structure
inventory/
├── hosts.yaml
├── groups.yaml
└── defaults.yaml
This structure separates host-specific settings, group variables, and global defaults for better readability and reusability.
Step 2: Define Devices in hosts.yaml
---
core_router1:
hostname: 192.168.1.1
port: 22
username: admin
password: secretpass
platform: ios
groups:
- cisco_routers
core_switch1:
hostname: 192.168.2.1
port: 22
username: admin
password: secretpass
platform: eos
groups:
- arista_switches
This file defines individual devices with attributes like IP, port, credentials, and group membership.
Step 3: Create Common Properties in groups.yaml
---
cisco_routers:
platform: ios
data:
location: datacenter-west
arista_switches:
platform: eos
data:
location: datacenter-east
Group files allow shared variables across multiple hosts. These can include physical location, vendor, or configuration templates.
Step 4: Set Global Defaults in defaults.yaml
---
username: defaultuser
password: defaultpass
Defaults are automatically applied to all hosts unless explicitly overridden at the host or group level.
This modular structure enhances scalability, reduces redundancy, and simplifies updates to shared variables across the environment.
Inventory Plugin Options
Nornir offers a wide selection of inventory plugins, allowing you to source device data from simple files or dynamic systems like NetBox, CSV, or Ansible. Here’s a step-by-step example of how to select and configure inventory plugins for different use cases.
Step 1: Choose the Inventory Plugin for Your Needs
-
SimpleInventory:
Uses YAML files (hosts.yaml
,groups.yaml
,defaults.yaml
). Ideal for straightforward, file-based setups. -
YAMLInventory:
Lets you manually specify YAML paths, offering more flexibility in file structure. -
AnsibleInventory:
Enables reusing existing Ansible inventory files, useful for teams migrating from Ansible. -
NetBoxInventory2:
Dynamically loads inventory data from a NetBox API, perfect for environments tracked with NetBox. -
CSVInventory / ExcelInventory:
Loads devices from CSV or Excel files for easy, tabular management at scale. -
NautobotInventory:
Integrates inventory from Nautobot’s automation platform for organizations using it as their source of truth.
Step 2: Configure Inventory Plugin in config.yaml
Below are configuration examples for several popular plugins:
# Example: SimpleInventory (YAML files)
inventory:
plugin: SimpleInventory
options:
host_file: 'inventory/hosts.yaml'
group_file: 'inventory/groups.yaml'
defaults_file: 'inventory/defaults.yaml'
# Example: NetBoxInventory2 (Dynamic NetBox inventory)
inventory:
plugin: NetBoxInventory2
options:
nb_url: https://netbox.local:8000
nb_token: "abcdefg12345"
# Example: AnsibleInventory (Reuse Ansible inventory files)
inventory:
plugin: AnsibleInventory
options:
hostsfile: "inventory.yaml"
# Example: CSVInventory (Tabular CSV format)
inventory:
plugin: CSVInventory
options:
csv_file: "inventory/devices.csv"
Each plugin may have its own options. Refer to the plugin’s documentation for all supported parameters.
Step 3: Initialize Nornir with the Configured Plugin
from nornir import InitNornir
nr = InitNornir(config_file="config.yaml")
With the chosen inventory plugin configured, Nornir will automatically load your devices and groups from the designated source, supporting both small static files and large dynamic datasets.
Key Recommendations
To maximize the efficiency, security, and scalability of your network automation with Nornir Inventory, follow these key recommendations:
Step 1: Maintain Complete Asset Visibility
- Ensure your inventory provides a comprehensive view of all network devices, including their locations, roles, configurations, and relationships. This visibility supports accurate targeting and expedites troubleshooting.
Step 2: Utilize Structured Grouping for Flexibility
- Organize devices into groups based on attributes such as platform, location, or function. Leverage group-level variables to reduce redundancy and simplify bulk operations.
Step 3: Emphasize Consistency and Standardization
- Use defaults and group variables to apply uniform configurations across devices. This approach reduces human error and ensures network-wide compliance.
Step 4: Leverage Dynamic Inventory and Integrations
- For rapidly changing or large environments, integrate Nornir with dynamic inventory sources (e.g., NetBox, IPAM, databases). This keeps your device inventory accurate and up to date with minimal manual intervention.
Step 5: Ensure Efficient Task Execution
- Base automation scripts on precise, up-to-date inventory data to ensure tasks are executed on the correct devices, reducing risk and increasing reliability.
Step 6: Prioritize Security and Risk Mitigation
- Regularly audit your inventory for outdated devices or configurations that may pose security risks, and automate remediation actions where possible.
Step 7: Plan for Adaptability and Growth
- Design your inventory structure to accommodate network changes, such as adding or relocating devices. Use automation workflows that can dynamically adapt to inventory updates.
By implementing these strategies, you ensure that your Nornir inventory remains a powerful foundation for all network automation efforts—driving efficiency, security, and operational consistency across your infrastructure.
Conclusion
Throughout this blog post, we’ve taken a deep dive into one of the most critical components of network automation with Nornir — the Inventory system. Whether you're managing a handful of routers or automating a global network, how you structure and maintain your inventory can significantly impact the reliability and scalability of your automation workflows.
Key Takeaways:
- Inventory Structure is Everything: Nornir’s hierarchy of
hosts
,groups
, anddefaults
allows for clean, reusable, and scalable inventory management. - SimpleInventory Gets You Started: YAML-based inventory is a great entry point that gives you full visibility and control over your device definitions.
- Plugins for Every Environment: From static files and Ansible inventories to dynamic NetBox and CSV integrations, Nornir supports various plugins to fit your existing workflows.
- Group and Default Variables Reduce Redundancy: Centralize common config elements using group and default files to keep your inventory DRY (Don't Repeat Yourself).
- Think in Context: Use metadata like location, role, or environment in your inventory to drive smarter, context-aware automation.
- Keep Inventory Secure and Sustainable: Always manage secrets responsibly, audit your device data regularly, and structure everything for adaptability and scale.
By following structured best practices, you can turn your Nornir inventory into a living source-of-truth that powers efficient, reliable, and secure network automation across your entire infrastructure.
Thanks for joining me on this walkthrough! I hope this post helps you build smarter inventory designs and inspires your next automation project.
—
Happy Automating! 🚀