LoginSignup
28
4

More than 3 years have passed since last update.

Nornir 3.0の注意点とCisco IOS XE ホスト名の取得

Last updated at Posted at 2020-11-30

はじめに

この記事はシスコの同志による Advent Calendar の一部として投稿しています
- 2017年版: https://qiita.com/advent-calendar/2017/cisco
- 2018年版: https://qiita.com/advent-calendar/2018/cisco
- 2019年版: https://qiita.com/advent-calendar/2019/cisco
- 2020年版: https://qiita.com/advent-calendar/2020/cisco (今ここ)

Nornir

NornirとはPython製自動化フレームワークのことでAnsibleなどに近いものになっています。
今回のブログでは3.0を動かす注意点とNETCONFでhost名を取得するコードについて書きます

接続方法
- Netmiko
- NAPALM
- NETCONF

Nornir3.0から2.0の時より大きく変わって過去のプログラミングのソースコードが使えなくなる部分が多く
注意点が多くなっています。
特に個別でpluginをインストールする必要が出たりします。pluginを理解しないとサンプルコードを書くことも出来ないです。

Nornirでよく出るエラーの例を出します。

ModuleNotFoundError: No module named 'nornir.plugins.tasks.networking'

2020.09.01バージョンだとエラーは出ないのですが最新の3.0ではエラーが出ます。何も考えないで過去のコードや公式Docのチュートリアルはモジュールないよのエラーで埋め尽くされることでしょう

自分が入れてエラーが出なくなったモジュールをいくつか紹介します。

~ (nornir) KATSHIMA:gg katshima$ pip install nornir
~ (nornir) KATSHIMA:gg katshima$ pip install nornir_napalm
~ (nornir) KATSHIMA:gg katshima$ pip install nornir_netmiko
~ (nornir) KATSHIMA:gg katshima$ pip install nornir_netbox
~ (nornir) KATSHIMA:gg katshima$ pip install nornir_ansible
~ (nornir) KATSHIMA:gg katshima$ pip install nornir_scrapli
~ (nornir) KATSHIMA:gg katshima$ pip install nornir_utils
~ (nornir) KATSHIMA:gg katshima$ pip install nornir_jinja2

上記のModuleNotFoundErrorはnornir_scrapliで解消されたのでnornir_scrapliは必須なのかなと思ってます
以上のモジュールは入れておかないとまともに動かない感じあります。
残りのプラグイン等は以下のページにdescriptionと共に書かれているので参考にすると良いと思います。
https://nornir.tech/nornir/plugins/

ホスト名を取得する

環境

環境
Cisco IOS-XE (16.11)
Cisco DevNet SandBox
Python 3.7.4
nornir 3.0.0a

このブログのすべての例では、Cisco Devnetが提供するCisco SandBox環境を使用します。​Devnetを見に行きましょう。主にIOS XEのSandBoxを使用しています。自動化を試験するときにわざわざ機材を準備しなくていいので本当に楽です。


config.yml
inventory/hosts.yml 
inventory/groups.yml
inventory/defaults.yml

config.yaml


---
runners:
  plugin: threaded
  options:
    num_workers: 10

inventory:
  plugin: "SimpleInventory"
  options: 
    host_file: "./inventory/hosts.yml"
    group_file: "./inventory/groups.yml"
    defaults_file: "./inventory/defaults.yml"

hosts.yaml


---
DEV01: 
  hostname: ios-xe-mgmt.cisco.com
  groups: 
    - Devnet

DEV02: 
  hostname: ios-xe-mgmt-latest.cisco.com
  groups: 
    - Devnet

default.yml

---
platform: ios
port: 8181
username: "developer"
password: "C1sco12345"

groups.yml


---
Devnet: 
  data: 
    ntp: 
      servers: 
        - 1.1.1.1

hostget.py

from nornir import InitNornir
from nornir_utils.plugins.functions import print_result

nornir = InitNornir('nornir_config.yml')
print(nornir.inventory.hosts)

r1 = nornir.inventory.hosts['DEV01']

print(f"Group: {r1.groups}")
print(f"Hostname: {r1.hostname}")
print(f"Username: {r1.username}")
print(f"Password: {r1.password}")

実行結果


(nornir) KATSHIMA-M-J6EA:non katshima$ python aa.py
{'DEV01': Host: DEV01, 'DEV02': Host: DEV02}
Group: [Group: Devnet]
Hostname: ios-xe-mgmt.cisco.com
Username: None
Password: None

 感想

2日間粘ってようやく動きました。
まだまだドキュメントが充実していないのでまだまだこれからの自動化フレームワークな気がしますが
いろんな接続方法を簡単に指定できるのはとても魅力的だと思います。

次回はいろんな機器の状態を持ってくるコードを紹介しようと思います。

コードはGithubにもあげます。よかったらフォローしてください
https://github.com/Katsuya414

参考記事

https://tekunabe.hatenablog.jp/entry/2020/06/14/nornir_netconf_get
https://nornir.readthedocs.io/en/latest/plugins/index.html
https://blog.wimwauters.com/networkprogrammability/2020-09-29_nornir3.0_introduction/
https://github.com/wiwa1978/blog-hugo-netlify-code/tree/master/Nornir_Introduction

28
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
4