Introducing Nornir - The Python automation framework

  • by Patrick Ogenstad
  • May 05, 2018

Nornir is a new automation framework written in Python and intended to be consumed directly from Python. You could describe it as the automation framework for Pythonistas. This might strike you as something wonderful, or it could trigger your spider-sense. Writing code? Isn’t that just for programmers?

Note, regarding names

Initially Nornir was called Brigade, but we changed the name due to a naming conflict with another tool.

Stop! Why do we need yet another automation tool?

There are a lot of tools around these days. You have Ansible, Salt, Chef, Puppet. Then there are things like StackStorm, Automatron and so on. When does it stop? How can you choose when this space is getting so crowded?

Nornir is a bit different compared to other tools, in the way that you write your own Python code to control the automation. For example Ansible is written in Python, but uses its own DSL which you use to describe what you want to have done.

What’s in it for me?

In Nornir we don’t use any DSL instead you write everything in Python. There are a few benefits that motivate this choice.

  • A DSL can get complex when you need “advanced” features
  • Code is easier to troubleshoot and debug, you can leverage existing Python logging and debugging tools
  • Sometimes you end up writing code anyway
  • You can leverage your existing IDE for code completion, linting and such
  • Easy integration into your existing Python code

I am a non-coder, is Nornir really for me?

For a lot of people, the idea of having to write your own code can be a deal-breaker. I think a large part of this is psychological and people tend to fool themselves. Regardless of which tool you choose, there will be a learning curve. A DSL that implements enough control structures and variable manipulation to be Turing complete is a programming language even if it’s not labelled as such. You don’t think about the fact that you are learning programming concepts, you just do.

A side benefit of using Nornir is that it will also teach you Python which I think will be a lot more useful to you, compared to mastering a DSL. To see some examples of how you can create tools using Nornir, we’ve setup the nornir-tools repo.

What can you do with Nornir?

To put it simple Nornir works with generic collections of data and execute tasks based on that data. Generally, this means that you have a number of hosts and groups along with some data associated with each element, this is the inventory. Then you have a number of tasks that you wish to run against the hosts or a subset of the hosts in your inventory. The tasks that you want to execute are regular Python functions.

A task can be something very simple or as complex as you want to, one of the included task plugins used to read Json files looks like this:

import json
from nornir.core.task import Result

def load_json(task, file):
    with open(file, "r") as f:
        data = json.loads(f.read())

    return Result(host=task.host, result=data)

Basically, it’s a function which takes a task object as its first argument followed by the arguments needed for the task, a Result object is then returned to the Nornir task handler. Nornir 1.0.0 was just released and it ships with a small number of task plugins. While this will naturally grow you will also find yourself writing your own plugins, as you can see from above it can be quite simple.

Getting started with Nornir

Nornir is available through PYPI and you install it as just another Python package.

pip install nornir

Before running Nornir you need an inventory containing your hosts and groups. The input to the Nornir inventory is one or two Python dictionaries, one for hosts and one optional for groups. The first release of Nornir includes three inventory plugins to make this easier for you.

  • SimpleInventory - Where you create your inventory in the standard Nornir format
  • AnsibleInventory - Which reads an Ansible ini or yaml inventory file
  • NSOTInventory - Which connects to the NSOT api to build the inventory

If you’re just starting out the SimpleInventory is a good choice. If you don’t specify anything the SimpleInventory is the default one, it will look for a file called hosts.yaml in the current directory and an optional groups.yaml. Here’s an example of a basic inventory of two Cisco devices I have at home.

hosts.yaml

---
og-sw-01:
  groups: ['home_network']

og-ap-01:
  groups: ['home_network']

groups.yaml

---
home_network:
  nornir_username: patrick
  nornir_password: ReallyS3cret!
  nornir_nos: ios

Then to run Nornir I create a short runner in Python.

run-nornir.py

from nornir.core import InitNornir
from nornir.plugins.tasks.networking import napalm_get
from nornir.plugins.functions.text import print_result

nr = InitNornir()

result = nr.run(
             napalm_get,
             getters=['get_facts'])

print_result(result)

To take it from the top we start by importing InitNornir which is a simple way to initialize Nornir, in this example we just accept all of the default settings. The next import is for napalm_get which uses the Napalm library to collect information from network devices. The last import for print_result is a function that helps us print the output to screen.

The first line of actual code “nr = InitNornir()” initializes Nornir and stores it in the nr variable. Next we run the Napalm getter against the hosts in our inventory and store the result in the result variable. Finally, the output is printed to screen.

Runner output

Who are the people behind Nornir?

Nornir was initially created by David Barroso, who also brought us Napalm. Currently, the other members of the project are Kirk Byers as well as myself, Patrick Ogenstad. While we’re all part of the networking world Nornir isn’t by any means limited to that domain. The current setup is more a result of the fact that we already knew each other.

More information

For more information about this project visit the Nornir documentation or look at the Nornir source code. You can also hear us talking about Nornir on the Software Gone Wild Episode #90 podcast, please note that during the recording the project was still called Brigade. Just mentally replace Brigade with Nornir as you listen and you should be fine. :)