blob: 26edcdda03b5b015145b93ac580582c463626429 [file] [log] [blame]
Zack Williamsdac2be42021-08-19 16:14:31 -07001#!/usr/bin/env python3
2
3# SPDX-FileCopyrightText: © 2021 Open Networking Foundation <support@opennetworking.org>
4# SPDX-License-Identifier: Apache-2.0
5
6# inventory.py
7# create an inventory file for a site, in YAML format
8# currently aether specific
9
10import nbhelper
11from ruamel import yaml
12
13if __name__ == "__main__":
14
15 extra_args = {
16 "--generic": {
17 "action": "store_true",
18 "help": "Use generic output, instead of Aether-specific",
19 },
20 }
21
22 args = nbhelper.initialize(extra_args)
23 tenant = nbhelper.Tenant()
24
25 routers = {}
26 servers = {}
27 switches = {}
28
29 for device in tenant.get_devices():
30
31 dev_name = device.data["name"]
32
33 dev_vars = {
34 "ansible_host": str(device.primary_ip),
35 }
36
37 if device.data.device_role.slug == "router":
38 routers[dev_name] = dev_vars
39
40 elif device.data.device_role.slug == "server":
41 servers[dev_name] = dev_vars
42
43 elif device.data.device_role.slug == "switch":
44 switches[dev_name] = dev_vars
45
46 if args.generic:
47 groups = {
48 "routers": {"hosts": routers},
49 "servers": {"hosts": servers},
50 "switches": {"hosts": switches},
51 }
52 else:
53 groups = {
54 "aethermgmt": {"hosts": routers},
55 "aethercompute": {"hosts": servers},
56 "aetherfabric": {"hosts": switches},
57 }
58
59 yaml_out = {"all": {"children": groups}}
60
61 print(yaml.safe_dump(yaml_out, indent=2))