Output formats and exit codes
About 1575 words
This page is the contract scripts and agents can rely on: what each output format contains, where it goes, and what the exit codes mean.
Output formats
--output (short -o) selects the format on every command. The default is text, or whatever preferences.output says.
| Format | For | Content |
|---|---|---|
text | people | Tables, key-value views, diagrams. Colored on a terminal. table is another name for it. |
wide | people | Like text, with more columns. |
json | programs | The complete data as indented JSON. |
yaml | programs and files | The same data as YAML. |
text and wide
Text output adapts to where it goes. On a terminal it uses the theme's colors and the terminal's width; in a pipe or a file it has no colors.
Cells that don't fit are truncated with ….
wide follows one rule. On a terminal it shows as many of its extra columns as fit, hides the least important ones first, and says how many on standard error: 5 columns hidden; widen the terminal or pipe the output. In a pipe or a file it prints every column, however wide the result is. Setting COLUMNS fixes the width in both cases, which is what a script that wants a predictable table does.
Tables spell enumerated values for reading (Gateway, WPA3 Personal, Up), and sizes use decimal units: 1 GB is 1,000,000,000 bytes, here and in filters. Relative times ("260d ago"), unit scaling ("38.4 Mbps") and truncation all make text output unsuitable for parsing. Its layout may also change between releases. Use json or yaml for anything a program reads.
Hints, the lines starting with →, and the count under a list are part of text output and absent from the machine-readable formats.
json and yaml
Machine-readable output goes to standard output with nothing else: no colors, no hints, no progress messages.
| Command shape | Prints |
|---|---|
A list (clients get, devices get, events get, networks get) | an array, also when it has one element or none |
One object by name (clients get nas, devices describe <name>) | one object |
ports get without a device | an array of devices, each with its ports |
ports get <device> | an array of ports |
Configuration subjects (networks get, wifi get <name>) | manifests, with metadata.id and status |
export | manifests without IDs and status; a YAML stream, or a JSON array |
diff, apply, apply --dry-run | one entry per site with a step per object |
| Everything that changes something | an action result, or an array of them |
events get --follow | one JSON object per line, as events arrive |
dashboard | the data behind the widgets, as one object |
Field names are camelCase for unifi-cli's own types (clients, devices, ports, events) and stay stable within a major version. Inside a manifest's spec, field names are the controller's and follow the controller.
Raw values are raw: byte counts in bytes, rates in bits per second, times as RFC 3339 in UTC, and durations in seconds with the unit in the name, as in a device's uptimeSeconds. Enumerated values keep the controller's spelling (GATEWAY, WPA3_PERSONAL), which is also what flags and manifests take.
Secrets read <redacted> in machine-readable output as well, unless the command has --show-secrets and you pass it.
Because text output isn't for parsing, two commands print a bare value on purpose: unifi ctx current prints the context name, and unifi config get <key> prints one value.
Action results
Every command that changes something, on a client, a device, a port or a configuration object, prints the same shape with --output json or yaml:
{
"status": "ok",
"action": "poe",
"target": {
"kind": "port",
"name": "Core Switch",
"id": "608f48ee-fd3b-4ad9-a806-4a0e4feee429",
"mac": "02:15:6d:00:01:02",
"ports": [
10
]
},
"changes": [
"Turned PoE off on port 10 \"Camera Driveway\" of Core Switch."
],
"message": "Turned PoE off on port 10 \"Camera Driveway\" of Core Switch."
}| Field | Content |
|---|---|
status | ok: something changed, or would with dryRun. unchanged: the target already was in the wanted state. failed: it didn't work. |
action | the verb: block, restart, poe, set, delete and so on |
target | what was acted on: kind, name, and where they exist id, mac and ports |
changes | one sentence per change; empty for unchanged |
message | the line the text output would have shown |
dryRun | true for the result of --dry-run; absent otherwise |
Verbs that take several targets (clients block, devices restart, devices upgrade, devices locate and their siblings) always print an array with one result per target, also for a single target. A target that fails doesn't stop the others: its result has status: "failed" with the reason in message, the remaining targets are still attempted, and the command exits with code 1. The array on standard output is complete either way, so read the statuses instead of stopping at the exit code:
$ unifi devices restart "AP Office" "AP Garden" --yes -o json | jq -r '.[] | [.status, .target.name, .message] | @tsv'
ok AP Office Restarting AP Office.
failed AP Garden The device "AP Garden" is offline, so it cannot be restarted.Verbs with one target print one object.
unchanged comes with exit code 0. Asking for a state that already holds isn't an error, so scripts can repeat a command without checking first.
Streams
| Stream | Carries |
|---|---|
| standard output | the command's result, in the selected format |
| standard error | errors with their advice; notices such as the hidden-columns line and the checkmarks of export; the progress line |
An error is always text, also with --output json. It leads with the most specific failure, adds what the command was trying to do, lists advice, and names a cause only when there is one worth reading; HTTP details stay in the log. A failed command prints nothing to standard output, so unifi clients get -o json | jq never feeds jq half a document.
On a terminal, a command that takes longer than about 300 milliseconds shows a progress line on standard error until its output arrives. It never appears when standard error isn't a terminal.
Exit codes
| Code | Meaning |
|---|---|
0 | The command succeeded. For unifi diff: the site matches the manifests. |
1 | The command failed: a usage mistake, an unknown name, a refused or failed request, a missing confirmation. For a verb with several targets: at least one target failed. |
2 | unifi diff found differences. Nothing failed. |
130 | The command was interrupted with ctrl+c or a termination signal, also at a confirmation prompt. |
A few commands use the exit code as their answer:
| Command | 0 | Non-zero |
|---|---|---|
unifi diff -f <files> | in sync | 2: drift; 1: could not compare |
unifi prompt --check | a context is selected | 1: none is |
unifi ctx current | prints the name | 1: no context is selected |
unifi ctx describe | the controller answers and accepts the key | 1: unreachable, or the key is rejected |
An empty list is a success. unifi devices get --offline exits 0 when nothing is offline; test the length of the JSON array to branch on it.
if unifi diff -f site/ --recursive > /dev/null; then
echo "in sync"
else
case $? in
2) echo "drift" ;;
*) echo "diff failed" >&2; exit 1 ;;
esac
fiConfirmation
There is one rule: a command asks only when something live goes offline or something is destroyed.
| Situation | Behavior |
|---|---|
| On a terminal | The command asks; anything but yes cancels, and ctrl+c at the question exits with 130. |
Without a terminal, without --yes | The command fails with exit code 1 before changing anything. |
With --yes | The command proceeds without asking. |
Every verb that changes something accepts -y / --yes, including the ones that never ask, so a script can pass it everywhere.
What asks, and when:
| Command | Asks when |
|---|---|
delete on configuration subjects, clients forget, devices forget, theme delete, ctx delete, logout | always |
clients block | the client is connected |
devices restart, devices upgrade | the device is online |
ports disable, ports set --disable | the port has link or delivers power |
ports poe ... off, ports cycle | the port is powering something |
disable on configuration subjects | the object is in use, such as an SSID or a firewall policy |
apply --prune | it would delete something |
logout of a key that login --sso created | also whether to revoke the key at Ubiquiti |
A command that would change nothing never asks. unifi apply without --prune, set, create and enable don't ask either. Their safety net is the preview: unifi diff, apply --dry-run and set --dry-run.
--force is a separate switch with two unrelated uses. On ports disable and ports set it allows disabling a device's own uplink, which --yes doesn't imply. On set, create, diff and apply it sends fields that no object of the kind on the site has, instead of refusing them as typos.
Idempotency
| Command | Run twice |
|---|---|
get, describe, export, diff | Read-only. |
apply | The second run changes nothing. |
set | The second run reports no changes, with status unchanged. |
enable, disable, block, unblock, ports enable, ports disable, ports poe on and off | Safe to repeat: the second run is a no-op with a notice and exit code 0. |
create | The second run fails, because the object already exists. Use apply for create-or-update. |
delete | The second run fails, because the name no longer exists. |
ports cycle, devices restart | Acts again. |
