blob: fe9e1a725ac97af276a508604590b2d5e7ee5743 [file] [log] [blame]
Zack Williams2aeb3ef2021-06-11 17:10:36 -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# 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
11from __future__ import absolute_import
12
13import nbhelper
Zack Williams2aeb3ef2021-06-11 17:10:36 -070014from ruamel import yaml
15
16# main function that calls other functions
17if __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 Williamsc5320ec2021-07-22 00:32:10 -070029 args = nbhelper.initialize(extra_args)
Zack Williamsdac2be42021-08-19 16:14:31 -070030 tenant = nbhelper.Tenant()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070031
32 yaml_out = {}
33 pxeboot_hosts = []
34
Zack Williamsdac2be42021-08-19 16:14:31 -070035 for device in tenant.get_devices():
Zack Williams2aeb3ef2021-06-11 17:10:36 -070036
37 # only pxeboot for servers
Zack Williamsdac2be42021-08-19 16:14:31 -070038 if device.data.device_role.slug in ["server", "router"]:
Zack Williams2aeb3ef2021-06-11 17:10:36 -070039
40 pxe_dev = {}
Zack Williams2aeb3ef2021-06-11 17:10:36 -070041 pxe_dev["hostname"] = device.data["name"]
42 pxe_dev["domain"] = args.domain_extension
Zack Williamsdac2be42021-08-19 16:14:31 -070043
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 Williams2aeb3ef2021-06-11 17:10:36 -070049
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))