Files
terraform-netbox/modules/netbox/networks.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

66 lines
1.9 KiB
Terraform

locals {
# "<site>:<vid>" -> {site, vid, name, description}.
site_vlans = merge([
for site, v in var.vlans : {
for vl in v.vlans : "${site}:${vl.vid}" => merge(vl, { site = site })
}
]...)
subnet_prefix = { for k, v in var.subnets : k => v.prefix }
}
resource "netbox_vlan_group" "this" {
for_each = var.vlans
name = "${each.key} vlans"
slug = "${each.key}-vlans"
scope_type = "dcim.site"
scope_id = tonumber(netbox_site.this[each.key].id)
vid_ranges = each.value.vid_ranges
}
resource "netbox_vlan" "this" {
for_each = local.site_vlans
name = each.value.name
vid = each.value.vid
site_id = tonumber(netbox_site.this[each.value.site].id)
group_id = tonumber(netbox_vlan_group.this[each.value.site].id)
status = "active"
description = each.value.description
}
# Network profiles: the interfaces a logical host should have. Published as a
# config context so the UI reads them from the NetBox API rather than from git.
resource "netbox_config_context" "network_profile" {
for_each = var.network_profiles
name = "network-profile:${each.key}"
description = each.value.description
weight = each.value.weight
sites = [for s in each.value.sites : tonumber(netbox_site.this[s].id)]
roles = [for r in each.value.roles : tonumber(netbox_device_role.this[r].id)]
data = jsonencode({
network_profile = {
name = each.key
interfaces = [
for i in each.value.interfaces : {
name = i.name
vlan = i.vlan
subnet = i.subnet
prefix = local.subnet_prefix[i.subnet]
subinterfaces = [
for s in i.subinterfaces : {
name = s.name
vlan = s.vlan
subnet = s.subnet
prefix = s.subnet != null ? local.subnet_prefix[s.subnet] : null
}
]
}
]
}
})
}