homelab-ansible
Ansible automation for my Proxmox homelab. Deploys Docker stacks, scripts and LXC configuration through Gitea Actions.
Secrets
Stack secrets live in OpenBao, never in this repository. When a stack needs secrets, the deploy reads them from OpenBao on the Ansible controller and writes them to the stack's .env file on the Docker host. The Docker host never talks to OpenBao.
How it works
- Secrets are stored in a KV version 2 engine, one secret per stack, at
<mount>/docker/<stack>. Each key in the secret becomes one variable in the stack's.env. - Ansible logs in with an AppRole whose policy can only read under
docker/. - The OpenBao address and the AppRole's role ID and secret ID are kept in files on the controller, in the folder that
homelab_secrets_dirpoints to. They are never committed. - If
stacks/<stack>/env.j2exists, thedocker_stackrole reads the secret, renders the template to.envwith mode600, and starts the stack. These tasks useno_loganddiff: false, so values never appear in job logs.
One-time setup
In OpenBao
-
Enable a KV version 2 secrets engine, or use an existing one. Set
homelab_bao_kv_mountto its path. -
Create a read-only ACL policy for the Docker deploys:
path "<mount>/data/docker/*" { capabilities = ["read"] } path "<mount>/metadata/docker/*" { capabilities = ["read", "list"] } -
Enable the AppRole auth method, then create a role with that policy and a short token lifetime:
write auth/approle/role/<role> token_policies=<policy> token_ttl=15m token_max_ttl=30m read auth/approle/role/<role>/role-id write -f auth/approle/role/<role>/secret-idThese commands work in the UI's built-in console (the
>_icon), or with thebaoCLI if you prefix them withbao.
On the Ansible controller
-
Create the secrets folder, readable only by the user that runs the CI runner:
sudo install -d -m 750 -o root -g <runner-user> <secrets-dir> -
Create three files in it, each owned by the runner user with mode
600:bao-url: the OpenBao addressbao-role-id: the role IDbao-secret-id: the secret ID
Put only the value in each file.
-
Install the Python library
hvacinto the Python environment Ansible runs from. Thecommunity.hashi_vaultcollection needs it, and the collection itself is installed fromcollections/requirements.ymlon every deploy.
The OpenBao CLI is not needed on the controller. Ansible uses the HTTP API through hvac.
Adding secrets to a stack
-
In OpenBao, create a secret at
docker/<stack>in the KV engine. To import an existing.env, convert it to JSON and paste it into the UI's JSON editor:jq -Rn '[inputs | select(test("^[A-Za-z_][A-Za-z0-9_]*=")) | capture("^(?<key>[^=]+)=(?<value>.*)$")] | from_entries' .envValues are stored exactly as they appear in the file, so remove any surrounding quotes first.
-
Add
stacks/<stack>/env.j2with one line per variable:DB_PASSWORD={{ docker_stack_secrets.DB_PASSWORD }}List every key explicitly. If a key is missing in OpenBao, the deploy fails instead of writing an empty value.
-
Deploy the stack. Compose reads
.envfrom the stack folder automatically.
Changing a secret
Save a new version of the secret in OpenBao, then deploy the stack again. Ansible rewrites .env, and Compose recreates the containers whose environment changed.
Adding another consumer
Give each system that reads secrets its own prefix (for example k8s/ next to docker/), its own policy and its own login. Never use a policy that covers several prefixes, so that one compromised consumer can't read another's secrets.
Troubleshooting
| Symptom | Cause |
|---|---|
| Login fails | Wrong role ID or secret ID, or extra characters in the files |
permission denied when reading |
The policy path doesn't match the secret's path, or the token has expired |
| Reading returns no data | No secret exists at that path |
| A deleted secret is still listed | The UI's Delete is a soft delete in KV version 2. Run delete <mount>/metadata/<path> in the console to remove it with all its versions |
To test access from the controller without Ansible:
TOKEN=$(curl -s "$BAO_ADDR/v1/auth/approle/login" \
-d "{\"role_id\":\"$ROLE_ID\",\"secret_id\":\"$SECRET_ID\"}" | jq -r .auth.client_token)
curl -s -H "X-Vault-Token: $TOKEN" "$BAO_ADDR/v1/<mount>/data/docker/<stack>" | jq '.data.data | keys'
It should print the secret's key names, never the values.