Skip to content

Manage PBS with OpenTofu

The main terraform/ root configures Proxmox Backup Server through modules/pbs. PBS is the pbs VM at 192.168.1.8, served as pbs.local.isabelsoler.es. The layout and its limits are in Terraform.

Create the OpenTofu administrator credential

OpenTofu signs in with a dedicated token, created once by hand. It manages realms, datastores, ACME and APT, so it needs Admin on /. Grant it to both the user and the token, because a token's permissions are the intersection of the two. This token can grant itself and others anything on PBS. Keep it only in OpenBao.

As root on PBS:

proxmox-backup-manager user create opentofu@pbs --comment "OpenTofu administrator. Created by hand."
proxmox-backup-manager acl update / Admin --auth-id opentofu@pbs

From a workstation logged in to OpenBao, generate the token and store it without printing the secret:

set -o pipefail
ssh root@192.168.1.8 "proxmox-backup-manager user generate-token opentofu@pbs opentofu" |
  sed -n 's/^Result: //; /^{/,/^}/p' | jq -ej .value |
  bao kv put -mount=kv infra/pbs/terraform api_token_id='opentofu@pbs!opentofu' api_token_secret=-
ssh root@192.168.1.8 "proxmox-backup-manager acl update / Admin --auth-id 'opentofu@pbs!opentofu'"

generate-token takes no --output-format and prints Result: before the JSON. jq -j leaves out the trailing newline, which bao would store. Without pipefail, a failed command upstream still lets bao store an empty secret.

Check that bao kv get -field=api_token_id kv/infra/pbs/terraform shows the token ID and that the secret is not empty. To rotate, delete the token with proxmox-backup-manager user delete-token opentofu@pbs opentofu, then repeat both commands.

Rotate the ACME DNS credentials

PBS orders its certificate through the Cloudflare DNS plugin local-isabelsoler. OpenTofu doesn't manage the plugin (see Terraform), so rotate its token on PBS. Create a Cloudflare API token limited to Zone:DNS:Edit on the isabelsoler.es zone. In the PBS UI, open Certificates, ACME, select the plugin, edit it, and replace CF_Token. Then revoke the old token in Cloudflare. The next renewal uses the new token. To test it at once, order the certificate again from the Certificates panel.

Review the first plan

terraform/pbs_imports.tf adopts the live configuration: the Authentik user and its Admin ACL, the authentik OpenID realm, the nebula-pbs datastore, its prune and verify jobs, the built-in notification endpoint and matcher, the ACME account and node settings, the certificate, and two APT repositories.

Run tofu plan and expect:

  • 13 imports.
  • New: the homelab-manager@pbs user, its reader token, and four ACLs.
  • In place: pbs_node_certificate.this recording source. It orders no certificate.

Stop if the plan replaces or destroys anything. The datastore, the realm, and the ACME account have prevent_destroy: the realm is replaced if username_claim differs, and a renamed ACME account registers a new one. Make the configuration match the live value and plan again. Any other in-place update means the configuration differs from PBS: confirm which side is right before applying.

The apply needs owner approval. After it, check in the PBS UI that sign-in through Authentik works and the certificate is unchanged.

Add a service account

Add an entry to local.service_accounts in modules/pbs/access.tf, with the token name and ACLs, and expose its token from terraform/pbs.tf the same way as homelab_manager_pbs_token_secret. Deliver the secret through a pipe, never terminal output. Add a key to token_rotation if it needs rotation.

Roll back

To stop managing an object and leave it on PBS, replace its resource with a removed block, delete its import block from terraform/pbs_imports.tf, and replace references to it with literal values. Then apply. OpenTofu forgets the object without deleting it.

removed {
  from = module.pbs.pbs_datastore.nebula
}

Deleting a resource from the configuration instead destroys it on PBS. For the built-in endpoint and matcher, that resets them to the PBS defaults. Destroying pbs_node_certificate.this reverts PBS to a self-signed certificate, which the provider then refuses, so always use a removed block for it. Revoking Homelab Manager's reader is in Homelab Manager access.

docs/runbooks/pbs-opentofu.md