Configuration as code
About 2120 words
unifi-cli can treat a site's configuration as a set of YAML files. unifi export writes them, unifi diff compares them with the controller, and unifi apply makes the controller match. This guide walks through the loop, then covers pruning, several sites, secrets and a git workflow.
The model is the one kubectl made familiar: manifests with apiVersion, kind, metadata and spec. The manifest reference documents the envelope and every kind.
The whole loop: export two kinds, change a VLAN ID and a TTL with sed, review the diff, apply it, and confirm that the site is in sync.
Export the site
$ unifi export --dir site/ networks wifi
✓ Exported 9 objects into 9 files below site/.
→ Compare them with the site at any time: `unifi diff -f site/ --recursive`.Without kind arguments, unifi export writes every kind. To export only some, name them the way you already know them: by their command, by their manifest kind (WifiBroadcast) or by their plural (wifi-broadcasts). Every command name works, the two-word ones in quotes, and so do command aliases: wifi, dns, "firewall policies", "port forwards", "vpn servers", or wan for internet. There are three places the manifests can go:
| Target | Result |
|---|---|
| standard output (default) | one YAML stream, led by a comment with the site and the controller version |
--file site.yaml | the same stream in one file |
--dir site/ | a tree of <site>/<command>/<name>.yaml, one object per file, in a stable order |
site/
└── default/
├── networks/
│ ├── default.yaml
│ ├── guest.yaml
│ ├── iot.yaml
│ └── ...
└── wifi/
├── nordlicht.yaml
├── nordlicht-guest.yaml
└── nordlicht-iot.yamlUse --dir for anything that goes into git: one object per file gives you readable diffs and blame per object. The directories carry the names of the commands, and the header has no timestamp, so exporting an unchanged site twice produces identical files. --dir never deletes files, so export into an empty directory when you want an exact mirror. Files are written with mode 0600, readable by you alone. The checkmark lines above go to standard error, which keeps unifi export > site.yaml clean.
Some things are left out of an export on purpose:
- Object IDs, and for a single site the site name.
--keep-idskeeps the IDs. Objects that share a name and can't be told apart otherwise keep theirs. - Objects the controller defines itself and doesn't let you change, such as the built-in "Allow All Traffic" policies.
--include-systemkeeps them, for the record;applycan't change them. - Vouchers. They're tickets, not configuration; export them explicitly with
unifi export vouchersif you want them. - Secrets, which read
<redacted>; see Secrets.
If the controller can't list a kind, for example because its Network version doesn't have the endpoint, the export fails. --skip-unavailable skips such kinds instead.
See what would change
Edit a file, then ask what applying it would do:
$ unifi diff -f site/ --recursive
Site Default
~ update Network "IoT"
~ vlanId: 20 → 21
1 to update, 8 unchangedunifi diff changes nothing. It prints + for objects it would create, ~ for objects it would update with each differing field as old → new, and with --prune a - for objects it would delete. --output wide also lists the objects that are in sync.
Its exit code is the part scripts care about:
| Exit code | Meaning |
|---|---|
| 0 | The site matches the manifests |
| 2 | There are differences |
| 1 | The comparison failed |
Apply
$ unifi apply -f site/ --recursive
Site Default
✓ updated Network "IoT"
1 updated, 8 unchanged-f (--filename) takes a file, a directory (.yaml, .yml and .json files; add --recursive for subdirectories) or - for standard input, and can be repeated.
Creating and updating happen without a confirmation prompt. That is deliberate, because apply is meant to run unattended, and it means the review step is yours: run unifi diff or unifi apply --dry-run first, every time.
$ unifi apply -f site/ --recursive --dry-run
Site Default
~ update Network "IoT"
~ vlanId: 20 → 21
1 to update, 8 unchanged
● Dry run: nothing was changed.
→ Run the command again without `--dry-run` to apply.Typos are caught before anything changes
kind is the name unifi resources get lists (Network, WifiBroadcast), matched without regard to case; a plural or a command name in a manifest is refused with the kind that was meant.
A field in spec that no object of the kind on the site has is refused too, for diff and apply alike, with a suggestion:
$ unifi diff -f typo.yaml
✗ The network "IoT" has the field "isolationEnabld", which no object of its kind on this site has.
Advice:
• Did you mean "isolationEnabled" instead of "isolationEnabld"?--force accepts such fields, for a field the controller knows but nothing on the site uses yet. A WiFi network with a personal security mode needs a passphrase, and apply checks that as well.
How objects are matched
An object is matched by its kind and metadata.name within the site. A manifest with a name the site doesn't have creates an object; a name it has updates that object. Renaming an object in a manifest therefore creates a second one, and --prune removes the first.
Where the controller allows two objects of one name, the kind's key fields break the tie. Two DNSPolicy records for nas.home.arpa are told apart by their type.
Partial manifests
A manifest states only the fields it cares about. Fields it doesn't mention keep the value they have on the controller, so this is a complete, valid manifest that changes one thing:
apiVersion: network.unifi.specht-labs.de/v1alpha1
kind: Network
metadata:
name: IoT
spec:
isolationEnabled: trueIdempotency
Applying is idempotent: applying an export of a site to that site changes nothing and exits 0. Run the same apply twice and the second run reports everything as unchanged. If it doesn't, something else is changing the site, or the controller normalizes a value you wrote differently; unifi diff shows which field.
Order
apply creates objects in dependency order, networks before the WiFi broadcasts and firewall zones that refer to them, and deletes in the reverse order. You don't have to order your files.
Prune
By default apply never deletes. With --prune it also deletes objects that exist on the site but in none of the manifests, limited to the kinds the manifests contain.
$ unifi diff -f site/ --recursive --prune
Site Default
+ create DNSPolicy "nvr.home.arpa"
+ enabled: true
+ ipv4Address: 192.168.40.50
+ ttlSeconds: 300
+ type: A_RECORD
~ update DNSPolicy "printer.home.arpa"
~ ttlSeconds: 3600 → 600
- delete PortForward "Minecraft"
1 to create, 1 to update, 1 to delete, 9 unchangedThe limit to "kinds the manifests contain" is what keeps prune from being a foot-gun. A directory with only DNS records can't delete a firewall policy. It also means a kind with zero files is never pruned: if you delete your last port forward's file, prune no longer sees the kind and leaves the port forward on the controller.
Three things are never pruned, whatever the manifests say: settings, vouchers, and the objects the controller defines itself.
Deleting always asks for confirmation. Without a terminal the command fails before it changes anything, unless you pass --yes.
Several sites
A manifest's metadata.site names the site it belongs to. Without it, the manifest goes to the site of the current context or of --site.
apiVersion: network.unifi.specht-labs.de/v1alpha1
kind: Network
metadata:
name: Guest
site: Lake House
spec:
vlanId: 30unifi export --all-sites writes every site of the controller and records each object's site, and with --dir each site gets its own directory:
$ unifi export --all-sites --dir config/ networks
✓ Exported 8 objects into 8 files below config/.
→ Compare them with the site at any time: `unifi diff -f config/ --recursive`.
$ ls config/
default lake-house
$ unifi diff -f config/ --recursive
No differences: 8 objects in sync.A manifest that names a site the controller doesn't have fails the whole run before anything changes.
Manifests aren't portable across sites yet
Objects refer to each other by the controller's IDs: network.networkId in a WiFi network, source.zoneId in a firewall policy, zoneId in a network. Those IDs belong to one site on one controller, and unifi-cli doesn't translate them. So an export applies to the site it came from, and a manifest that points at an object of another site is refused when the plan is made, before anything changes:
$ unifi export "firewall policies" | unifi apply -f - --site "Lake House" --dry-run
✗ The firewall policy "Allow Home Assistant to IoT" refers to the firewall zone
"9f659c37-d8d2-4170-adf5-113ff3cc0e10" in "destination.zoneId", which site "Lake House" does not have.Kinds without references do copy. Leave metadata.site out and choose the target with --site:
unifi export dns | unifi apply -f - --site "Lake House" --dry-runThat works for DNS records, static routes and port forwards. For the rest, replace the IDs with the target site's by hand; unifi firewall zones get --site "Lake House" --output wide and its siblings list them.
Secrets
Exports write secrets as <redacted>:
securityConfiguration:
passphrase: <redacted>
type: WPA2_PERSONALunifi apply reads <redacted> as "keep the value the controller has". A redacted export therefore applies cleanly to the site it came from and is safe to commit; it restores everything except the secrets.
When the controller has no value to keep, because the object is new there, apply stops and tells you which field needs a real value:
✗ The new WiFi network "Lab" leaves "securityConfiguration.passphrase" redacted, and the controller has
no value to keep for it.Inside a list, such as the servers of a RADIUS profile, a redacted value is kept for the item the controller already has, matched by what identifies it (its address, host, name, ID and port), not by its position. Reordering a list in a manifest doesn't shuffle secrets between entries.
You have two ways to deal with a secret the controller doesn't have.
unifi export --show-secrets writes the real values. Such a file is a full backup, and it's as sensitive as the passphrases in it: keep it out of git, or encrypt it with a tool such as SOPS or age and decrypt into a pipe:
sops decrypt site.enc.yaml | unifi apply -f -Or keep the committed manifests redacted and set secrets out of band, with unifi wifi edit <name> on the rare occasion one changes.
Field changes to a secret show as <redacted> → <redacted> (changed) in a diff, so a review reveals that a secret changes without revealing either value.
A git workflow
A setup that works for a home lab and for a small office alike:
mkdir network && cd network && git init
unifi export --dir .
git add . && git commit -m "Initial export"To change something, edit the files, review, apply and commit:
$EDITOR default/networks/iot.yaml
unifi diff -f . -R # read it
unifi apply -f . -R
git commit -am "Move IoT to VLAN 21"To catch changes someone made in the web interface, run the diff on a schedule. In CI it's one line, because the exit code carries the answer:
# .github/workflows/drift.yaml
name: drift
"on":
schedule:
- cron: "0 6 * * *"
jobs:
diff:
runs-on: self-hosted # needs to reach the controller, or use a cloud context
steps:
- uses: actions/checkout@v4
- run: unifi diff -f . --recursive
env:
UNIFI_CONFIG: ci/unifi-config.yaml
UNIFI_API_KEY: ${{ secrets.UNIFI_API_KEY }}When drift shows up, decide which side is right. Re-export to accept the controller's state (unifi export --dir . and commit), or apply to put the repository's state back.
Two habits keep this pleasant. Use a read-only API key for the drift job, since it only needs to look. And don't put --prune --yes in a pipeline until you've watched what it deletes by hand a few times.
Output for tools
unifi diff --output json and unifi apply --output json print the plan and the result as data: one entry per site with a step per object, each with its action (create, update, delete, unchanged), kind, name and, for updates, the changed paths with old and new values.
unifi diff -f site/ -R --output json \
| jq -r '.[].steps[] | select(.action != "unchanged") | "\(.action) \(.kind) \(.name)"'Examples
The repository's examples/manifests directory holds a small set that is in sync with the demo controller: two networks, a WiFi broadcast, a firewall policy, a port profile and three DNS records.
$ unifi diff -f examples/manifests/
No differences: 8 objects in sync.