---
title: "Rebuild any server from code · Agent Wranglers"
url: https://agentwranglers.se/playbook/infrastructure/servers/rebuild-from-scratch
updated: 2026-10-20
licence: "All rights reserved"
---

# Rebuild any server from code

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Servers](https://agentwranglers.se/playbook/infrastructure/servers)
- Rule: Keep everything needed to rebuild each server in code, so one command builds a fresh one. Prove it on a new machine in the quarterly [restore drill](https://agentwranglers.se/playbook/operations/disaster-recovery/restore-drills).
- How: The single server is the place where hand-made setup piles up. Write down every package, setting, user, timer and firewall rule in code. Keep the data apart and restore it as in the [restore runbook](https://agentwranglers.se/playbook/operations/disaster-recovery/restore-runbook). Time the drill from an empty machine to a passing health check, against the service's recovery time objective.
- Check: The last rebuild drill is under 3 months old. It built a working server from code and backups within the recovery time objective, without logging in to fix anything.

Example:

```sh
# Drill: a fresh machine, the same code, then the health check
tofu apply -var="name=shop-drill"
ansible-playbook -i shop-drill.example.com, site.yml
./restore-latest-backup.sh shop-drill.example.com
curl -fsS https://shop-drill.example.com/health
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers infrastructure playbook: https://agentwranglers.se/playbook/infrastructure/servers/rebuild-from-scratch`.

- Previous: [Configure long-lived servers from code](https://agentwranglers.se/playbook/infrastructure/servers/configuration-management)
- Next: [Install security updates by themselves](https://agentwranglers.se/playbook/infrastructure/servers/automatic-security-updates)
- [Servers](https://agentwranglers.se/playbook/infrastructure/servers)
