blob: 97aedab3f47efcbc9befbf50d4e7f6ea1061208a [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 -070014
15from ruamel import yaml
16
17# main function that calls other functions
18if __name__ == "__main__":
19
20 # this is passed to argparse, key is option name, rest is kwargs
21 extra_args = {
22 "domain_extension": {
23 "default": "aetherproject.net",
24 "nargs": "?",
25 "type": ascii,
26 "help": "Domain extension (optional, default: aetherproject.net)",
27 },
28 }
29
Zack Williamsc5320ec2021-07-22 00:32:10 -070030 args = nbhelper.initialize(extra_args)
31 tenant = nbhelper.NBTenant()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070032
33 yaml_out = {}
34 pxeboot_hosts = []
35
Zack Williamsc5320ec2021-07-22 00:32:10 -070036 prefixes = nbhelper.NBPrefix.all_prefixes()
37 devices = nbhelper.NBDevice.all_objects()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070038
39 for dev_id, device in devices.items():
40
41 # only pxeboot for servers
42 if device.data["device_role"]["slug"] == "server":
43
44 pxe_dev = {}
45 pxe_dev["serial"] = device.data["serial"]
46 pxe_dev["hostname"] = device.data["name"]
47 pxe_dev["domain"] = args.domain_extension
48 pxe_dev["mac_address"] = device.primary_iface()["mac_address"].lower()
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))