Ansible Playbooks
The configuration layer of my homelab: one playbook per service fills in what its image left blank, writes its secrets from an encrypted vault and brings it up, safe to run again any time.
In short
-
Fills in the blanks
Every image ships with tokens like
__DOMAIN__where anything specific to a deployment goes. Each role swaps in the real values, so one image works anywhere. -
Secrets live in the vault
Each service keeps its secrets in its own encrypted vault file, never in a playbook. A secret two services need is kept once and read from where it lives.
-
Safe to run again
A token that's already been replaced is left alone, and a service only restarts when something it reads has actually changed.
-
Running, not just installed
A run ends with the service up and working: databases created, certificates issued, and the app's own installer run once where it has one.
- Playbooks
- 16
- Roles
- 18
- Tasks
- 185
- Placeholders filled
- 41
- NixOSbuilds the host
- Distrobuilderbakes the image
- OpenTofucreates the container
- Ansibleconfigures it
In detail
The last of four steps
NixOS builds the host, Distrobuilder bakes each service's image, OpenTofu creates its container, and these playbooks finish the job. An image comes complete except for anything specific to a deployment or secret, which it marks with tokens like __DOMAIN__. A run fills those in and brings the service up, so it ends genuinely running rather than just installed. For Traefik, that means real TLS certificates, not just the program in place.
One playbook per service
Each service has a short playbook with its settings at the top, and a role that does the work: tasks, templates, and handlers that restart the service when something changed. A few roles are shared: one gets a TLS certificate by DNS challenge and renews it daily, one sets up the three websites, and one creates databases.
Filling in the blanks is usually one task with a list of tokens:
- name: Wire the real secrets into Apache's envvars
ansible.builtin.replace:
path: /etc/apache2/envvars
regexp: "{{ item.token }}"
replace: "{{ item.value }}"
loop:
- { token: '__OIDC_CLIENT_ID__', value: "{{ oidc_client_id }}" }
- { token: '__OIDC_CLIENT_SECRET__', value: "{{ oidc_client_secret }}" }
no_log: true
notify: restart apache2
no_log keeps the values out of the output. Apache only reads that file when it starts, so the notify matters: it restarts Apache, but only if a token was actually replaced.
Secrets
- One vault file per service. Ansible Vault encrypts whole files, not single values, so each service's secrets get a file of their own next to the inventory, loaded automatically for that host.
- One copy of each secret. When two services need the same secret, it lives in the vault of the one that owns it, and the other reads it from there. Changing it is one edit, then a re-run of each playbook that uses it.
- One password opens them all, so a single prompt covers a play that reads from several vaults.
- Generated, not stored. Passwords nobody needs to type are generated on the first run and kept outside the repository.
Reaching the containers
At home, tasks run through Incus's own API, so a container needs Python but no SSH server or login account. On the VPS they still go over SSH through the host, until it's rebuilt on NixOS and moves over too. Either way, the tasks run inside the container, so the roles don't care which.
House rules
- Settings in the playbook, secrets in the vault, work in the role.
- Every run is safe to repeat. Nothing should break or be duplicated by a second run.
- Look before you leap.
--check --diffshows what a run would change on a live container without changing it. - Each playbook says how to become root, since Alpine uses
doasand the otherssudo. - Every service documented, with what its vault needs and any steps a person has to do.
Lessons learned the hard way
A vault in the wrong folder is silently ignored. Files under group_vars/ only load for a group of that name, and each service is a host. The vault was there, with the right key, and was simply never read. Host files belong in host_vars/.
Never rotate a key by accident. One app's role rewrites its settings file on every run, and a fresh encryption key would make everything already encrypted unreadable. It took three fixes, found by fingerprinting the key line across runs, to make sure the existing key always survives.
Some installers need a person. Forgejo's web installer writes its own config. The role prepares the database beforehand, and the steps that need the finished config are tagged to run only when asked for.
The image decides what's on the container. The database modules need a Python driver on the container itself, so it's baked into every image that uses a database rather than installed at run time.