Nornir inventory介绍

单可以说是最重要的规范。让我们看看它是如何工作的。首先,inventory由host,组和默认值组成。

在本教程中,我们使用SimpleInventory插件。此inventory插件将所有相关数据存储在三个文件中。让我们从检查它们开始:

  
  1 ---
  2 host1.cmh:
  3     hostname: 127.0.0.1
  4     port: 2201
  5     username: vagrant
  6     password: vagrant
  7     platform: linux
  8     groups:
  9         - cmh
 10     data:
 11         site: cmh
 12         role: host
 13         type: host
 14         nested_data:
 15             a_dict:
 16                 a: 1
 17                 b: 2
 18             a_list: [1, 2]
 19             a_string: "asdasd"
 20 
 21 host2.cmh:
 22     hostname: 127.0.0.1
 23     port: 2202
 24     username: vagrant
 25     password: vagrant
 26     platform: linux
 27     groups:
 28         - cmh
 29     data:
 30         site: cmh
 31         role: host
 32         type: host
 33         nested_data:
 34             a_dict:
 35                 b: 2
 36                 c: 3
 37             a_list: [1, 2]
 38             a_string: "qwe"
 39 
 40 spine00.cmh:
 41     hostname: 127.0.0.1
 42     username: vagrant
 43     password: vagrant
 44     port: 12444
 45     platform: eos
 46     groups:
 47         - cmh
 48     data:
 49         site: cmh
 50         role: spine
 51         type: network_device
 52 
 53 spine01.cmh:
 54     hostname: 127.0.0.1
 55     username: vagrant
 56     password: ""
 57     platform: junos
 58     port: 12204
 59     groups:
 60         - cmh
 61     data:
 62         site: cmh
 63         role: spine
 64         type: network_device
 65 
 66 leaf00.cmh:
 67     hostname: 127.0.0.1
 68     username: vagrant
 69     password: vagrant
 70     port: 12443
 71     platform: eos
 72     groups:
 73         - cmh
 74     data:
 75         site: cmh
 76         role: leaf
 77         type: network_device
 78         asn: 65100
 79 
 80 leaf01.cmh:
 81     hostname: 127.0.0.1
 82     username: vagrant
 83     password: ""
 84     port: 12203
 85     platform: junos
 86     groups:
 87         - cmh
 88     data:
 89         site: cmh
 90         role: leaf
 91         type: network_device
 92         asn: 65101
 93 
 94 host1.bma:
 95     groups:
 96         - bma
 97     platform: linux
 98     data:
 99         site: bma
100         role: host
101         type: host
102 
103 host2.bma:
104     groups:
105         - bma
106     platform: linux
107     data:
108         site: bma
109         role: host
110         type: host
111 
112 spine00.bma:
113     hostname: 127.0.0.1
114     username: vagrant
115     password: vagrant
116     port: 12444
117     platform: eos
118     groups:
119         - bma
120     data:
121         site: bma
122         role: spine
123         type: network_device
124 
125 spine01.bma:
126     hostname: 127.0.0.1
127     username: vagrant
128     password: ""
129     port: 12204
130     platform: junos
131     groups:
132         - bma
133     data:
134         site: bma
135         role: spine
136         type: network_device
137 
138 leaf00.bma:
139     hostname: 127.0.0.1
140     username: vagrant
141     password: vagrant
142     port: 12443
143     platform: eos
144     groups:
145         - bma
146     data:
147         site: bma
148         role: leaf
149         type: network_device
150 
151 leaf01.bma:
152     hostname: 127.0.0.1
153     username: vagrant
154     password: wrong_password
155     port: 12203
156     platform: junos
157     groups:
158         - bma
159     data:
160         site: bma
161         role: leaf
162         type: network_device

host文件基本上是一个映射,其中最外键是host的名称,然后是Host对象。您可以通过执行以下命令查看对象的架构:

from nornir.core.inventory import Host
import json
print(json.dumps(Host.schema(), indent=4))


 1 ---
 2 global:
 3     data:
 4         domain: global.local
 5         asn: 1
 6 
 7 eu:
 8     data:
 9         asn: 65100
10 
11 bma:
12     groups:
13         - eu
14         - global
15 
16 cmh:
17     data:
18         asn: 65000
19         vlans:
20           100: frontend
21           200: backend

groups_file遵循相同的hosts_file规则。

# groups file
%highlight_file inventory/groups.yaml

 1 ---
 2 global:
 3     data:
 4         domain: global.local
 5         asn: 1
 6 
 7 eu:
 8     data:
 9         asn: 65100
10 
11 bma:
12     groups:
13         - eu
14         - global
15 
16 cmh:
17     data:
18         asn: 65000
19         vlans:
20           100: frontend
21           200: backend

最后,默认文件具有与我们之前描述的Host相同的架构,但是没有用于表示各个元素的外键。在本教程后面的部分,我们将看到如何使用组和默认文件中的数据。

# defaults file
%highlight_file inventory/defaults.yaml

1 ---
2 data:
3     domain: acme.local

觉得文章有用?

点个广告表达一下你的爱意吧 !😁