Infrastructure as code
How I run my own infrastructure, firewall and DNS included, as code in one repository, with Terraform for what exists, Ansible for what runs, an independent review required on every merge request, a yes before every live apply, and a read-only drift check.
Infrastructure that lives in one person’s head cannot be rebuilt by anyone else, and it cannot be handed over. I run my own: a firewall, DNS, virtual machines on a small cluster, a secrets vault, a self-hosted GitLab, and a mail server. It is managed as code from one private repository. Changes land by merge request (MR), under a rule that every MR gets an independent review: 564 merged so far, all since September 2026.
Two results show what that buys in practice. The firewall ruleset was imported into Terraform with zero changes to the live firewall: 133 objects in the first import, adopted as they were, then cleaned up in small reviewed changes. And a drift check is set to compare the Terraform code with the live state every night and after every merge, and to open an issue when they disagree.
Where it started
In July 2026 my infrastructure code sat in three small repos. The architecture decision record (ADR) that replaced them describes them plainly: “Three dormant projects each solved a third of infrastructure-as-code.” One discovered what was running, one held the desired state and the disaster-recovery notes, and one ran the health checks. Each saw only part of the fleet, and their registries were kept by hand. Hand-kept lists rot.
On July 7, 2026, I accepted ADR-001: one repo, built from the parts that worked. Much of what follows comes from that record and the ones after it.
Who does what
The decisions are mine: ADR-001, ADR-002, and ADR-004, the split between tools, the rule that every change goes through code, and a yes before every live apply. The work is done with a team of AI agents, and “we” on this page means that team. Claude agents write most of the code, open most of the MRs, and produce the Terraform plans. Codex, a model from another vendor, reviews them. Nearly every non-merge commit in the repo carries an agent co-author line.
I am not an Ansible expert, and the repo’s rule is to teach as it goes. ADR-001 says so: “every playbook should read like a lesson, every runbook should explain why.” It also names the cost and who carries it: “Ansible learning curve for the operator — mitigated by teaching-style docs (see preamble) and by Claude carrying the syntax.”
Decisions
One repo, built from what worked
The three old repos each did a few days of real work. Keeping them meant three partial views of the fleet, each with its own assumptions. ADR-001 started a new, clean repo instead and listed what to port and what to drop: the discovery plugins, the recovery playbook and notes, the runbook template, and a few helpers came over; dead integrations and an old dashboard did not. The cost was a port and a new home for everything. The gain was one place where the state of the infrastructure is written down.
Terraform provisions, Ansible configures
Two tools, each with one job. The rule is the doctrine line at the top of the Terraform docs: “Terraform PROVISIONS, Ansible CONFIGURES.” Terraform owns the things it can create and track: virtual machines, DNS records, firewall objects, identities, Git hosting settings. Ansible configures what runs inside a machine, and it deploys Docker Compose files rather than replacing them: “Compose files remain the source of truth for what runs.” Terraform state lives in PostgreSQL, one schema per stack, with locking.
I set that split on September 12, 2026. The AI agents had more than once steered me away from Terraform; I kept it. The firewall followed. ADR-001 had planned it through Ansible, and from July it was managed mostly that way. On October 29, 2026, we imported the ruleset into Terraform (four held port forwards followed two days later), and from the next day firewall changes went through Terraform MRs. ADR-001 had predicted the effect: “The firewall’s configuration becomes reviewable text instead of clicked state”. In the days after the import, a run of small MRs removed leftover rules and merged overlapping ones.
Generate the lists; never keep them by hand
A list that someone has to remember to update will drift. So the lists are generated from the code, and CI fails when a generated file is stale. A coverage file records, for every host, whether code manages it, with a strict rule: “Terraform alone does not count.” A host counts only when a playbook targets it. Today 24 of 25 hosts are covered; the last one is a recorded, accepted exception. An identity registry works the same way: a row that “lacks purpose, owner, consumers or why_not_reuse” fails the build.
Merge is not apply
An MR merged into the repo changes nothing on the live systems by itself. The rule in the repo’s map is short: “Merge is not apply. No Ansible run without --check first, no Terraform apply without a saved plan you read. A live apply needs Carlos’s yes in this session.” CI runs offline checks only, “no secrets, no SSH, no Terraform or Ansible runs,” so the plan is made outside CI, and its summary goes into the MR. A reviewer has blocked a Terraform MR for nothing more than a missing plan.
One firewall change on October 30, 2026, shows the loop (times are Hawaii time). At 08:51 Codex returned “REWORK” on the first version, at a named commit, with a P1 finding. The author pushed a new version and asked for a review of “this head,” with the plan summary: one change, one removal. Two more reviews still found a blocker. At 09:17 the fourth review was a pass at a named commit. I said yes at 09:20. The change was applied from 09:21 to 09:24, with a backup first, and a new plan afterwards showed no changes. At 09:33 I ran the live test myself, and it passed. Four reviews took twenty-six minutes.
A drift check that can only read
Code and live systems can drift apart. A drift check on the control host runs a Terraform plan against each of four state stacks once a night and after every merge to main. It can do nothing else: the script runs init and plan and refuses any other command, it never takes the state lock, and it reads secrets with a read-only identity whose token is revoked at the end of each pass. When a stack drifts, it opens or updates one GitLab issue for that stack, and it closes the issue when the stack is clean again. ADR-001’s rule holds in the code: “NO auto-apply to production, ever.”
It shipped switched off, to be turned on with my yes. I said yes on October 23, 2026. The first thing it found was an account disabled on the live system but still enabled in code. The check does not decide which side is right. A person does. I said the live side was right, and the code was changed to match.
Even a one-off change goes through code
In October 2026 an agent proposed moving a small container by hand, just once. I said no: infrastructure changes go through code, even one-off moves. The repo’s map now says it as a rule: “A host change is code in this repo first. Do not fix a host by hand and leave the repo behind.” A later lesson goes one step further: a change should apply to the whole group of hosts with the same role, not to one host.
Patch age by threat, not one number
Our rule had been one flat number: install nothing younger than 30 days. ADR-004, which I approved on November 17, 2026, names the problem with it: “Exploits arrive in minutes to days,” while poisoned releases on public registries are real, and “the 30-day wait is what gives scanners and maintainers time to catch them.” Its core sentence: “One number cannot serve both.”
So the minimum age of a fix now depends on two things: how dangerous the flaw is (on the US government’s list of exploited vulnerabilities, critical by score or likely to be exploited, or neither) and where the fix comes from. A signed update from a distribution channel can go in after 1, 7, or 30 days by tier; a digest-pinned vendor image after 7, 14, or 30; a package from a public registry after 14 days plus a compromise check, 21, or 30. “No fix, no exception.” A script enforces the rule and fails closed, and changing a number takes a new ADR.
How it’s built
- Layout: Ansible roles and playbooks, Terraform stacks, a Python discovery tool, governance checks, docs, and ADRs in one repo. 72 roles, 111 playbooks, 87 Terraform files in four state stacks and a VM module, five ADR files.
- In code: the firewall and router, DNS, virtual machines and the hypervisor cluster, secrets and identities, Git hosting, a CDN edge, a mesh VPN, mail, databases, backups, monitoring, a managed switch, and the controls for the AI agents themselves.
- CI: 43 offline jobs on a pinned image, and 127 test files that run roles and scripts against fakes (a fake container runtime, a fake firewall API, a fake vault). Examples: the generated firewall import must equal the Terraform files, and rendered configs must pass the daemon’s own syntax checker.
- Secrets: HashiCorp Vault, read through two wrappers into the environment for one command. The rule: a secret value is never typed, printed, or committed.
- Pins: providers pinned to exact versions; the drift check’s Terraform download pinned by SHA-256; the Ansible controller image pins its collections.
Where it stands
1,570 commits since the first one on July 7, 2026. 564 merged MRs, all since September 2026. 153 closed issues. In the order they reached Terraform or CI: virtual machines, DHCP reservations, and DNS in September; the mesh VPN, the CDN edge, and Git hosting in early October; the coverage gate and the drift check on October 22 and 23; the full firewall ruleset at the end of October.
What’s next
The goal I set on October 11, 2026, is full coverage: every gap closed until the whole fleet is in code. Open work includes peer groups, so that hosts with the same role get the same capabilities, and folding the drift rules into the docs. What remains for coverage is not in my head. It is in the coverage file: 24 of 25 hosts covered, one accepted exception, and the open work written down as named human actions.