01 前言
前段時間分享了華為NETCONF結(jié)合協(xié)程gevent,在大規(guī)模數(shù)據(jù)中心自動化批量操作的方法,不過我忽略了兩個問題文虏,一個是協(xié)程gevent的加入會讓排錯難度大幅提升节视,導(dǎo)致很多小伙伴自己應(yīng)用時無限掉坑被逼瘋,第二是gevent在windows環(huán)境下總會出現(xiàn)莫名其妙的問題益楼,而很多使用者對linux又不算熟悉猾漫,最終導(dǎo)致很多人使用gevent并不順利。
秉著簡單易用的原則感凤,我重新研究了下nornir這個專為網(wǎng)絡(luò)而生的模塊悯周,個人感覺3000臺以下網(wǎng)絡(luò)設(shè)備的簡單跑批操作使用nr就可以了。相比于ansible來說這個模塊在效率和靈活性上都有質(zhì)的飛躍陪竿,相比協(xié)程+api的方式又簡單了許多禽翼,相比單獨的netmiko模塊又有更多的優(yōu)化。所以中小規(guī)模企業(yè)推薦直接使用nornir族跛。nornir的基礎(chǔ)知識王印老師(知乎@弈心)已經(jīng)做過介紹闰挡,華為安裝的一些坑朱嘉盛老師也介紹過,這些內(nèi)容直接在知乎搜索即可礁哄,我就不重復(fù)介紹了长酗。
雖然nr模塊確實非常好用,但從實踐的角度我發(fā)現(xiàn)一個問題桐绒,就是從現(xiàn)有的資料來看并不適合大規(guī)模跑批操作夺脾,因為網(wǎng)上分享的內(nèi)容(包括官方案例)都有一個問題,就是nr模塊強依賴hosts.yaml這個文件茉继,但沒人強調(diào)如何大批量導(dǎo)入資產(chǎn)信息到這個yaml文件內(nèi)咧叭,如果你有1000臺設(shè)備,按照yaml格式一個一個輸入到y(tǒng)aml文件里手都要斷了烁竭,所以我手寫了一個自動生成hosts.yaml+nr跑批的py腳本菲茬,這樣在大規(guī)模場景下更加實用。
02 實現(xiàn)邏輯
- 簡化hosts.yaml內(nèi)部配置,只保存設(shè)備名稱和IP地址(yaml內(nèi)的hostname)生均,用戶名听想、密碼、platform全放在defaults.yaml中马胧,不使用group.yaml(想使用也可以汉买,因為我這個內(nèi)容主要是HUAWEI,沒有第二個廠商佩脊,所以不需要)蛙粘。
config.yaml
---
inventory:
plugin: SimpleInventory
options:
host_file: "hosts.yaml"
defaults_file: "defaults.yaml"
runner:
plugin: threaded
options:
num_workers: 20
defaults.yaml
---
username: user
password: pass
platform: huawei_vrpv8
- 讀取excel內(nèi)容(excel內(nèi)容如下:),形成dict威彰,在用dict專為yaml文件出牧,生成hosts.yaml,再使用nornir進行跑批操作歇盼。
03 代碼示例
#!/usr/bin/env python
#coding: utf-8
import os
from pprint import pprint
from ruamel import yaml
#ruamel.yaml用來讀寫yaml文件
from openpyxl import Workbook
from openpyxl import load_workbook
from collections import defaultdict
#使用defaultdict必須導(dǎo)入
from nornir import InitNornir
from nornir_netmiko import netmiko_send_command
from nornir_utils.plugins.functions import print_result,print_title
def read_device_excel():
current_path = os.path.abspath(".")
devices_filename = current_path + "/info02.xlsx"
wb1 = load_workbook(devices_filename)
ws1 = wb1.get_sheet_by_name("device list")
device_dict = defaultdict(dict)
for cow_num in range(2,ws1.max_row+1):
device_dict[ws1["a"+str(cow_num)].value.strip()]["hostname"]=ws1["b"+str(cow_num)].value.strip()
return (dict(device_dict))
def dict_to_yaml(yaml_file):
device_dict = read_device_excel()
with open(yaml_file, 'w', encoding='utf-8') as file:
file.write("---\\n")
yaml.dump(device_dict, file, Dumper=yaml.RoundTripDumper)
def main():
current_path = os.path.abspath(".")
yaml_path = os.path.join(current_path, "hosts.yaml")
dict_to_yaml(yaml_path)
nr = InitNornir(config_file="config.yaml", dry_run=True)
results = nr.run(netmiko_send_command, command_string='disp ver')
print_result(results)
#===========================================================
# 讀取excel內(nèi)容自動生成hosts.yaml文件舔痕,再使用nr進行跑批操作
#===========================================================
if __name__ == '__main__':
main()
執(zhí)行結(jié)果(示例我就執(zhí)行了1臺)
netmiko_send_command************************************************************
* SHM-RO4-POD2-1 ** changed : False ********************************************
vvvv netmiko_send_command ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Huawei Versatile Routing Platform Software
VRP (R) software, Version 8.150 (CE6851HI V200R002C50SPC018T)
Copyright (C) 2012-2017 Huawei Technologies Co., Ltd.
HUAWEI CE6851-48S6Q-HI uptime is 1476 days, 2 hours, 26 minutes
CE6851-48S6Q-HI(Master) 1 : uptime is 1476 days, 2 hours, 25 minutes
StartupTime 2017/06/09 14:44:20
Memory Size : 2048 M bytes
Flash Size : 1024 M bytes
CE6851-48S6Q-HI version information
1\. PCB Version : CEM48S6QP04 VER B
2\. MAB Version : 1
3\. Board Type : CE6851-48S6Q-HI
4\. CPLD1 Version : 103
5\. CPLD2 Version : 103
6\. BIOS Version : 383
^^^^ END netmiko_send_command ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
04 代碼詳解
def read_device_excel():
current_path = os.path.abspath(".")
devices_filename = current_path + "/info02.xlsx"
wb1 = load_workbook(devices_filename)
ws1 = wb1.get_sheet_by_name("device list")
device_dict = defaultdict(dict)
for cow_num in range(2,ws1.max_row+1):
device_dict[ws1["a"+str(cow_num)].value.strip()]["hostname"]=ws1["b"+str(cow_num)].value.strip()
return (dict(device_dict))
看過我分享的都比較熟悉上面的代碼了,就不做介紹了豹缀,主要說下defaultdict(dict)伯复,這個可以用作嵌套dict,比如要根據(jù)已知內(nèi)容生成{'SHM-RO4-POD2-1': {'hostname': '10.13.0.224'}}邢笙,那我們就用for循環(huán)遍歷excel內(nèi)容啸如,然后以A列為key,hostname:B列內(nèi)容作為value氮惯,生成嵌套dict叮雳。
def dict_to_yaml(yaml_file):
device_dict = read_device_excel()
with open(yaml_file, 'w', encoding='utf-8') as file:
file.write("---\\n")
yaml.dump(device_dict, file, Dumper=yaml.RoundTripDumper)
使用yaml.dump()方法將dict內(nèi)容以標準yaml格式寫入hosts.yaml文件,注意需要加參數(shù)Dumper=yaml.RoundTripDumper妇汗。
生成的hosts.yaml
---
SHM-RO4-POD2-1:
hostname: 10.13.0.224
SHM-RO4-POD2-2:
hostname: 10.13.0.225
SHM-RO4-POD2-3:
hostname: 10.13.0.226
SHM-RO4-POD2-4:
hostname: 10.13.0.227
def main():
current_path = os.path.abspath(".")
yaml_path = os.path.join(current_path, "hosts.yaml")
dict_to_yaml(yaml_path)
nr = InitNornir(config_file="config.yaml", dry_run=True)
results = nr.run(netmiko_send_command, command_string='disp ver')
# results = nr.run(task=napalm_get, getters=["facts"]) #napalm API不好用
print_result(results)
主函數(shù)很簡單帘不,調(diào)用生成yaml文件的方法,然后使用Nornir進行跑批操作铛纬。