blob: 34e45a057f32600824c030097f7144ab23cc100a [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
14import json
15import pprint
16
17from ruamel import yaml
18
19# main function that calls other functions
20if __name__ == "__main__":
21
22 # this is passed to argparse, key is option name, rest is kwargs
23 extra_args = {
24 "domain_extension": {
25 "default": "aetherproject.net",
26 "nargs": "?",
27 "type": ascii,
28 "help": "Domain extension (optional, default: aetherproject.net)",
29 },
30 }
31
32 args = nbhelper.parse_cli_args(extra_args)
33 nbh = nbhelper.NBHelper(args)
34
35 yaml_out = {}
36 pxeboot_hosts = []
37
38 prefixes = nbh.all_prefixes()
39 devices = nbhelper.NBDevice.all_devs()
40
41 for dev_id, device in devices.items():
42
43 # only pxeboot for servers
44 if device.data["device_role"]["slug"] == "server":
45
46 pxe_dev = {}
47 pxe_dev["serial"] = device.data["serial"]
48 pxe_dev["hostname"] = device.data["name"]
49 pxe_dev["domain"] = args.domain_extension
50 pxe_dev["mac_address"] = device.primary_iface()["mac_address"].lower()
51
52 pxeboot_hosts.append(pxe_dev)
53
54 # yaml_out["devices"] = devices
55 yaml_out["pxeboot_hosts"] = pxeboot_hosts
56
57 print(yaml.safe_dump(yaml_out, indent=2))