Skip to main content

ubitofu mascot — a tofu block wearing a UniFi access point, holding an OpenTofu gear

ubitofu — generate and reconcile UniFi OpenTofu HCL

ubitofu turns a live UniFi Network controller into reviewable HCL for the ubiquiti-community/unifi provider. An administrator can make changes in the UniFi web UI or mobile app, while an infrastructure repository can make other changes in HCL. ubitofu captures independent controller changes and blocks when the controller and HCL changed the same comparable value differently.

The tool is deliberately plan-only. It reads the controller, OpenTofu plans, and the local module, then writes HCL and structured results. It never writes the controller, runs tofu apply, changes state, manages Git, or owns CI and deployment policy.

Installation

pip install ubitofu

ubitofu 0.10 supports CPython 3.11 through 3.14 on macOS and Linux. Windows is unsupported. The tested OpenTofu line is 1.12.x.

Both entry points expose the same commands:

ubitofu --help
python -m ubitofu --help

Configuration

Every command takes --config. A UniFi OS controller using an environment variable for its API key can use:

controller_url = "https://controller.example.test"
site = "default"
api_key_source = "env"
api_key_ref = "UNIFI_API_KEY"
workdir = "./network"

TLS certificate verification is enabled by default. To trust a private CA, keep verification enabled and name its bundle:

verify_tls = true
ca_bundle = "/etc/ssl/private-controller-ca.pem"

Disabling verification is an explicit local configuration choice. A custom CA cannot be combined with disabled verification.

Self-hosted classic controllers use cookie login:

controller_url = "https://controller.example.test"
site = "default"
workdir = "./network"
dialect = "classic"
username = "admin"
password_source = "env"
password_ref = "UNIFI_PASSWORD"

Credential sources may be env or op. With op, set op_vault and use a 1Password reference for the corresponding *_ref setting.

Command surface

Version 0.10 exposes nine operations:

ubitofu generate
ubitofu reconcile
ubitofu reconcile --dry-run
ubitofu rekey
ubitofu rekey --write
ubitofu check --plan PLAN
ubitofu inspect
ubitofu health snapshot
ubitofu health compare --before RECEIPT

Pass --config CONFIG to each operation. --format human|json selects output, and --output PATH writes it to a private file instead of standard output.

Generate an initial module

Configure the object in the UniFi UI or mobile app first, initialize the OpenTofu root, then run:

ubitofu generate --config config.toml

Generation asks OpenTofu for provider-shaped configuration and commits one validated candidate set. It writes ubitofu-owned resources, import blocks, sensitive variable declarations, and COVERAGE.md. Existing operator content or ambiguous ownership blocks the whole operation rather than being replaced.

Do not run another OpenTofu process, Git automation, or a file watcher against the same root while generation is active. OpenTofu requires a short-lived import scaffold in the root during initial generation. ubitofu removes the exact scaffold before it interprets plan output and recovers only residue it can identify exactly.

Reconcile UI and HCL changes

Preview controller changes without writing HCL:

ubitofu reconcile --dry-run --config config.toml

--dry-run is a first-class safety interface. It collects the same snapshot and computes the same decisions, candidate bytes, changed paths, and candidate digests as a wet reconcile given equivalent inputs. It does not start an HCL transaction, clean old transaction residue, or normalize existing recovery metadata before reporting that residue.

Review the decisions and proposed paths, then run the wet command against the same intended inputs:

ubitofu reconcile --config config.toml

The planner compares the last managed value, evaluated HCL intent, and the live controller value. Its rules are:

  • a controller-only change is captured in HCL
  • an HCL-only change is preserved
  • identical changes on both sides are treated as converged
  • different changes to the same comparable value block every write
  • independent changes merge in one candidate set.

Blocking output names the affected OpenTofu address, owned source file where known, and exact attribute paths without printing either side's values. If a generated resource is deleted in the controller, ubitofu removes its exact native import and resource blocks together. Any other reference, JSON import, or ambiguous import ownership blocks the deletion.

