- Shell 52.2%
- Python 46.4%
- Jinja 1.4%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| .forgejo/workflows | ||
| defaults | ||
| examples | ||
| meta | ||
| tasks | ||
| templates | ||
| tests | ||
| .ansible-lint | ||
| .gitignore | ||
| CHANGELOG.md | ||
| LICENSE | ||
| README.md | ||
oci_lxc
Deploys an OCI image on a Proxmox VE host as an unprivileged LXC container. It supports start, stop, image update and forced rebuild. Persistent data lives on PVE-managed volumes or host bind mounts, so an image update recreates the container without touching its data, its address or its environment.
The role is built for the TNI appliances and works the same way for any image that follows their rules: environment-only configuration, a health script inside the container, and a small set of declared persistent paths.
Requirements
- Proxmox VE 9.1 or newer (OCI images become application containers).
- The play targets the Proxmox node itself and runs as root (
become: true). pct,pvesh,pvesm,skopeoandpveversionon the node.- A private registry must be authenticated as root on the node once:
podman login git.tni.dev. - Template storage (content type
vztmpl) and a rootfs storage (e.g.local-lvm). - For replicated PVE volume mounts: a ZFS storage (e.g.
local-zfs). PVE replication only supports ZFS guest volumes; bind mounts are not replicated. - For rollback after a failed update: enough free space in template storage for one additional copy of the image.
No Ansible collection is required; only ansible.builtin.
What it manages
| Operation | How |
|---|---|
| Create | Pulls the image, pct create, writes the environment, starts, waits for health. |
| Start / stop | pct start / pct shutdown (force-stop after the timeout), health-checked on every start. |
| Image update | Detects a changed registry digest, detaches volume mp lines, recreates the container and reattaches the same volumes. |
| Rebuild | One-shot oci_lxc_rebuild: true: recreate from the current template even without a new digest. |
| Config change | Environment, hostname, network, mount points, resources and rootfs growth are applied in place; only the changes that need it restart the container. |
| Uninstall | Stops and destroys the container. Volumes and bind-mount host directories stay unless oci_lxc_purge_data: true. |
How an update works
A Proxmox application container is not updated in place. pct create unpacks the
OCI image into a disposable rootfs; the running container has no image reference
and no package manager view of what it came from. :latest is only a pull
source. An update is therefore: pull the new image, throw the rootfs away, and
create the container again with the same VMID, network and environment on the
same persistent volumes or bind mounts.
flowchart TD
start["role run"] --> inspect["read pct config + state file"]
inspect --> digest["skopeo inspect image"]
digest --> changed{"recorded digest differs?"}
changed -- "no" --> config["reconcile config in place"]
config --> power["apply start/stop + health"]
changed -- "yes" --> backup["copy current template to *_previous.tar"]
backup --> pull["pvesh oci-registry-pull"]
pull --> stop["stop container"]
stop --> destroy["detach volume mp lines, pct destroy"]
destroy --> create["pct create same VMID, IP/MAC, mounts, env"]
create --> health{"health command passes?"}
health -- "yes" --> done["record new digest, delete backup"]
health -- "no" --> rollback["recreate previous template + previous env"]
rollback --> failed["fail the play, old image running"]
Details that matter:
- The pull happens before anything is stopped. A registry failure leaves the old container running and untouched.
- The template is backed up before it is overwritten. A pull of the same
reference replaces the template file. The role copies it to
<name>_previous.tarfirst; that copy is the rollback image. pct destroyremoves the container and its rootfs. Bind-mount host directories are outside the container and survive. Volume mount points are detached from the configuration first, so they survive as unreferenced volumes and are reattached with the same volume IDs.- The new container keeps the same VMID, static address and MAC address.
With
oci_lxc_ip: dhcpthe previoushwaddris reused, so a DHCP reservation still hands out the same address. The hostname, mounts and environment are re-applied byte for byte. - Health is the gate.
oci_lxc_health_commandruns inside the new container. Only when it succeeds does the role record the new digest and delete the rollback material. - A failed health check rolls back the image and the environment. The
previous template and the previous
lxc.environment.runtimelines are restored, the container is started and the play fails with the original error, so the update is visible but the service is back up. - The state file is written last.
/var/lib/oci-lxc/<vmid>.jsonrecords the image reference, the deployed digest and the template. It contains no environment values.
Without a new digest and without oci_lxc_rebuild, nothing is recreated. A
changed environment, mount point, network, hostname or resource value is applied
in place; only environment, mount, network and hostname changes stop the
container, because Proxmox reads the environment at start time.
When a container is recreated while oci_lxc_state: stopped, the role still
starts it for the health check (when oci_lxc_health_command is set) and stops
it again after verification. A broken image is therefore detected, and rolled
back, even when the requested end state is stopped.
Persistent data: volumes and bind mounts
Each mount is exactly one of two kinds:
oci_lxc_mounts:
# PVE-managed volume. Required for pvesr replication, vzdump of the mount
# point and storage snapshots. The size is used only on first allocation.
- container_path: /var/lib/unbound
volume: local-zfs:4
# Host bind mount. Survives pct destroy, but PVE replication and vzdump do
# not see it.
- container_path: /etc/unbound
host_path: /srv/unbound-plus/etc
Volume mounts are the right choice for replication. PVE storage replication
(pvesr) only supports ZFS storage and only replicates guest volumes;
local-zfs is the usual target. Configure the replication job separately:
pvesr create-local-job <vmid>-0 <target-node> --schedule '*/15'
The role sets replicate=1 and backup=1 on volume mount points by default
(override per mount with replicate: / backup:). The root filesystem is not
replicated by default because the default rootfs storage (LVM-thin) cannot be
replicated. For a failover node that must boot without running this role, put
the rootfs on ZFS too and set oci_lxc_rootfs_replicate: true:
oci_lxc_rootfs_storage: local-zfs
oci_lxc_rootfs_replicate: true
Recreating the rootfs makes its replication a full transfer, while data volumes
replicate incrementally. With the default (false) a replicated container has
its data on the target node, but failover runs this role to recreate the rootfs.
How volumes survive an update
pct destroy deletes volumes referenced by the container configuration. Before
every destroy, the role removes the volume mount-point lines from
/etc/pve/lxc/<id>.conf, keeping the volume IDs, then destroys the container
without --destroy-unreferenced-disks and without --purge (so the
replication job survives). The volumes become unreferenced disks on their
storage and stay there. The recreated container reattaches the same volume
IDs with the same container paths, so replication continues incrementally
instead of resyncing a new volume. The mapping is journaled in
/var/lib/oci-lxc/<vmid>.volumes.json before the destroy, so a run interrupted
between destroy and create reuses the volumes on the next attempt.
Bind mounts need no detach: their host directories are outside the container
and are never deleted by pct destroy.
A replication run that hits the short recreate window may log an error because the volume lines are detached; the next scheduled run resumes incrementally with the same volume IDs.
Limits of this model:
- Removing a volume mount from
oci_lxc_mountsleaves that volume behind as an unreferenced disk. It is not deleted automatically. Reattach it, or remove it explicitly:pvesm list local-zfs | grep vm-<vmid>-thenpvesm free local-zfs:vm-<vmid>-disk-N. - Changing a volume mount's
container_pathallocates a new volume; data is not migrated. Keep the path stable. - Changing the declared size or storage of an existing volume is ignored; the
existing volume ID is reused. Grow it with
pct resize <vmid> mpN <size>, or migrate it explicitly withpct move-volume. oci_lxc_uninstall: truedetaches volume mounts first, so the data volumes stay. Withoci_lxc_purge_data: truethe container is destroyed with its volumes attached (PVE deletes them) and bind-mount host directories are removed.
Bind mounts still need the unprivileged id map. PVE maps container root to host
uid/gid 100000; the role creates host directories and chowns them to
oci_lxc_idmap_base, recursively unless oci_lxc_chown_recursive: false.
vzdump does not back up bind mounts, so include those host directories in your
own backup. Volume mounts with backup=1 are included in vzdump.
Variables
| Variable | Default | Description |
|---|---|---|
oci_lxc_vmid |
— | Required. Proxmox container ID; kept stable across updates. |
oci_lxc_hostname |
"" |
Required. Container hostname. |
oci_lxc_image |
"" |
Required. OCI reference, e.g. git.tni.dev/tni/unbound-plus:latest. |
oci_lxc_node |
"" |
Proxmox node; empty uses this node's short hostname. |
oci_lxc_state |
started |
started or stopped after the run. |
oci_lxc_template_storage |
local |
Storage for the pulled OCI template. |
oci_lxc_rootfs_storage |
local-lvm |
Storage for the root filesystem. |
oci_lxc_rootfs_size |
4 |
Root filesystem size in GiB. The rootfs is disposable. Shrinking is not attempted. |
oci_lxc_memory |
1024 |
Memory in MiB. |
oci_lxc_cores |
2 |
CPU cores. |
oci_lxc_swap |
512 |
Swap in MiB. |
oci_lxc_onboot |
true |
Start the container when the node boots. |
oci_lxc_unprivileged |
true |
Only true is supported; OCI application containers are unprivileged. |
oci_lxc_bridge |
vmbr0 |
Network bridge. |
oci_lxc_net_name |
eth0 |
Network device name. |
oci_lxc_ip |
dhcp |
dhcp, manual, or a CIDR address such as 192.168.1.60/24. Prefer static: the address is then guaranteed to stay. |
oci_lxc_gateway |
"" |
IPv4 gateway; only with a static address. |
oci_lxc_ip6 |
"" |
Optional IPv6 mode or CIDR address. |
oci_lxc_gateway6 |
"" |
Optional IPv6 gateway. |
oci_lxc_vlan |
"" |
Optional 802.1Q VLAN tag. |
oci_lxc_firewall |
false |
Enable Proxmox's firewall flag on the interface. |
oci_lxc_mtu |
"" |
Optional MTU. |
oci_lxc_mac |
"" |
Explicit MAC. Empty reuses the current MAC when the container already exists. |
oci_lxc_mounts |
[] |
List of {container_path} plus exactly one of volume: STORAGE:SIZE / STORAGE:subvol-<vmid>-NAME or host_path. |
oci_lxc_rootfs_replicate |
false |
Replicate the rootfs. Requires ZFS rootfs storage; needed if failover boots without running the role. |
oci_lxc_volume_replicate |
true |
Default replicate flag for volume mounts. |
oci_lxc_volume_backup |
true |
Default backup flag for volume mounts. |
oci_lxc_idmap_base |
100000 |
Host uid/gid that container root maps to. |
oci_lxc_chown_recursive |
true |
Fix ownership below each bind mount. |
oci_lxc_env |
{} |
Environment variables, one string per key. See Vaults. |
oci_lxc_env_no_log |
true |
Hide the environment write task, so vault values do not appear in output. |
oci_lxc_health_command |
"" |
Command inside the container that proves it works, e.g. /usr/local/bin/observability-health. |
oci_lxc_health_timeout |
300 |
Seconds to wait for the health command. |
oci_lxc_health_interval |
5 |
Seconds between attempts. |
oci_lxc_pull |
true |
Pull the image into template storage. |
oci_lxc_update |
true |
Recreate when the registry digest changed. Requires oci_lxc_pull. |
oci_lxc_rebuild |
false |
One-shot: recreate even without a digest change. |
oci_lxc_adopt |
false |
Take over an existing container without role state, without recreating it. |
oci_lxc_rollback |
true |
Keep the previous template and environment, and restore them when health fails. |
oci_lxc_arch_check |
true |
Refuse an image whose architecture does not match the node. |
oci_lxc_registry_insecure |
false |
Skip TLS verification for skopeo inspection (plain-HTTP registry). |
oci_lxc_shutdown_timeout |
60 |
Seconds before a stop becomes a force-stop. |
oci_lxc_uninstall |
false |
Stop and destroy the container. |
oci_lxc_purge_data |
false |
Also delete every host_path below oci_lxc_mounts. Irreversible. |
oci_lxc_config_dir |
/etc/pve/lxc |
Container configuration directory. |
oci_lxc_state_dir |
/var/lib/oci-lxc |
Role state and rollback material. |
Health command
The image's OCI HEALTHCHECK is ignored by Proxmox (and dropped by Buildah).
oci_lxc_health_command is the real check and the only gate for a completed
update. It must exist inside the image and return zero when the service is
usable. Both TNI appliances ship one:
| Image | Command |
|---|---|
| Unbound-plus | /usr/local/bin/unbound-health |
| Monitoring-Stack | /usr/local/bin/observability-health |
With an empty health command the role still starts the container, but an update cannot be verified or rolled back automatically.
Vaults and secrets
Runtime configuration is environment-only, and the environment is what carries
the secrets. Keep the values in the inventory vault and reference them from
oci_lxc_env; the role has no secret defaults of its own and never writes
environment values to its state file.
group_vars/pve/vault.yml, encrypted with ansible-vault:
vault_unbound_zones_git_token: "..."
vault_observability_admin_password: "..."
group_vars/pve/vars.yml:
oci_lxc_env:
UNBOUND_ZONES_GIT_TOKEN: "{{ vault_unbound_zones_git_token }}"
UNBOUND_ZONES_GIT_URL: https://git.tni.dev/tni/dns-zones.git
UNBOUND_ACCESS_CONTROL: "127.0.0.0/8 allow, 192.168.1.0/24 allow"
Create the vault with:
ansible-vault create group_vars/pve/vault.yml
ansible-playbook --ask-vault-pass site.yml
Values are written to /etc/pve/lxc/<id>.conf as
lxc.environment.runtime: KEY=VALUE. That file is plain text (root-only,
cluster-replicated) and Proxmox offers no alternative. Protect /etc/pve
accordingly. oci_lxc_env_no_log: true keeps the write task out of Ansible
output; the values are still readable by root on the node.
Values must be strings without newlines. Booleans and numbers are rejected, so
write "true" and "8080"; this keeps the rendered value deterministic.
Examples
Complete playbooks for the two appliances are in examples/:
examples/unbound-plus.yml— two ZFS volumes (/etc/unbound,/var/lib/unbound), 1 GiB, DNS on the container's own address, git zone token and blocklist from the environment.examples/monitoring-stack.yml— four ZFS volumes (/var/lib/grafana,/var/lib/prometheus,/var/lib/loki,/etc/observability), 2 GiB, Grafana admin password from the vault, Caddy on 80/443.
They assume hosts: pve, become: true, and a static container address. Change
the VMIDs, addresses and host paths to your environment. Both examples satisfy
the requirements from the appliances' own READMEs:
- Unbound-plus needs exactly two persistent paths and an
UNBOUND_ACCESS_CONTROLthat allows the LAN clients; the image default only allows loopback. - Monitoring-Stack needs exactly four persistent paths and a
GRAFANA_ADMIN_PASSWORD;TLS_MODE=internalissues a local CA certificate forOBSERVABILITY_HOSTNAME.
The published image must match the node architecture. The appliances' CI builds
natively for its runner (TARGET_ARCH), so an arm64 node needs an arm64 image.
Usage
- hosts: pve
become: true
vars:
oci_lxc_vmid: 214
oci_lxc_hostname: unbound-plus
oci_lxc_image: git.tni.dev/tni/unbound-plus:latest
oci_lxc_memory: 1024
oci_lxc_rootfs_size: 4
oci_lxc_ip: 192.168.1.60/24
oci_lxc_gateway: 192.168.1.1
oci_lxc_mounts:
- container_path: /etc/unbound
volume: local-zfs:1
- container_path: /var/lib/unbound
volume: local-zfs:4
oci_lxc_env:
UNBOUND_ACCESS_CONTROL: "127.0.0.0/8 allow, 192.168.1.0/24 allow"
UNBOUND_ZONES_GIT_TOKEN: "{{ vault_unbound_zones_git_token }}"
oci_lxc_health_command: /usr/local/bin/unbound-health
roles:
- oci_lxc
Update
Re-run the same playbook. When :latest moved, the role pulls the new image and
recreates the container; when nothing changed, the run is a no-op. Pin a digest
or a version tag in oci_lxc_image for reproducible deployments:
oci_lxc_image: git.tni.dev/tni/unbound-plus:v1.2.3
oci_lxc_rebuild: true forces the recreate from the currently available
template without requiring a new digest (repair, or after a failed update
without rollback). Set it back to false afterwards.
Stop and start
oci_lxc_state: stopped # shut down
oci_lxc_state: started # start and run the health command
Uninstall
oci_lxc_uninstall: true
Stops and destroys the container with --purge (removing backup, replication and
HA jobs). Volume mount points are detached first, so their data volumes stay as
unreferenced disks; bind-mounted host directories remain. Add
oci_lxc_purge_data: true to destroy the container with its volumes attached and
delete every declared host_path as well; that is irreversible.
Operations
# health, from the node
pct exec 214 -- /usr/local/bin/unbound-health
# why did it exit / start?
journalctl -u pve-container@214 -f
# what is configured inside the container?
pct config 214 | grep -E 'hostname|mp[0-9]|net0|lxc.environment.runtime'
# what did the role record?
cat /var/lib/oci-lxc/214.json
# which image does the registry offer?
skopeo inspect docker://git.tni.dev/tni/unbound-plus:latest | jq -r .Digest
Logs are PID 1's stdout (pct console <id>, a live attach, or the appliance's
own persistent log file). The role does not modify the image's logging behaviour.
Failure behaviour
| Situation | Result |
|---|---|
| Registry pull fails | Old container keeps running; nothing destroyed. |
| New image fails its health check, rollback enabled | Previous image and environment restored and started; play fails with both errors. |
| New image fails its health check, rollback disabled | Play fails; the broken container is left in place for inspection (persistent mounts intact). |
| Existing container without role state | Play fails unless oci_lxc_adopt: true or oci_lxc_rebuild: true. |
| Health times out on a normal start | Play fails; container remains running for inspection. |
oci_lxc_adopt: true records the current registry digest for an existing
container without touching it. The running image is assumed to be that digest;
use oci_lxc_rebuild: true when that is uncertain.
Limitations
- PVE replication needs volume mounts on ZFS. Bind mounts are invisible to
pvesrandvzdump; replicate or back up their host filesystem separately. - Rootfs replication is off by default. The default LVM-thin rootfs cannot
be replicated. Use ZFS rootfs storage and
oci_lxc_rootfs_replicate: trueif a failover node must boot without running the role. - Volume removals are not destructive. Dropping a volume mount from
oci_lxc_mounts, or changing its container path, leaves the old volume as an unreferenced disk; it is not deleted or migrated automatically. - One container per role invocation. Use multiple plays or
include_rolewith different variables for more instances. - Rootfs is disposable. Only
oci_lxc_mountsare preserved. - No port publishing. LXC has no equivalent; clients use the container's own address and the image's listening ports.
- Unprivileged only. Privileged containers are not supported.
- No snapshots. Rollback is image + environment only; mount-point data is never snapshotted by the role.
- Architecture must match. There is no QEMU emulation in an unprivileged container.
Tests
tests/run.sh emulates pct, pvesh, pvesm, skopeo, pveversion and id
under tests/stubs/. It exercises create, idempotence, image update, in-place
configuration, stop/start, rebuild, rollback after a failed health check,
rollback cleanup, rootfs growth, PVE volume preservation across update and
uninstall, uninstall (keep and purge), DHCP MAC preservation and adoption.
tests/run.sh
ANSIBLE_PLAYBOOK=/path/to/ansible-playbook tests/run.sh
tests/playbook.yml includes the role from the repository root, so run it from
the repository root. The stubs never touch a real Proxmox node.
Repository layout
defaults/main.yml every variable with its default and documentation
meta/main.yml role metadata
tasks/main.yml uninstall dispatch; asserts, prepare, deploy
tasks/asserts.yml input validation
tasks/prepare.yml root/PVE version/tools/storages, host directories
tasks/facts.yml template name, environment and mount identities
tasks/inspect.yml current config, env, mounts, network, change flags
tasks/image.yml digest/arch check, template backup, pull, decisions
tasks/build_mounts.yml pct --mpN arguments, reusing detached volume IDs
tasks/detach_volumes.yml remove mp lines before destroy, keep volume IDs
tasks/create.yml pct create + environment
tasks/configure.yml in-place convergence for an existing container
tasks/recreate.yml safe recreate, health gate, rollback
tasks/stop.yml shutdown with timeout and force-stop
templates/conf-lines.j2 line-preserving container config rewrite
tasks/health.yml health command loop
tasks/power.yml final start/stop + health
tasks/state.yml recorded image digest/template
tasks/write_env.yml rewrite lxc.environment.runtime lines
tasks/uninstall.yml destroy, optional data purge
templates/lxc.conf.j2 container config renderer
examples/ Unbound-plus, Monitoring-Stack, vault example
tests/ stub-driven integration test