iac/ansible/README.md
Sergei Poljanski 56dd0255dc
All checks were successful
deploy / ansible (push) Successful in 8m12s
monitoring: watch peers, devices, accounts and logins
Monitoring covered availability only: a new WireGuard peer, a machine
on the private network, an extra account or key, or a login from
outside the VPN would have gone unnoticed.

security-metrics runs every minute on both hosts and exports what it
finds for node_exporter: WireGuard peers and where they connect from,
addresses answering a ping sweep of the VPC, login accounts, sudo and
docker members, listening ports, containers with host-level access,
and fingerprints of the account, sudoers, sshd, WireGuard and
authorized_keys files. Each item is compared with a baseline in the
role defaults and host_vars and exported with known="true|false", so a
deliberate change has to update the baseline with it. On ops it also
counts Forgejo users, keys and runners.

Grafana gets a Security dashboard and an alert group for anything
unknown, a changed access file, an SSH login from outside WireGuard,
failed-login bursts on SSH, Forgejo, Grafana and the asxp.io admin, a
new container, and for logs or the collector itself going quiet, which
would otherwise blind the rest.

It never reads `wg show dump`, which prints preshared keys.

Assisted-by: Claude:fable-5-2
2026-11-15 21:59:16 +04:00

128 lines
5.1 KiB
Markdown

# Ansible
`site.yaml` is the desired state of both servers and is applied by the
deploy pipeline on every push to `main`. The playbooks under
`playbooks/` are host bootstrap and are run by hand.
## Running
```sh
echo '<vault password>' > .vault_pass && chmod 600 .vault_pass
ansible-playbook site.yaml --check --diff # dry run, over WireGuard
ansible-playbook site.yaml # apply
ansible-playbook site.yaml --tags forgejo # one role
```
On NixOS: `nix shell nixpkgs#ansible -c ansible-playbook …`.
Templates that carry secrets are never shown in `--diff` output.
## Inventories
- `inventory/hosts.yaml`: default. Connects as root to the WireGuard
addresses.
- `inventory/ci.yaml`: used by the pipeline. Connects as `deploy` over
the private network, reaching asxpio-services through asxpio-ops.
`known_hosts` pins both servers' SSH host keys for the pipeline.
## Variables
```
inventory/
├── group_vars/all/
│ ├── network.yaml # VPC and WireGuard addresses
│ ├── paths.yaml # /opt
│ ├── s3.yaml # object storage buckets and s3fs mounts
│ ├── wireguard.yaml # WireGuard peers
│ └── vault.yaml # encrypted secrets
├── group_vars/ops_servers/
│ ├── traefik.yaml # traefik_version
│ └── forgejo.yaml # forgejo_version, domain and aliases
├── group_vars/services_servers/
│ ├── forgejo_runner.yaml # runner version, labels' nix image
│ └── minecraft.yaml
└── host_vars/ # per-host addresses, SSH and s3fs settings
```
Compose files without variables are plain files under
`roles/<role>/files/`; edit the image tag there.
## Roles in site.yaml
`base`, `traefik`, `crawler_alert`, `forgejo`, `forgejo_runner`,
`sillytavern`, `minecraft`, `xmpp`, `xray`, `restic_backup`. Service roles go
through `docker_service`, which writes the files, runs
`docker compose up -d`, restarts on a config change and waits for the
service to answer.
`crawler_alert` installs a copy of
[traefik-crawlers-statistics](https://git.asxp.io/asxpi/traefik-crawlers-statistics);
update `roles/crawler_alert/files/` from there.
## Security monitoring
`security-metrics` (role `monitoring_agent`, every minute on both hosts)
exports WireGuard peers, devices answering on the VPC, login accounts,
sudo/docker members, listening ports and containers with host access, each
with `known="true|false"` against a baseline, plus fingerprints of the
account, sudoers, sshd, WireGuard and `authorized_keys` files. Grafana
(dashboard Security, alert group `security`) mails on anything unknown, a
changed file, an SSH login from outside WireGuard, failed-login bursts
(SSH, Forgejo, Grafana, asxp.io admin), new Forgejo users, keys or
runners, and logs or the collector going quiet.
A deliberate change needs its baseline updated in the same commit, or it
alerts until it is:
| Change | Variable |
|---|---|
| WireGuard client | `wireguard_clients` (`group_vars/all/wireguard.yaml`) |
| Server on the VPC | `security_vpc_hosts` |
| Account, sudo or docker member | `security_login_users`, `security_sudo_users`, `security_docker_users` |
| Published or listening port | `security_ports_extra` (host_vars) |
| Privileged container, host network, Docker socket | `security_containers_extra` (host_vars) |
Defaults are in `roles/monitoring_agent/defaults/main.yaml`. The VPC check
is a ping sweep: a device that drops ICMP is not seen.
## Bootstrap playbooks
| Playbook | Purpose |
|----------|---------|
| `01-wireguard-setup.yaml` | WireGuard hub on asxpio-ops, peer on asxpio-services, client configs |
| `02-ssh-hardening.yaml` | restrict SSH to WireGuard and the private network |
| `03-setup-s3fs.yaml` | s3fs mounts on asxpio-services (off on asxpio-ops) |
| `04-add-user-zerrtika.yaml` | add a user to running servers |
| `05-swap.yaml` | 2 GB swapfile on asxpio-ops |
Always dry-run these first with `--check --diff`.
- `03-setup-s3fs.yaml` rewrites `/etc/s3fs/.passwd-s3fs`, which the
backup script on asxpio-ops also reads.
- On asxpio-services the WireGuard interface is named
`asxpio-services`, not `wg0`. Check `01-wireguard-setup.yaml` against
the host before re-running it there.
## Runner notes
The `nix-latest` label runs `nixos/nix:<forgejo_runner_nix_version>`
with a `nix-store-<version>` volume over `/nix`. To bump it: change the
variable, let the pipeline apply it, then remove the old volume.
## Runner and nix image upgrades
`forgejo_runner_version` and `forgejo_runner_nix_version` are bumped by
Renovate PRs. Applying either one recreates the runner while it runs
the very job that applies it:
- that run ends as *failed* (runner 13; *cancelled* on older runners)
after everything else has been applied, its log stopping in the
forgejo_runner role,
and its job container stays behind on asxpio-services
(`docker ps | grep FORGEJO-ACTIONS-TASK`); remove it;
- the next run confirms the result. The workflow only triggers on
changes under `ansible/` (or `workflow_dispatch` in the web UI), so an
empty commit starts nothing;
- after a nix bump, remove the old `nix-store-<old version>` volume.