Files
terraform-netbox/modules/netbox/main.tf
T
unkin-agent efeb148be7
ci/woodpecker/pr/plan Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
Recut the module to the NetBox catalogue only
Device records, interfaces, MACs and cabling are written by the
autodiscovery agent, and host names, roles and IP allocation by the UI, so
terraform declaring them fights both owners.

- drop the device, interface, MAC, per-host IP and net:<name> tag resources
- declare sites, device types, roles and platforms in their own config files
- add interface templates per device type and model the Omada SG3429X
- add per-site VLAN groups and VLANs, seeding vid 101 and 201 in syd1
- publish network profiles as site-scoped config contexts
- rename subnets to net-<a>-<b>-<c>-<d>-<len> and restore dns/domain/next_server
2026-10-03 17:26:43 +10:00

62 lines
1.8 KiB
Terraform

locals {
# Netmask suffix per subnet, e.g. "24".
masks = { for k, v in var.subnets : k => split("/", v.prefix)[1] }
# Subnets that request a DHCP scope.
dhcp_subnets = { for k, v in var.subnets : k => v if try(v.dhcp.enabled, false) }
# Subnets that declare a gateway.
gateways = { for k, v in var.subnets : k => v if v.router != null }
}
# A site that already exists in NetBox must be imported rather than re-created
# (NetBox site names are unique).
resource "netbox_site" "this" {
for_each = var.sites
name = each.value.name
slug = each.key
description = each.value.description
}
resource "netbox_prefix" "this" {
for_each = var.subnets
prefix = each.value.prefix
status = "active"
description = each.value.description
site_id = each.value.site != null ? tonumber(netbox_site.this[each.value.site].id) : null
}
# Role tagging a range as DHCP-managed; created once and shared by every range.
resource "netbox_ipam_role" "dhcp" {
name = "dhcp"
slug = "dhcp"
}
resource "netbox_ip_range" "dhcp" {
for_each = local.dhcp_subnets
start_address = "${cidrhost(each.value.prefix, each.value.dhcp.start)}/${local.masks[each.key]}"
end_address = "${cidrhost(each.value.prefix, each.value.dhcp.stop)}/${local.masks[each.key]}"
role_id = netbox_ipam_role.dhcp.id
status = "active"
description = "DHCP pool for ${each.key}"
}
resource "netbox_ip_address" "gateway" {
for_each = local.gateways
ip_address = "${cidrhost(each.value.prefix, each.value.router)}/${local.masks[each.key]}"
status = "active"
description = "gateway"
}
resource "netbox_ip_address" "managed" {
for_each = { for m in var.managed_ips : m.ip => m }
ip_address = each.value.ip
status = "active"
description = each.value.description
}