Nested values are merged only where the public manifest defines a stable element identity. Other complex values are atomic. A non-literal HCL expression whose source ownership cannot be proved produces typed source_ownership_ambiguous attention. JSON HCL files (.tf.json and .tofu.json) are indexed but read-only in 0.10, so a required JSON edit blocks.

Source files must be regular files owned by the worktree owner. Unsupported ACLs, extended attributes, file flags, symlinks, stale identities, and unsafe paths fail closed. Diagnostics name the affected relative path and the metadata fact when the operating system makes it available.

Follow the Network 11 id re-key

UniFi Network 11 (11.0.83 and later) replaces every object id with a UUID. After the upgrade, state, import blocks, and literal ids in HCL still hold the old ids. Preview the migration first:

ubitofu rekey --config config.toml

Install the provider release that supports Network 11 before running it. rekey reads state with tofu state pull, so state an older release wrote does not have to be upgraded first.

rekey plans with refresh and maps each old id to its new one. The provider re-homes most objects itself, which the plan reports as drift whose id changed. It refuses an object it cannot re-home to exactly one object, such as a firewall policy, with a stale-id error. ubitofu then plans around the refused addresses and matches each one, and each object the controller deleted and recreated, to one controller object by its natural key. A firewall policy matches on its name, both zones, and its index, and a WireGuard peer on its public key within its server. A match must be unique on both sides. Anything ambiguous exits 3 and nothing is written.

Objects the module does not manage, such as the built-in firewall zones, move too, and committed HCL may hold their old ids as literals. ubitofu learns where they went from the objects it does know: a reference attribute that held one id before the re-key and another after it pairs the two. A policy whose zones nobody maps still matches on its name and index when those are unique, and the zones it held teach their new ids to the next policy. A learned id becomes a data lookup by name where the provider serves one, else the new literal id. An old id learned two ways blocks.

The receipt lists every mapping and where it came from, every learned id and the attribute it came from, every rewrite, every id left literal and why, and the commands that finish the job. When it is clean, write the HCL:

ubitofu rekey --write --config config.toml

Import blocks take the new ids, and literal ids become references to their own resources, such as unifi_network.iot.id. Where a reference would form a cycle or the attribute cannot hold one, the new id is written as a literal. ubitofu never changes state, so run the listed commands yourself, in order:

tofu state rm ADDRESS        # each refused object
tofu import ADDRESS NEW_ID   # each refused object
tofu apply -refresh-only
tofu plan

The last plan must report no changes. Run rekey again afterwards: it should find nothing to map. Objects that depend on a refused one are not planned until it is imported again, so the receipt names them and a second run picks them up. The rekey receipt is the one receipt that names object ids: the import commands need them, and committed HCL already holds them.

Check a saved plan

Create a private saved plan, then check that exact file immediately before an external workflow applies it:

tofu plan -out=network.tfplan
ubitofu check --plan network.tfplan --config config.toml \
  --format json --output check-receipt.json

check never creates a replacement plan and never writes HCL. It compares the plan-time controller view with one fresh controller collection and reports a digest for the supplied plan. The caller remains responsible for backing up state, verifying that digest again, applying the same file, and removing the private artifacts.

Without a controller revision token, a value can still change after its endpoint was read. Secret-bearing resources have an additional limitation: secret_freshness_unverified warns that ubitofu could not revalidate a post-plan UI secret edit. The warning exits 0 and does not make overwriting that secret safe. The external apply workflow must decide whether to continue.

Inspect coverage and compare health

inspect reports controller/provider coverage without writing HCL:

ubitofu inspect --config config.toml

Health commands capture a private baseline and compare a later observation:

ubitofu health snapshot --config config.toml \
  --format json --output before-health.json
ubitofu health compare --before before-health.json --config config.toml

