Skip to content

Commit

Permalink
Add script to append required DHCP options to the Netplan config
Browse files Browse the repository at this point in the history
The Netplan config is created by cloud-init, but it needs to be
slightly modified and reapplied.  This script takes care of the
modification.

See these issues for more details:
- cisagov/skeleton-packer#300
- canonical/cloud-init#4764
  • Loading branch information
jsf9k committed Jan 10, 2024
1 parent 4ba4359 commit 4a5a9c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ repos:
rev: v1.5.1
hooks:
- id: mypy
additional_dependencies:
- types-pyyaml
- repo: https://github.com/asottile/pyupgrade
rev: v3.10.1
hooks:
Expand Down
34 changes: 34 additions & 0 deletions cloud-init/fix-dhcp.tpl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3

"""Append the necessary DHCP options to the Netplan configuration.
The Netplan configuration is created by cloud-init, but it needs to be
slightly modified and reapplied. This script takes care of the
modification.
See these issues for more details:
- cisagov/skeleton-packer#300
- canonical/cloud-init#4764
This file is a template. It should be processed by Terraform.
"""

# Third-Party Libraries
import yaml

# Inputs from Terraform
NETPLAN_CONFIG = "${netplan_config}"

with open(NETPLAN_CONFIG) as f:
# Load the current Netplan configuration
config = yaml.safe_load(f)
# Add a dhcp4-overrides section to each network
config["network"]["ethernets"] = {
k: v | {"dhcp4-overrides": {"use-domains": True}}
for (k, v) in config["network"]["ethernets"].items()
}

# Write the results back out to the Netplan configuration file
with open(NETPLAN_CONFIG, "w") as f:
f.write(yaml.dump(config))

0 comments on commit 4a5a9c0

Please sign in to comment.