Zack Williams | dac2be4 | 2021-08-19 16:14:31 -0700 | [diff] [blame] | 1 | #!/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 | |
| 10 | import nbhelper |
| 11 | from ruamel import yaml |
| 12 | |
| 13 | if __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)) |