Use CSV files as a Nornir Inventory source with hosts, groups and defaults.

Overview

nornir_csv

published

Use CSV files as a Nornir Inventory source with hosts, groups and defaults. This can be used as an equivalent to the Simple Inventory plugin but using CSV files instead of YAML. This does not generate any new files, but instead reads host data from three files:

  • hosts.csv
  • groups.csv
  • defaults.csv

Installation

As of right now, the code hasn't been published to PyPi, so it must be installed from source. Be sure to clone the repository locally:

cd /directory/where/you/clone/git/stuff/
git clone https://github.com/matman26/nornir_csv.git

The nornir_csv directory can then be copied to your project root and added as an external library.

cp nornir_csv/nornir_csv /destination/project/root

Usage

Since this is an external plugin for Nornir 3.0+, it must be registered before usage. The main project file must therefore contain something similar to:

from nornir import InitNornir
from nornir_csv.plugins.inventory.csv_inventory import CsvInventory
from nornir.core.plugins.inventory import InventoryPluginRegister

InventoryPluginRegister.register("CsvInventoryPlugin", CsvInventory)

nr = InitNornir(config_file='sample_config.yaml')

By default, the plugin will look for the files hosts.csv, groups.csv and defaults.csv inside the ./inventory/ directory, but the directory can be changed by specifying the plugin option inventory_dir_path. A sample file such as below can be used:

inventory:
  plugin: CsvInventoryPlugin
  options:
    inventory_dir_path: /path/to/inventory/dir/
runner:
  plugin: threaded
  options:
    num_workers: 20

The name of the csv files to be read for hosts, groups and defaults can also be customized by setting the options hosts_file, groups_file and defaults_file, respectively. These should correspond to the file's basenames (no paths) with extension, if any.

CSV Syntax

Hosts

The hosts_file follows a specific syntax to make it nornir-compatible, see sample below:

name,hostname,username,password,port,platform,groups,custom_var
R1,192.168.122.10,cisco,cisco,22,cisco_ios,core main,foo
R2,192.168.122.20,cisco,cisco,22,cisco_xr,,bar

Note that name, hostname, username, password, port, platform and groups are nornir base attributes. This means they are hosts attributes directly, such that

nr.inventory.hosts['R1'].password

will yield the return value of 'cisco' as expected. Any custom variables that are added will be put inside the 'data' dictionary on the target host. So

nr.inventory.hosts['R1'].data['custom_var']

will return 'foo'.

Notice also that to specify the list of groups to which a host belongs the list must be specified one group at a time, separated by spaces. In the csv above, R1 belongs to the groups 'core' and 'main'. A hosts file is mandatory.

Groups

The groups_file is optional. It can be used to set default values for the base attributes of each host (for example, if every host of the same group uses the same credentials). Any attributes that are non-base attributes will be added to the 'data' container inside the generated group, similar to how it behaves with hosts. If no groups are specified in the csv file, hosts can still be assigned to groups but these will hold no data.

name,username,password,dns_server
core,cisco,cisco,8.8.8.8
main,,,,

Notice that the groups csv does not have any mandatory fields other than name.

Defaults

The defaults_file specifies any default variables. This file is also free-form, but is only composed of two lines: a header with the name of the variable and a second line with its value. The defaults file is optional.

message_of_the_day,foo,port
hello world!,bar,22
You might also like...
Based Telegram Bot and Userbot To Play Music in Your Telegram Groups With Some Cool Extra Features! 🥰
Based Telegram Bot and Userbot To Play Music in Your Telegram Groups With Some Cool Extra Features! 🥰

CallMusicPlus69 This Repo base on! 🤗️ A CallsMusic Based Telegram Bot and Userbot To Play Music in Your Telegram Groups With Some Cool Extra Features

This bot can mention members upto 10,000 in groups and can mention members upto 200 in channels !

Mention All Bot This bot can mention members upto 10,000 in groups and can mention members upto 200 in channels ! 🏷 Infomation Language: Python. Tele

❤️A next gen powerful telegram group manager bot for manage your groups and have fun with other cool modules
❤️A next gen powerful telegram group manager bot for manage your groups and have fun with other cool modules

Natsuki Based on Python Telegram Bot Contributors Video Tutorial: Complete guide on deploying @TheNatsukiBot's clone on Heroku. ☆ Video by Sadew Jayas

Telegram bot to stream videos in telegram voicechat for both groups and channels

Telegram bot to stream videos in telegram voicechat for both groups and channels. Supports live streams, YouTube videos and telegram media. With record stream support, Schedule streams, and many more.

Amanda-A next gen powerful telegram group manager bot for manage your groups and have fun with other cool modules.
Amanda-A next gen powerful telegram group manager bot for manage your groups and have fun with other cool modules.

Amanda-A next gen powerful telegram group manager bot for manage your groups and have fun with other cool modules.

Telegram bot to stream videos in telegram voicechat for both groups and channels.
Telegram bot to stream videos in telegram voicechat for both groups and channels.

Telegram bot to stream videos in telegram voicechat for both groups and channels. Supports live streams, YouTube videos and telegram media. With record stream support, Schedule streams, and many more.

Add members to unlimited telegram channels and groups
Add members to unlimited telegram channels and groups

Program Features 📌 Coded with Python version 10. 📌 without the need for a proxy. 📌 without the need for a Telegram ID. 📌 Ability to add infinite p

