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
Unofficial python api for MicroBT Whatsminer ASICs

whatsminer-api Unofficial python api for MicroBT Whatsminer ASICs Code adapted from a python file found in the Whatsminer Telegram group that is credi

Satoshi Anonymoto 16 Dec 23, 2022
Python client library for Postmark API

Postmarker Python client library for Postmark API. Gitter: https://gitter.im/Stranger6667/postmarker Installation Postmarker can be obtained with pip:

Dmitry Dygalo 109 Dec 13, 2022
Random Geek Jokes REST API

Geek-Jokes A RESTful API to get random geek jokes written in Flask What is the Geek-Jokes-api? The Geek Jokes RESTful API lets you fetch a random geek

Sameer Kumar 84 Dec 15, 2022
Presentation and code files for the talk at PyCon Indonesia

pycon-indonesia Presentation and code files for the talk at PyCon Indonesia. Files used for the PyCon Indonesia presentation. [Directory Includes:] Be

Neeraj Pandey 2 Dec 04, 2021
RDMAss - A Python Discord bot creating an interaction with RDM API

RDMAss A Python Discord bot creating an interaction with RDM API. Features Assig

5 Sep 21, 2022
PokemonGo-Bot - The Pokemon Go Bot, baking with community.

PokemonGo-Bot PokemonGo-Bot is a project created by the PokemonGoF team. Since no public API available for now, a patch to use HASH-Server was applied

3.8k Jan 08, 2023
A superb Telegram VoiceChat Player. Powered by FalconRoBo.

𝕱𝖆𝖑𝖈𝖔𝖓𝕸𝖚𝖘𝖎𝖈 A sᴜᴘᴇʀʙ Tᴇʟᴇɢʀᴀᴍ VᴏɪᴄᴇCʜᴀᴛ Pʟᴀʏᴇʀ, ᴍᴀᴅᴇ ᴜsɪɴɢ Lᴀᴛᴇsᴛ Pʏᴛʜᴏɴ ᴀɴᴅ Pʏʀᴏɢʀᴀᴍ. 𝑷𝒐𝒘𝒆𝒓𝒆𝒅 𝒃𝒚 𝑭𝒂𝒍𝒄𝒐𝒏𝑹𝒐𝑩𝒐 FalconMusic

FalconRoBo 2 Oct 21, 2022
A Flask inspired, decorator based API wrapper for Python-Slack.

A Flask inspired, decorator based API wrapper for Python-Slack. About Tangerine is a lightweight Slackbot framework that abstracts away all the boiler

Nick Ficano 149 Jun 30, 2022
Represents a Lavalink client used to manage nodes and connections.

lavaplayer Represents a Lavalink client used to manage nodes and connections. setup pip install lavaplayer setup lavalink you need to java 11* LTS or

HazemMeqdad 37 Nov 21, 2022
Guilherme Matheus 11 Sep 11, 2022
McTrade is a bot exploiting Binance API, open source! built in python !

Open Source Crypto Trading Bot using Binance API Report Bug · Request Feature Table of Contents About The Project Built With Getting Started Prerequis

Raphael Cohen 5 Jul 17, 2022
Library written in Python that wraps Halo Infinite API.

haloinfinite Library written in Python that wraps Halo Infinite API. Before start It's unofficial, reverse-engineered, neither stable nor production r

Miguel Ferrer 4 Dec 28, 2022
Ice-Userbot adalah userbot Telegram modular yang berjalan di Python3 dengan database sqlalchemy

Ice-Userbot Telegram Ice-Userbot adalah userbot Telegram modular yang berjalan di Python3 dengan database sqlalchemy. Berbasis Paperplane dan ProjectB

6 Apr 29, 2022
AWS Workmail Migration Tool

WMigrate A tool for migrating AWS Workmail Users and Groups cross region and cross accounts. It also creates user and group aliases and adds the users

NK 1 Oct 27, 2021
Cleiton Leonel 4 Apr 22, 2022
a discord libary that use to make discord bot with low efficiency and bad performance because I don't know how to manage the project

Aircord 🛩️ a discord libary that use to make discord bot with low efficiency and bad performance because I don't know how to manage the project Examp

Aircord 2 Oct 24, 2021
The best discord.py template with a changeable prefix

Discord.py Bot Template By noma4321#0035 With A Custom Prefix To Every Guild Function Features Has a custom prefix that is changeable for every guild

Noma4321 5 Nov 24, 2022
A Collection Manager for the objkt.com Minting Factory

Objkt Collection Manager A Collection Manager for the objkt.com Minting Factory. This contract can create a collection on objkt.com and mint into it.

Asbjorn Enge 5 Nov 22, 2022
可基于【腾讯云函数】/【GitHub Actions】/【Docker】的每日签到脚本(支持多账号使用)签到列表: |爱奇艺|全民K歌|腾讯视频|有道云笔记|网易云音乐|一加手机社区官方论坛|百度贴吧|Bilibili|V2EX|咔叽网单|什么值得买|AcFun|天翼云盘|WPS|吾爱破解|芒果TV|联通营业厅|Fa米家|小米运动|百度搜索资源平台|每日天气预报|每日一句|哔咔漫画|和彩云|智友邦|微博|CSDN|王者营地|

每日签到集合 基于【腾讯云函数】/【GitHub Actions】/【Docker】的每日签到脚本 支持多账号使用 特别声明: 本仓库发布的脚本及其中涉及的任何解锁和解密分析脚本,仅用于测试和学习研究,禁止用于商业用途,不能保证其合法性,准确性,完整性和有效性,请根据情况自行判断。

87 Nov 12, 2022
A Telegram Bot for searching any channel messages from Inline by @AbirHasan2005

Message-Search-Bot A Telegram Bot for searching any channel messages from Inline by @AbirHasan2005. I made this for @AHListBot. You can use this for s

Abir Hasan 44 Dec 27, 2022