be53170557
Stop repeating the site slug inside the YAML: terragrunt passes the dc as site_slug and site.yaml carries only site-wide attributes. Declare device types, roles, platforms, custom fields and the dhcp IPAM role once in config/global via their own unit and state; the per-site unit looks them up by slug or name.
69 lines
1.9 KiB
Terraform
69 lines
1.9 KiB
Terraform
# NetBox objects that are GLOBAL, not site-scoped: a second region/dc would
|
|
# collide on name if a per-site unit created them from its own state. They are
|
|
# declared once here and looked up by slug/name from the per-site module.
|
|
locals {
|
|
manufacturers = toset([for t in var.device_types : t.manufacturer])
|
|
|
|
# One interface template per port, flattened to "<type slug>:<name>".
|
|
interface_templates = merge([
|
|
for slug, t in var.device_types : {
|
|
for e in flatten([
|
|
for p in t.ports : [
|
|
for i in range(p.count) : {
|
|
name = format(p.name_format, p.first + i)
|
|
type = p.type
|
|
mgmt_only = p.mgmt_only
|
|
}
|
|
]
|
|
]) : "${slug}:${e.name}" => merge(e, { device_type = slug })
|
|
}
|
|
]...)
|
|
}
|
|
|
|
resource "netbox_manufacturer" "this" {
|
|
for_each = local.manufacturers
|
|
|
|
name = each.value
|
|
slug = lower(replace(each.value, "/[^0-9A-Za-z]+/", "-"))
|
|
}
|
|
|
|
resource "netbox_device_type" "this" {
|
|
for_each = var.device_types
|
|
|
|
manufacturer_id = tonumber(netbox_manufacturer.this[each.value.manufacturer].id)
|
|
model = each.value.model
|
|
slug = each.key
|
|
u_height = each.value.u_height
|
|
}
|
|
|
|
resource "netbox_interface_template" "this" {
|
|
for_each = local.interface_templates
|
|
|
|
device_type_id = tonumber(netbox_device_type.this[each.value.device_type].id)
|
|
name = each.value.name
|
|
type = each.value.type
|
|
mgmt_only = each.value.mgmt_only
|
|
}
|
|
|
|
resource "netbox_device_role" "this" {
|
|
for_each = var.device_roles
|
|
|
|
name = each.key
|
|
slug = lower(replace(each.key, "::", "-"))
|
|
color_hex = each.value.color_hex
|
|
description = each.value.description
|
|
}
|
|
|
|
resource "netbox_platform" "this" {
|
|
for_each = var.platforms
|
|
|
|
name = each.value
|
|
slug = each.key
|
|
}
|
|
|
|
# Role tagging an IP range as DHCP-managed; shared by every site's ranges.
|
|
resource "netbox_ipam_role" "dhcp" {
|
|
name = "dhcp"
|
|
slug = "dhcp"
|
|
}
|