Telegram bot to stream videos in telegram Voice Chat for both groups and channels

Telegram bot to stream videos in telegram Voice Chat for both groups and channels. Supports live steams, YouTube videos and telegram media. Supports scheduling streams, recording and many more.

An interactive and multi-function Telegram bot, made especially for Telegram groups.

PyKorone An interaction and fun bot for Telegram groups, having some useful and other useless commands. Created as an experiment and learning bot but

Comments
  • Request: To have more complex csv with data and for nornir_scrapli usecases

    Request: To have more complex csv with data and for nornir_scrapli usecases

    I use more fields in yaml for example of host file below, how can this be documented in CSV? specially for headers like, data: connection_options:, scrapli:, and scrapli_netconf:


    rtr1: hostname: 10.91.11.11 data: interface: TenGigE0/0/0/16 localmepid: 1 remotemepid: 2 rd: 1600 evi: 2600 xrsw: 7.1.2 connection_options: scrapli: platform: cisco_iosxr port: 22 extras: ssh_config_file: True auth_strict_key: False scrapli_netconf: port: 830 extras: ssh_config_file: True auth_strict_key: False

    enhancement 
    opened by jbl241987 5
  • ModuleNotFoundError: No module named 'nornir_csv.plugins'

    ModuleNotFoundError: No module named 'nornir_csv.plugins'

    Hi, thanks for such plugin, I was looking for something like this for sometime. It is really simple, but when I try I get this error

    ModuleNotFoundError: No module named 'nornir_csv.plugins'

    I have the package installed though

    Package Version


    importlib-metadata 4.10.1 mypy-extensions 0.4.3 nornir 3.2.0 nornir-csv 0.1.3

    repo for reference https://github.com/jbl241987/Nornir_Project.git

    opened by jbl241987 5
  • [Bug] Default value handling for Hosts

    [Bug] Default value handling for Hosts

    thanks @matman26 , I will definitely try this option also.

    One more query, I have used hosts.csv as exactly shown in Readme. But i want to remove username and password from hosts.csv and keep only in defaults.csv or groups.csv

    I tried that but that doesn't work. Seems it is mandatory in hosts.csv.

    How can I make it work?

    Originally posted by @jbl241987 in https://github.com/matman26/nornir_csv/issues/2#issuecomment-1019105423

    bug 
    opened by matman26 2
Releases(v0.3)
Owner
Matheus Augusto da Silva
Compulsive-obsessive Bash scripter | Network Programmability and Automation
Matheus Augusto da Silva
SimpleTelegramScraper - A python script scrapes accounts from public groups via Telegram API and saves them in a CSV file

SimpleTelegramScraper - the best scraper on GitHub This simple python script scr

Deniz Shabani 12 Oct 6, 2022
Discord Bot for server hosts, devs, and admins. Analyzes timings reports & uploads text files to hastebin. Developed by https://birdflop.com.

"Botflop" Click here to invite Botflop to your server. Current abilities Analyze timings reports Paste a timings report to review an in-depth descript

Purpur 76 Dec 31, 2022
DB-Drive-CSV - This is app is can be used to access CSV file as JSON from Google Drive.

DB Drive CSV This is app is can be used to access CSV file as JSON from Google Drive. How To Use Create file/ upload file to Google Drive There's 2 fi

Hartawan Bahari M. 5 Oct 20, 2022
MAASTA is a wrapper to create an Ansible inventory for MAAS instances that are provisioned by Terraform.

MAASTA is a wrapper to create an Ansible inventory for MAAS instances that are provisioned by Terraform.

Saeid Bostandoust 144 Dec 16, 2022
If you are in allot of groups or channel and you would like to leave them at once use this

Telegram-auto-leave-groups If you are in allot of groups or channel and you would like to leave them at once use this USER GUIDE ?? Insert your telegr

Julius Njoroge 4 Jan 3, 2023
📢 Video Chat Stream Telegram Bot. Can ⏳ Stream Live Videos, Radios, YouTube Videos & Telegram Video Files On Your Video Chat Of Channels & Groups !

Telegram Video Chat Bot (Beta) ?? Video Chat Stream Telegram Bot ?? Can Stream Live Videos, Radios, YouTube Videos & Telegram Video Files On Your Vide

brut✘⁶⁹ // ユスフ 15 Dec 24, 2022
Telegram bot to stream videos in telegram voicechat for both groups and channels. Supports live strams, YouTube videos and telegram media.

Telegram VCVideoPlayBot An Telegram Bot By @ZauteKm To Stream Videos in Telegram Voice Chat. NOTE: Make sure you have started a VoiceChat in your Grou

Zaute 20 Oct 21, 2022
Telegram bot to stream videos in telegram voicechat for both groups and channels. Supports live strams, YouTube videos and telegram media.

Telegram bot to stream videos in telegram voicechat for both groups and channels. Supports live strams, YouTube videos and telegram media.

SUBIN 449 Dec 27, 2022
❤️ Hi There Im EzilaX ❤️ A next gen powerful telegram group manager bot 😱 for manage your groups and have fun with other cool modules Made By Sadew Jayasekara 🔥

❤️ EzilaX v1 ❤️ Unmaintained. The new repo of @EzilaXBot is Public. (It is no longer based on this source code. The completely rewritten bot available

Sadew Jayasekara 18 Nov 24, 2021