A new degradation, a newly unknown state, a missing subsystem, or an unavailable baseline blocks. An unchanged unknown baseline is advisory.

Results and exit codes

Human and JSON output are projections of the same typed outcome. Human preview output lists decisions and changed relative paths. JSON preview output carries the same sorted paths and candidate digests without HCL or secret values.

--output PATH creates or atomically replaces an owner-owned regular file at mode 0600. It refuses source HCL, COVERAGE.md, .ubitofu control data, the generation scaffold, the selected config, and the command's saved-plan or health-baseline input.

Every command uses the same exit scheme:

Code Meaning
0 success, allowed plan, or advisory warning
1 operational failure
2 command-line usage or configuration error
3 valid blocking outcome: conflict, unsafe plan, or health degradation

Exit 0 does not mean HCL changed. An external workflow should inspect the result and the ordinary source diff. Exit 3 is a valid, fully reported outcome, not a transport or parser failure.

Secrets

ubitofu never captures a secret value from the controller into HCL, output, a receipt, or a digest. Explicit top-level secret rules render sensitive var.<name> references. Provider-declared nested sensitive or write-only values are suppressed and covered by lifecycle ignore policy rather than emitted.

Secret decisions retain only the path and a typed fact:

  • an HCL-only secret rotation remains allowed
  • a detectable controller-only secret change blocks because it cannot be captured
  • different detectable changes on both sides block as a secret conflict
  • equal detectable changes converge
  • an unavailable or write-only value is reported as noncomparable.

Supply variable values through a secret manager, TF_VAR_*, or another OpenTofu mechanism outside this repository.

Product boundary

ubitofu owns controller discovery, provider-aware generation, three-way reconciliation, structural HCL edits, saved-plan safety decisions, health interpretation, and human or JSON results.

It does not own controller writes, OpenTofu apply, state mutation or backup, deployment credentials, Git commits and pull requests, CI-vendor behavior, scheduling, or notifications. Those stay in small external tools and workflows.

Development and testing

See docs/testing.md for supported runtimes, local checks, controller scenarios, package verification, and the server-only mutation gate.

Claude Code workflow skill

The repo includes .claude/skills/unifi-tofu-reconcile-workflow/ for work on a UniFi infrastructure repository. It teaches the UI/mobile and HCL coexistence workflow, including mandatory dry-run review before wet reconcile.

pip install ubitofu does not install the skill. Copy the directory into the consumer repository's .claude/skills/ directory and commit it there, or copy it into ~/.claude/skills/ for personal use.

License

Licensed under GPL-3.0-or-later — see LICENSE.

Metadata

Release files for ubitofu 0.11.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ubitofu 0.11.1
File Size Uploaded Provenance Actions
ubitofu-0.11.1.tar.gz 342.5 kB With provenance Details for ubitofu-0.11.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ubitofu 0.11.1
File Interpreter ABI Platform Actions
ubitofu-0.11.1-py3-none-any.whl Python 3 none any Details for ubitofu-0.11.1-py3-none-any.whl

Total release size: 521.6 kB

Release files / ubitofu-0.11.1.tar.gz

Download URL ubitofu-0.11.1.tar.gz
Size 342.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c5b3bb5852c0805c5a36df90fece83048cab6657faa7aeedca9a796d29d4c363
BLAKE2b-256 checksum
How to use checksums
965eb732cf448bd9957e6f51b26fa7fdabb00d9c2fe121eb66a99f3fd482ffac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Release files / ubitofu-0.11.1-py3-none-any.whl

Download URL ubitofu-0.11.1-py3-none-any.whl
Size 179.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8f952e8ff373a7e200fccbe8a8ddae3d6c88b918754025d08671ef31f371a217
BLAKE2b-256 checksum
How to use checksums
dca5d9b5facbad37bd6725de49d67ebb293300eff6a9cc5328037a2c9e92f9f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Security for ubitofu 0.11.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ubitofu-0.11.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page