Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -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 | # pxeconfig.py |
| 7 | # Given a yaml config file (same as ansible inventory for a site), create a |
| 8 | # YAML file consumable by ansible that has input for the pxeboot role, for creating |
| 9 | # preseed files for servers |
| 10 | |
| 11 | from __future__ import absolute_import |
| 12 | |
| 13 | import nbhelper |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 14 | from ruamel import yaml |
| 15 | |
| 16 | # main function that calls other functions |
| 17 | if __name__ == "__main__": |
| 18 | |
| 19 | # this is passed to argparse, key is option name, rest is kwargs |
| 20 | extra_args = { |
| 21 | "domain_extension": { |
| 22 | "default": "aetherproject.net", |
| 23 | "nargs": "?", |
| 24 | "type": ascii, |
| 25 | "help": "Domain extension (optional, default: aetherproject.net)", |
| 26 | }, |
| 27 | } |
| 28 | |
Zack Williams | c5320ec | 2021-07-22 00:32:10 -0700 | [diff] [blame] | 29 | args = nbhelper.initialize(extra_args) |
Zack Williams | dac2be4 | 2021-08-19 16:14:31 -0700 | [diff] [blame] | 30 | tenant = nbhelper.Tenant() |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 31 | |
| 32 | yaml_out = {} |
| 33 | pxeboot_hosts = [] |
| 34 | |
Zack Williams | dac2be4 | 2021-08-19 16:14:31 -0700 | [diff] [blame] | 35 | for device in tenant.get_devices(): |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 36 | |
| 37 | # only pxeboot for servers |
Zack Williams | dac2be4 | 2021-08-19 16:14:31 -0700 | [diff] [blame] | 38 | if device.data.device_role.slug in ["server", "router"]: |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 39 | |
| 40 | pxe_dev = {} |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 41 | pxe_dev["hostname"] = device.data["name"] |
| 42 | pxe_dev["domain"] = args.domain_extension |
Zack Williams | dac2be4 | 2021-08-19 16:14:31 -0700 | [diff] [blame] | 43 | |
| 44 | if device.data["serial"]: |
| 45 | pxe_dev["serial"] = device.data["serial"] |
| 46 | |
| 47 | if device.primary_iface["mac_address"]: |
| 48 | pxe_dev["mac_address"] = device.primary_iface["mac_address"].lower() |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 49 | |
| 50 | pxeboot_hosts.append(pxe_dev) |
| 51 | |
| 52 | # yaml_out["devices"] = devices |
| 53 | yaml_out["pxeboot_hosts"] = pxeboot_hosts |
| 54 | |
| 55 | print(yaml.safe_dump(yaml_out, indent=2)) |