您可以使用以下inventory属性访问资源inventory
:
from nornir import InitNornir
nr = InitNornir(config_file="config.yaml")
print(nr.inventory.hosts)
{'host1.cmh': Host: host1.cmh, 'host2.cmh': Host: host2.cmh, 'spine00.cmh': Host: spine00.cmh, 'spine01.cmh': Host: spine01.cmh, 'leaf00.cmh': Host: leaf00.cmh, 'leaf01.cmh': Host: leaf01.cmh, 'host1.bma': Host: host1.bma, 'host2.bma': Host: host2.bma, 'spine00.bma': Host: spine00.bma, 'spine01.bma': Host: spine01.bma, 'leaf00.bma': Host: leaf00.bma, 'leaf01.bma': Host: leaf01.bma}
inventory有两个类似字典的属性,hosts
并且groups
可以使用分别访问的host和组:
nr.inventory.hosts
{'host1.cmh': Host: host1.cmh,
'host2.cmh': Host: host2.cmh,
'spine00.cmh': Host: spine00.cmh,
'spine01.cmh': Host: spine01.cmh,
'leaf00.cmh': Host: leaf00.cmh,
'leaf01.cmh': Host: leaf01.cmh,
'host1.bma': Host: host1.bma,
'host2.bma': Host: host2.bma,
'spine00.bma': Host: spine00.bma,
'spine01.bma': Host: spine01.bma,
'leaf00.bma': Host: leaf00.bma,
'leaf01.bma': Host: leaf01.bma}
nr.inventory.groups
{'global': Group: global,
'eu': Group: eu,
'bma': Group: bma,
'cmh': Group: cmh}
nr.inventory.hosts["leaf01.bma"]
Host: leaf01.bma
host和组也是类似dict的对象:
host = nr.inventory.hosts["leaf01.bma"]
host.keys()
dict_keys(['site', 'role', 'type', 'asn', 'domain'])
host["site"]
'bma'