Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3069d62fb3 | ||
|
|
72485d6037 | ||
|
|
69a1c0a9fb | ||
|
|
6e2dd06b9e | ||
|
|
5309aa0cdd | ||
|
|
27240cef17 | ||
|
|
90324a0501 | ||
|
|
4852971991 | ||
|
|
4d8c3cc0b4 | ||
|
|
431702ff74 |
No files matched your search
@@ -105,7 +105,7 @@ Keep it short:
|
|||||||
|
|
||||||
- PR link and commits.
|
- PR link and commits.
|
||||||
- The review result, with any pre-existing findings or nits worth a follow-up.
|
- The review result, with any pre-existing findings or nits worth a follow-up.
|
||||||
- What the merge triggers, from `on.push` in `.gitea/workflows/`. Today no workflow deploys on merge; say so until that changes.
|
- What the merge triggers, from `on.push` in `.gitea/workflows/`. Say whether the PR touched a `deploy-stacks.yaml` path, and so deployed.
|
||||||
- Anything left uncommitted.
|
- Anything left uncommitted.
|
||||||
|
|
||||||
Do not delete the branch, dispatch a workflow, or change a live system.
|
Do not delete the branch, dispatch a workflow, or change a live system.
|
||||||
@@ -2,6 +2,16 @@ name: Deploy Stacks
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
paths:
|
||||||
|
- stacks/**
|
||||||
|
- roles/docker_stack/**
|
||||||
|
- playbooks/docker_stacks.yml
|
||||||
|
- inventory/**
|
||||||
|
- collections/requirements.yml
|
||||||
|
- .gitea/workflows/deploy-stacks.yaml
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
deploy-stacks:
|
deploy-stacks:
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ Branch → PR (lint, secret scan, later `--check --diff`) → merge to protected
|
|||||||
|
|
||||||
- Keep the `/home/pi/docker/<service>/` paths, because Compose derives the project name from the folder.
|
- Keep the `/home/pi/docker/<service>/` paths, because Compose derives the project name from the folder.
|
||||||
- Ansible manages config, never data. No ownership changes under `DOCKER_DATA_ROOT` (mapped UIDs).
|
- Ansible manages config, never data. No ownership changes under `DOCKER_DATA_ROOT` (mapped UIDs).
|
||||||
- When adopting a stack: pin its image and remove its Watchtower label.
|
- When adopting a stack: remove its Watchtower label. Pinning images is a later task I'll do myself; don't flag unpinned or `:latest` images.
|
||||||
- Gitea, its runner and Postgres never auto-deploy. I trigger them from the `ansible` LXC.
|
- Gitea, its runner and Postgres never auto-deploy. I trigger them from the `ansible` LXC.
|
||||||
|
|
||||||
## Secrets
|
## Secrets
|
||||||
@@ -42,7 +42,7 @@ Only commit when I explicitly say "commit" for that change. A yes to a question
|
|||||||
|
|
||||||
The remote is a self-hosted Gitea, so `gh` doesn't work. For PRs and their CI status use `.claude/scripts/gitea.sh` (acts as `claude-bot`), e.g. `.claude/scripts/gitea.sh GET '/repos/kasun/homelab-ansible/pulls?state=open'`. Use it for PRs, and for issues when I ask. Reading from Gitea is free.
|
The remote is a self-hosted Gitea, so `gh` doesn't work. For PRs and their CI status use `.claude/scripts/gitea.sh` (acts as `claude-bot`), e.g. `.claude/scripts/gitea.sh GET '/repos/kasun/homelab-ansible/pulls?state=open'`. Use it for PRs, and for issues when I ask. Reading from Gitea is free.
|
||||||
|
|
||||||
Open and merge PRs through `/merge`: typing it is my OK to commit, open a PR and merge for that run. Outside `/merge`, ask first. Every merge call asks for permission (also inside `/merge`), and `.claude/hooks/guard-merge.sh` refuses it unless the PR's newest review comment is a pass for its head commit. Before deploy-on-merge is enabled, revisit this: PRs that deploy will need my approval.
|
Open and merge PRs through `/merge`: typing it is my OK to commit, open a PR and merge for that run. Outside `/merge`, ask first. Every merge call asks for permission (also inside `/merge`), and `.claude/hooks/guard-merge.sh` refuses it unless the PR's newest review comment is a pass for its head commit. Merging deploys: a push to `main` that touches the paths in `.gitea/workflows/deploy-stacks.yaml` runs the stacks playbook, so allowing the merge prompt is also my OK to deploy.
|
||||||
|
|
||||||
## Verifying changes
|
## Verifying changes
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,99 @@
|
|||||||
# homelab-ansible
|
# homelab-ansible
|
||||||
|
|
||||||
Ansible automation for my Proxmox homelab. Deploys Docker stacks, scripts and LXC configuration through Gitea Actions.
|
Ansible automation for my Proxmox homelab. Deploys Docker stacks, scripts and LXC configuration through Gitea Actions.
|
||||||
|
|
||||||
|
## Secrets
|
||||||
|
|
||||||
|
Stack secrets live in [OpenBao](https://openbao.org/), 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_dir` points to. They are never committed.
|
||||||
|
- If `stacks/<stack>/env.j2` exists, the `docker_stack` role reads the secret, renders the template to `.env` with mode `600`, and starts the stack. These tasks use `no_log` and `diff: false`, so values never appear in job logs.
|
||||||
|
|
||||||
|
### One-time setup
|
||||||
|
|
||||||
|
**In OpenBao**
|
||||||
|
|
||||||
|
1. Enable a KV version 2 secrets engine, or use an existing one. Set `homelab_bao_kv_mount` to its path.
|
||||||
|
2. Create a read-only ACL policy for the Docker deploys:
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
path "<mount>/data/docker/*" { capabilities = ["read"] }
|
||||||
|
path "<mount>/metadata/docker/*" { capabilities = ["read", "list"] }
|
||||||
|
```
|
||||||
|
|
||||||
|
3. 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-id
|
||||||
|
```
|
||||||
|
|
||||||
|
These commands work in the UI's built-in console (the `>_` icon), or with the `bao` CLI if you prefix them with `bao`.
|
||||||
|
|
||||||
|
**On the Ansible controller**
|
||||||
|
|
||||||
|
1. Create the secrets folder, readable only by the user that runs the CI runner:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo install -d -m 750 -o root -g <runner-user> <secrets-dir>
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Create three files in it, each owned by the runner user with mode `600`:
|
||||||
|
- `bao-url`: the OpenBao address
|
||||||
|
- `bao-role-id`: the role ID
|
||||||
|
- `bao-secret-id`: the secret ID
|
||||||
|
|
||||||
|
Put only the value in each file.
|
||||||
|
3. Install the Python library `hvac` into the Python environment Ansible runs from. The `community.hashi_vault` collection needs it, and the collection itself is installed from `collections/requirements.yml` on every deploy.
|
||||||
|
|
||||||
|
The OpenBao CLI is not needed on the controller. Ansible uses the HTTP API through `hvac`.
|
||||||
|
|
||||||
|
### Adding secrets to a stack
|
||||||
|
|
||||||
|
1. 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:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
jq -Rn '[inputs | select(test("^[A-Za-z_][A-Za-z0-9_]*=")) | capture("^(?<key>[^=]+)=(?<value>.*)$")] | from_entries' .env
|
||||||
|
```
|
||||||
|
|
||||||
|
The role writes values back unquoted, as `KEY=value`, so Compose reads them the same way it reads the original file. Import values exactly as they appear. When you set a new value, avoid `$`, `#`, quotes and spaces: Compose would interpret them.
|
||||||
|
2. Add `stacks/<stack>/env.j2` with 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.
|
||||||
|
3. Deploy the stack. Compose reads `.env` from 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:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
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.
|
||||||
@@ -2,3 +2,5 @@
|
|||||||
collections:
|
collections:
|
||||||
- name: community.docker
|
- name: community.docker
|
||||||
version: "5.4.1"
|
version: "5.4.1"
|
||||||
|
- name: community.hashi_vault
|
||||||
|
version: "7.1.0"
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
---
|
||||||
|
# Files on the ansible LXC that hold deploy credentials. They are never committed.
|
||||||
|
homelab_secrets_dir: /etc/homelab-ansible
|
||||||
|
|
||||||
|
# OpenBao address and AppRole login, read on the ansible LXC when a stack needs secrets.
|
||||||
|
homelab_bao_url: "{{ lookup('ansible.builtin.file', homelab_secrets_dir + '/bao-url') }}"
|
||||||
|
homelab_bao_role_id: "{{ lookup('ansible.builtin.file', homelab_secrets_dir + '/bao-role-id') }}"
|
||||||
|
homelab_bao_secret_id: "{{ lookup('ansible.builtin.file', homelab_secrets_dir + '/bao-secret-id') }}"
|
||||||
|
homelab_bao_kv_mount: secret
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
---
|
||||||
|
# Run by hand from the ansible LXC only. Gitea runs the deploy pipeline, so no
|
||||||
|
# workflow may call this playbook: a broken deploy would take down the pipeline itself.
|
||||||
|
- name: Deploy Gitea
|
||||||
|
hosts: docker_hosts
|
||||||
|
gather_facts: false
|
||||||
|
tasks:
|
||||||
|
- name: Deploy gitea
|
||||||
|
ansible.builtin.include_role:
|
||||||
|
name: docker_stack
|
||||||
|
vars:
|
||||||
|
docker_stack_name: gitea
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
---
|
---
|
||||||
docker_stack_root: "{{ homelab_stacks_root }}"
|
docker_stack_root: "{{ homelab_stacks_root }}"
|
||||||
docker_stack_name: ""
|
docker_stack_name: ""
|
||||||
|
docker_stack_src: "{{ playbook_dir }}/../stacks/{{ docker_stack_name }}"
|
||||||
|
# Stacks with an env.j2 get their .env rendered from this OpenBao KV path.
|
||||||
|
docker_stack_env_template: "{{ docker_stack_src }}/env.j2"
|
||||||
|
docker_stack_secret_path: "docker/{{ docker_stack_name }}"
|
||||||
@@ -14,10 +14,32 @@
|
|||||||
|
|
||||||
- name: Copy compose file for {{ docker_stack_name }}
|
- name: Copy compose file for {{ docker_stack_name }}
|
||||||
ansible.builtin.copy:
|
ansible.builtin.copy:
|
||||||
src: "{{ playbook_dir }}/../stacks/{{ docker_stack_name }}/docker-compose.yml"
|
src: "{{ docker_stack_src }}/docker-compose.yml"
|
||||||
dest: "{{ docker_stack_root }}/{{ docker_stack_name }}/docker-compose.yml"
|
dest: "{{ docker_stack_root }}/{{ docker_stack_name }}/docker-compose.yml"
|
||||||
mode: "0644"
|
mode: "0644"
|
||||||
|
|
||||||
|
# no_log and diff: false keep secret values out of the job log, which also runs with --diff.
|
||||||
|
- name: Read secrets for {{ docker_stack_name }}
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
docker_stack_secrets: >-
|
||||||
|
{{ lookup('community.hashi_vault.vault_kv2_get', docker_stack_secret_path,
|
||||||
|
engine_mount_point=homelab_bao_kv_mount,
|
||||||
|
url=homelab_bao_url,
|
||||||
|
auth_method='approle',
|
||||||
|
role_id=homelab_bao_role_id,
|
||||||
|
secret_id=homelab_bao_secret_id).secret }}
|
||||||
|
when: docker_stack_env_template is file
|
||||||
|
no_log: true
|
||||||
|
|
||||||
|
- name: Write env file for {{ docker_stack_name }}
|
||||||
|
ansible.builtin.template:
|
||||||
|
src: "{{ docker_stack_env_template }}"
|
||||||
|
dest: "{{ docker_stack_root }}/{{ docker_stack_name }}/.env"
|
||||||
|
mode: "0600"
|
||||||
|
when: docker_stack_env_template is file
|
||||||
|
diff: false
|
||||||
|
no_log: true
|
||||||
|
|
||||||
- name: Start {{ docker_stack_name }}
|
- name: Start {{ docker_stack_name }}
|
||||||
community.docker.docker_compose_v2:
|
community.docker.docker_compose_v2:
|
||||||
project_src: "{{ docker_stack_root }}/{{ docker_stack_name }}"
|
project_src: "{{ docker_stack_root }}/{{ docker_stack_name }}"
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
services:
|
||||||
|
server:
|
||||||
|
image: gitea/gitea:latest
|
||||||
|
container_name: gitea
|
||||||
|
environment:
|
||||||
|
- USER_UID=${NON_ROOT_USER_UID}
|
||||||
|
- USER_GID=${NON_ROOT_USER_UID}
|
||||||
|
- GITEA__database__DB_TYPE=${DB_TYPE}
|
||||||
|
- GITEA__database__HOST=${DB_HOST}
|
||||||
|
- GITEA__database__NAME=${DB_NAME}
|
||||||
|
- GITEA__database__USER=${DB_USER}
|
||||||
|
- GITEA__database__PASSWD=${DB_PASSWD}
|
||||||
|
- GITEA__server__ROOT_URL=${GITEA__server__ROOT_URL}
|
||||||
|
restart: always
|
||||||
|
networks:
|
||||||
|
- pg_network
|
||||||
|
volumes:
|
||||||
|
- ${DOCKER_DATA_ROOT}/gitea:/data
|
||||||
|
- /etc/timezone:/etc/timezone:ro
|
||||||
|
- /etc/localtime:/etc/localtime:ro
|
||||||
|
ports:
|
||||||
|
- "3000:3000"
|
||||||
|
- "222:22"
|
||||||
|
gitea-runner:
|
||||||
|
image: gitea/runner:latest
|
||||||
|
container_name: gitea-runner
|
||||||
|
restart: always
|
||||||
|
depends_on:
|
||||||
|
- server
|
||||||
|
networks:
|
||||||
|
- pg_network
|
||||||
|
volumes:
|
||||||
|
- /var/run/docker.sock:/var/run/docker.sock
|
||||||
|
- ${DOCKER_DATA_ROOT}/gitea-runner:/data
|
||||||
|
environment:
|
||||||
|
- GITEA_INSTANCE_URL=${GITEA__server__ROOT_URL}
|
||||||
|
- GITEA_RUNNER_REGISTRATION_TOKEN=${RUNNER_TOKEN}
|
||||||
|
- GITEA_RUNNER_NAME=proxmox-lab-runner
|
||||||
|
- GITEA__server__ROOT_URL=${GITEA__server__ROOT_URL}
|
||||||
|
networks:
|
||||||
|
pg_network:
|
||||||
|
external: true
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
# Rendered by Ansible from OpenBao secret/docker/gitea. Do not edit on the host.
|
||||||
|
NON_ROOT_USER_UID={{ docker_stack_secrets.NON_ROOT_USER_UID }}
|
||||||
|
DB_TYPE={{ docker_stack_secrets.DB_TYPE }}
|
||||||
|
DB_HOST={{ docker_stack_secrets.DB_HOST }}
|
||||||
|
DB_NAME={{ docker_stack_secrets.DB_NAME }}
|
||||||
|
DB_USER={{ docker_stack_secrets.DB_USER }}
|
||||||
|
DB_PASSWD={{ docker_stack_secrets.DB_PASSWD }}
|
||||||
|
GITEA__server__ROOT_URL={{ docker_stack_secrets.GITEA__server__ROOT_URL }}
|
||||||
|
DOCKER_DATA_ROOT={{ docker_stack_secrets.DOCKER_DATA_ROOT }}
|
||||||
|
RUNNER_TOKEN={{ docker_stack_secrets.RUNNER_TOKEN }}
|
||||||
Reference in new issue
Block a user