From 23cc8008b08f328b5a1facafce81cba41311105c Mon Sep 17 00:00:00 2001 From: kubealex Date: Sun, 4 Feb 2024 16:27:30 +0100 Subject: [PATCH] add dhcp range and fix dns variables naming --- examples/terraform-libvirt-network/main.tf | 28 ++++++++++--------- modules/terraform-libvirt-network/README.md | 16 +++++++---- modules/terraform-libvirt-network/main.tf | 14 ++++++++-- .../templates/dhcp-range-patch.xslt | 16 +++++++++++ .../terraform-libvirt-network/variables.tf | 26 ++++++++++++++--- 5 files changed, 75 insertions(+), 25 deletions(-) create mode 100644 modules/terraform-libvirt-network/templates/dhcp-range-patch.xslt diff --git a/examples/terraform-libvirt-network/main.tf b/examples/terraform-libvirt-network/main.tf index 5f1f98d..4fc6f0a 100644 --- a/examples/terraform-libvirt-network/main.tf +++ b/examples/terraform-libvirt-network/main.tf @@ -1,22 +1,24 @@ module "libvirt_network" { source = "kubealex/libvirt-resources/libvirt//modules/terraform-libvirt-pool" - version = "0.0.1" - network_autostart = true + network_autostart = true network_name = "example_network" - network_mode = "nat" - network_domain = "example.com" - network_cidr = ["192.168.122.0/24"] - network_bridge = "br0" - network_mtu = 1500 - network_dhcp_enabled = true - network_dhcp_local = false - network_dnsmasq_options = { + network_mode = "nat" + network_domain = "example.com" + network_cidr = ["192.168.122.0/24"] + network_bridge = "br0" + network_mtu = 1500 + network_dns_enabled = true + network_dns_local = false + network_dhcp_enabled = true + network_dhcp_range_start = "192.168.122.15" + network_dhcp_range_end = "192.168.122.50" + network_dnsmasq_options = { "server" = "/example.com/192.168.122.1" } - network_dns_entries = { + network_dns_entries = { "example" = "192.168.122.2" } - network_routes = { + network_routes = { "10.0.0.0/24" = "10.0.0.1" } -} +} \ No newline at end of file diff --git a/modules/terraform-libvirt-network/README.md b/modules/terraform-libvirt-network/README.md index 4185a03..d81dbd0 100644 --- a/modules/terraform-libvirt-network/README.md +++ b/modules/terraform-libvirt-network/README.md @@ -9,14 +9,17 @@ This Terraform module sets up a libvirt network with customizable configurations | Variable Name | Description | Required | Default Value | | --------------------------------------| -------------------------------------------------- | -------- | ------------- | | `network_autostart` | Whether to autostart the libvirt network | ❌ | `true` | -| `network` | Name of the libvirt network | ✔️ | | +| `network_name` | Name of the libvirt network | ✔️ | | | `network_mode` | Mode of the libvirt network | ❌ | `nat` | | `network_domain` | Domain of the libvirt network | ❌ | | | `network_cidr` | CIDR for the libvirt network | ❌ | `["192.168.122.0/24"]` | | `network_bridge` | Bridge for the libvirt network | ❌ | | | `network_mtu` | MTU for the libvirt network | ❌ | | +| `network_dns_enabled` | Whether DNS is enabled for the libvirt network | ❌ | `false` | +| `network_dns_local` | Whether DNS is local-only for the libvirt network | ❌ | `false` | | `network_dhcp_enabled` | Whether DHCP is enabled for the libvirt network | ❌ | `false` | -| `network_dhcp_local` | Whether DHCP is local-only for the libvirt network | ❌ | `false` | +| `network_dhcp_range_start` | DHCP Range start for given address | ❌ | | +| `network_dhcp_range_end` | DHCP Range end for given address | ❌ | | | `network_dnsmasq_options` | Map of dnsmasq options for the libvirt network | ❌ | `{}` | | `network_dns_entries` | Map of DNS entries for the libvirt network | ❌ | `{}` | | `network_routes` | Map of routes for the libvirt network | ❌ | `{}` | @@ -25,16 +28,19 @@ This Terraform module sets up a libvirt network with customizable configurations ```hcl module "libvirt_network" { - source = "github.com/kubealex/terraform-libvirt//modules/terraform-libvirt-network" + source = "kubealex/libvirt-resources/libvirt//modules/terraform-libvirt-pool" network_autostart = true - network = "example_network" + network_name = "example_network" network_mode = "nat" network_domain = "example.com" network_cidr = ["192.168.122.0/24"] network_bridge = "br0" network_mtu = 1500 + network_dns_enabled = true + network_dns_local = false network_dhcp_enabled = true - network_dhcp_local = false + network_dhcp_range_start = "192.168.122.15" + network_dhcp_range_end = "192.168.122.50" network_dnsmasq_options = { "server" = "/example.com/192.168.122.1" } diff --git a/modules/terraform-libvirt-network/main.tf b/modules/terraform-libvirt-network/main.tf index 1d8b549..486f85f 100644 --- a/modules/terraform-libvirt-network/main.tf +++ b/modules/terraform-libvirt-network/main.tf @@ -8,8 +8,8 @@ resource "libvirt_network" "vm_network" { mtu = var.network_mtu dns { - enabled = var.network_dhcp_enabled - local_only = var.network_dhcp_local + enabled = var.network_dns_enabled + local_only = var.network_dns_local dynamic "hosts" { for_each = concat( @@ -20,7 +20,10 @@ resource "libvirt_network" "vm_network" { ip = hosts.value.ip } } + } + dhcp { + enabled = var.network_dhcp_enabled } dnsmasq_options { @@ -35,7 +38,6 @@ resource "libvirt_network" "vm_network" { } } - dynamic "routes" { for_each = var.network_routes content { @@ -44,6 +46,12 @@ resource "libvirt_network" "vm_network" { } } + xml { + xslt = var.network_dhcp_range_start != null && var.network_dhcp_range_start != null ? templatefile("${path.module}/templates/dhcp-range-patch.xslt", { + network_dhcp_range_start = var.network_dhcp_range_start + network_dhcp_range_end = var.network_dhcp_range_end + }) : null + } } data "libvirt_network_dnsmasq_options_template" "options" { diff --git a/modules/terraform-libvirt-network/templates/dhcp-range-patch.xslt b/modules/terraform-libvirt-network/templates/dhcp-range-patch.xslt new file mode 100644 index 0000000..40b930a --- /dev/null +++ b/modules/terraform-libvirt-network/templates/dhcp-range-patch.xslt @@ -0,0 +1,16 @@ + + + + + + + + + + + ${network_dhcp_range_start} + ${network_dhcp_range_end} + + + + diff --git a/modules/terraform-libvirt-network/variables.tf b/modules/terraform-libvirt-network/variables.tf index c0657a3..f30952c 100644 --- a/modules/terraform-libvirt-network/variables.tf +++ b/modules/terraform-libvirt-network/variables.tf @@ -40,18 +40,36 @@ variable "network_mtu" { default = null } -variable "network_dhcp_enabled" { - description = "Whether DHCP is enabled for the libvirt network" +variable "network_dns_enabled" { + description = "Whether DNS is enabled for the libvirt network" + type = bool + default = false +} + +variable "network_dns_local" { + description = "Whether DNS is local-only for the libvirt network" type = bool default = false } -variable "network_dhcp_local" { - description = "Whether DHCP is local-only for the libvirt network" +variable "network_dhcp_enabled" { + description = "Whether DHCP is enabled for the libvirt network" type = bool default = false } +variable "network_dhcp_range_start" { + description = "DHCP range start" + type = string + default = null +} + +variable "network_dhcp_range_end" { + description = "DHCP range end" + type = string + default = null +} + variable "network_dnsmasq_options" { description = "Map of dnsmasq options for the libvirt network" type = map(string)