From e5188807dd1dc2aa3de8ab8b462aa08e2e7edd9d Mon Sep 17 00:00:00 2001 From: Seth Grover Date: Wed, 1 Nov 2023 15:28:37 -0600 Subject: [PATCH] make sure preloaded prefixes get populated with default site name (idaholab/Malcolm#279) --- netbox/preload/prefixes_defaults.yml | 3 +++ netbox/scripts/netbox_init.py | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/netbox/preload/prefixes_defaults.yml b/netbox/preload/prefixes_defaults.yml index 0fdb935f1..1788bb584 100644 --- a/netbox/preload/prefixes_defaults.yml +++ b/netbox/preload/prefixes_defaults.yml @@ -1,3 +1,6 @@ - prefix: 10.0.0.0/8 + site: NETBOX_DEFAULT_SITE - prefix: 172.16.0.0/12 + site: NETBOX_DEFAULT_SITE - prefix: 192.168.0.0/16 + site: NETBOX_DEFAULT_SITE diff --git a/netbox/scripts/netbox_init.py b/netbox/scripts/netbox_init.py index ab1aba957..65d6746b4 100755 --- a/netbox/scripts/netbox_init.py +++ b/netbox/scripts/netbox_init.py @@ -617,12 +617,16 @@ def main(): # only preload catch-all IP Prefixes if explicitly specified and they don't already exist if args.preloadPrefixes: + defaultSiteName = next(iter([x for x in args.netboxSites]), None) for loadType in ('vrfs', 'prefixes'): defaultFileName = os.path.join(tmpPreloadDir, f'{loadType}_defaults.yml') loadFileName = os.path.join(tmpPreloadDir, f'{loadType}.yml') if os.path.isfile(defaultFileName) and (not os.path.isfile(loadFileName)): try: - shutil.copyfile(defaultFileName, loadFileName) + with open(defaultFileName, 'r') as infile: + with open(loadFileName, 'w') as outfile: + for line in infile: + outfile.write(line.replace("NETBOX_DEFAULT_SITE", defaultSiteName)) except Exception: pass