| #!/usr/bin/env python3 |
| |
| # SPDX-FileCopyrightText: © 2021 Open Networking Foundation <support@opennetworking.org> |
| # SPDX-License-Identifier: Apache-2.0 |
| |
| # inventory.py |
| # create an inventory file for a site, in YAML format |
| # currently aether specific |
| |
| import nbhelper |
| from ruamel import yaml |
| |
| if __name__ == "__main__": |
| |
| extra_args = { |
| "--generic": { |
| "action": "store_true", |
| "help": "Use generic output, instead of Aether-specific", |
| }, |
| } |
| |
| args = nbhelper.initialize(extra_args) |
| tenant = nbhelper.Tenant() |
| |
| routers = {} |
| servers = {} |
| switches = {} |
| |
| for device in tenant.get_devices(): |
| |
| dev_name = device.data["name"] |
| |
| dev_vars = { |
| "ansible_host": str(device.primary_ip), |
| } |
| |
| if device.data.device_role.slug == "router": |
| routers[dev_name] = dev_vars |
| |
| elif device.data.device_role.slug == "server": |
| servers[dev_name] = dev_vars |
| |
| elif device.data.device_role.slug == "switch": |
| switches[dev_name] = dev_vars |
| |
| if args.generic: |
| groups = { |
| "routers": {"hosts": routers}, |
| "servers": {"hosts": servers}, |
| "switches": {"hosts": switches}, |
| } |
| else: |
| groups = { |
| "aethermgmt": {"hosts": routers}, |
| "aethercompute": {"hosts": servers}, |
| "aetherfabric": {"hosts": switches}, |
| } |
| |
| yaml_out = {"all": {"children": groups}} |
| |
| print(yaml.safe_dump(yaml_out, indent=2)) |