OpenTofu Plans

Modular infrastructure-as-code plans for my home network, home server and VPS. Reusable, versioned, thoroughly tested and fully documented.

In short

Workspaces
3
Networks
10
Firewall rules
30
Test checks
20
  1. NixOSbuilds the host
  2. Distrobuilderbakes the image
  3. OpenTofucreates the container
  4. Ansibleconfigures it

In detail

Three workspaces, kept apart

The plans are written for OpenTofu, the open-source fork of Terraform, and split into three workspaces, each with its own state:

  • Network: the UniFi side of the home network: networks and VLANs, Wi-Fi, firewall zones and the rules between them, port forwards and the RADIUS profile.
  • Home server: the identity containers, FreeIPA and FreeRADIUS, in their own Incus project on the identity network.
  • VPS: the public-facing containers: Traefik, mail, Forgejo and the websites.

They're different machines with different networks, so no single tofu apply should ever touch two of them.

Declaring a container

Each container lives in its own module, and the workspace says which ones it wants and where they plug in:

module "freeipa" {
  source  = "./containers/freeipa"
  bridge  = var.identity_bridge
  project = "identity"
}

The module creates the container from its Distrobuilder image and hands it a cloud-init file for its first boot and its network. Then it's the usual loop: tofu plan shows what would change, tofu apply makes it so, and running the plan again should say there's nothing left to do.

Tests that need no network

The network workspace has a test file that runs tofu plan against a mock of the UniFi provider, so it checks the design without touching the real controller. Five runs and twenty checks, including:

  • The DMZ gets exactly one allow rule.
  • Only ports 80 and 443 are forwarded from the internet, and only to the edge Traefik.
  • The one hole through to the directory's LDAPS port is for one service, on that port only.
  • Turning on RADIUS without a shared secret fails the plan, on purpose.

House rules

  • One source of truth. The network design is one map in the code, and the written architecture describes the same thing. Change both together.
  • Look it up, don't own it. Bridges the host's own config makes are read with a data source, so the two can never disagree.
  • Flags for chicken-and-egg steps. DHCP keeps handing out the gateway for DNS until freeipa_ready is turned on, and the main Wi-Fi stays on a passphrase until radius_ready is. The tests cover both sides of each.
  • Secrets stay secret. Passphrases and shared secrets are sensitive variables, so they never show in a plan's output.
  • Every module documented, with a page per service on what it needs and how to apply it.

Lessons learned the hard way

Handing something over needs a removed block. Tofu used to create the VPS's bridge. When the host's own config took it over, just deleting the resource from the code would have made the next apply destroy the bridge every container sits on. A removed block with destroy = false forgets it and leaves it running.

Code you stop using can still delete things. Containers taken out of a workspace are still in its state, so the next apply plans to destroy them. Moving them somewhere else means moving the state too, or applying only against a fresh host.

Not everything has a resource. The UniFi provider can't make WireGuard servers or set some IPv6 options, so those are done by hand and listed in comments beside the code that depends on them.

Flags beat clever ordering. DNS can't point at FreeIPA before FreeIPA exists. A plain true/false variable, flipped once it's up, is easier to follow than tricks to make one apply do both.

All projects