初始化nornir的最简单方法是使用函数InitNornir。
随着InitNornir
您可以用配置文件初始化nornir,用代码或两者的组合。
让我们从一个配置文件开始:
%highlight_file config.yaml
1 ---
2 inventory:
3 plugin: SimpleInventory
4 options:
5 host_file: "inventory/hosts.yaml"
6 group_file: "inventory/groups.yaml"
7 defaults_file: "inventory/defaults.yaml"
8 runner:
9 plugin: threaded
10 options:
11 num_workers: 100
现在创建nornir对象:
from nornir import InitNornir
nr = InitNornir(config_file="config.yaml")
您也可以在不使用配置文件的情况下以编程方式初始化nornir:
from nornir import InitNornir
nr = InitNornir(
runner={
"plugin": "threaded",
"options": {
"num_workers": 100,
},
},
inventory={
"plugin": "SimpleInventory",
"options": {
"host_file": "inventory/hosts.yaml",
"group_file": "inventory/groups.yaml"
},
},
)
或同时使用两种方法:
from nornir import InitNornir nr = InitNornir( config_file="config.yaml", runner={ "plugin": "threaded", "options": { "num_workers": 50, }, }, ) nr.config.runner.options["num_workers"] 执行输出:50