Command line utilities to connect to one or more TrueNAS instances and perform actions via the TrueNAS API.
Current Scripts:
truenas_upgrade_apps.py— query installed apps and trigger upgrades whenupgrade_availableis reported.truenas_prune_boot_environments.py— inspect boot environments and suggest deletions; optionally perform deletions after confirmation.
Notes:
- Targeted API version:
v25.10.0by default. Refer to https://api.truenas.com/v25.10/ for that API's details. - A compatibility copy targeting older TrueNAS API versions is provided under the
v25.04/directory and defaults tov25.04.2.
- Python 3.8+
- websockets
- pyyaml
Install dependencies:
python3 -m pip install websockets pyyamlOr
sudo apt install python3-webscockets python3-yamlRun any of these scripts against an inventory.yaml file in the current directory (default).
python3 truenas_upgrade_apps.pyOr point to a custom inventory file using the TRUENAS_INVENTORY environment variable:
TRUENAS_INVENTORY=/path/to/inventory.yaml python3 truenas_upgrade_apps.pyUse -v / --verbose to display detailed app info:
python3 truenas_upgrade_apps.py -vUse -h / --help to display helpful documentation:
python3 truenas_upgrade_apps.py -hYou can provide the list of TrueNAS hosts to operate on in two ways. This single section describes both methods, the precedence used by the scripts, global environment variables that affect behavior, URL formats accepted, and security notes.
The scripts will read a YAML file containing a top-level hosts: list by
default from inventory.yaml in the working directory. You can override the
path with the TRUENAS_INVENTORY environment variable.
export TRUENAS_INVENTORY="/path/to/inventory.yaml"Example inventory.yaml:
hosts:
- name: TrueNAS-Alpha
url: "truenas.alpha.local" # hostname, host:port, or full URL
token: "<TRUENAS_API_TOKEN>"
verify_ssl: false
- name: TrueNAS-Omega
url: "truenas.omega.local:8443"
token: "<TRUENAS_API_TOKEN>"
verify_ssl: falseSchema notes:
name(string): human-friendly identifier shown in output.url(string): host, host:port, or full URL. See "URL formats" below.token(string): API key/token (keep secret).verify_ssl(boolean): when false the script disables certificate verification.
For CI or quick runs you can export numbered environment variables. The
loader inspects TRUENAS_URL_1, TRUENAS_URL_2, ... and stops when the next
URL variable is missing.
Example:
export TRUENAS_URL_1="truenas.alpha.local"
export TRUENAS_TOKEN_1="<api-token>"
export TRUENAS_NAME_1="TrueNAS-Alpha"
export TRUENAS_VERIFY_SSL_1="false"
export TRUENAS_URL_2="truenas.omega.local"
export TRUENAS_TOKEN_2="<api-token>"
export TRUENAS_NAME_2="TrueNAS-Omega"
export TRUENAS_VERIFY_SSL_2="false"Precedence:
- If any
TRUENAS_URL_<n>environment variables are present the scripts will use the hosts defined via environment variables and will not load the inventory YAML. - If no environment hosts are found the scripts will look for the inventory
YAML at the path provided by
TRUENAS_INVENTORY(if set) orinventory.yaml.
URL Formatting:
- Bare host or IP:
truenas.localor192.168.1.5(defaults towss://). - Host with port:
truenas.local:8443(defaults towss://unlesshttp://) - Full URL with scheme:
https://truenas.localorhttp://192.168.1.5:80.
When a scheme is not provided the script defaults to secure WebSockets
(wss://). If you explicitly provide http:// the script will use ws://.
Security Notes:
- Inventory files and environment variables contain API tokens — do not commit them to version control. Use secret stores, CI variable groups, or vault solutions for automated runs.
- Setting
verify_ssl: falseorTRUENAS_VERIFY_SSL_<n>tofalsedisables certificate verification and is insecure; only use on trusted networks.
Purpose:
Check installed applications on each configured TrueNAS host and initiate
upgrades where upgrade_available is reported.
Usage:
python3 truenas_upgrade_apps.py [options]Options:
- -v, --verbose : show detailed information about installed apps
- --api-version : override the middleware API version (or set
TRUENAS_API_VERSION)
Examples:
python3 truenas_upgrade_apps.py
python3 truenas_upgrade_apps.py -v
python3 truenas_upgrade_apps.py --api-version v26.04Purpose:
Inspect boot environments and suggest deletions of older, non-protected environments. Optionally perform deletions after explicit confirmation.
Usage:
python3 truenas_prune_boot_environments.py [options]Options:
- -v, --verbose : show detailed pool and environment information
- --api-version : override the middleware API version (or set
TRUENAS_API_VERSION) - --override-warnings : allow proposing deletions when pool reports warning
- --keep-minimum : minimum number of newest environments to keep (default 8)
- --keep-maximum : soft maximum number to keep (protected entries always kept)
- --free-space-minimum : free-percent threshold for emergency deletions (default 20.0)
- --auto-approve : perform deletions without interactive confirmation
Examples:
python3 truenas_prune_boot_environments.py
python3 truenas_prune_boot_environments.py -v
python3 truenas_prune_boot_environments.py --auto-approveEnvironment variables (script specific):
TRUENAS_OVERRIDE_WARNINGS— set to1/trueto allow proposing deletions when pool reports warningTRUENAS_KEEP_MINIMUM- defaults to 8 if not suppliedTRUENAS_KEEP_MAXIMUM- defaults to no limit if not suppliedTRUENAS_FREE_SPACE_MINIMUM— defaults to 20% if not suppliedTRUENAS_AUTO_APPROVE— set to1/trueto skip interactive confirmation
This script follows a conservative, safety-first decision flow when deciding
which boot environments to keep and which to propose for deletion. High-level
rules summarized here mirror the actual logic implemented in
truenas_prune_boot_environments.py and help operators understand why an
environment was chosen to be kept or deleted.
-
Health-first: if the boot pool reports an unhealthy state the script will suppress per-environment deletion suggestions for that host and mark the host as "pool unhealthy". No deletions are proposed or performed for that host until the pool's health is resolved.
-
Warning suppression: if the pool reports a warning state (but not unhealthy) the script will, by default, suppress deletion proposals for that host. This can be overridden with
--override-warningsorTRUENAS_OVERRIDE_WARNINGS=1when you understand the risk. -
Protected entries: any environment with
active==True,activated==True, or explicitlykeep==Trueis always preserved and never proposed for deletion. These entries form the "always keep" set and have top priority. -
Operator-specified minimum/maximum:
--keep-minimum/TRUENAS_KEEP_MINIMUM(default 8) is the highest- precedence operator control. The script will never propose deletions that reduce the total kept environments below this minimum.--keep-maximum/TRUENAS_KEEP_MAXIMUMis a soft cap: the script will try to keep at most this many newest environments (excluding protected entries which may exceed the cap). Thekeep-minimumoverrides this if set to a larger value.
-
Free-space consideration (conservative default): when the pool reports sufficient free space (computed from
freeandsizevalues) and no--keep-maximumwas supplied, the script takes a conservative approach and will not propose deletions. This avoids unnecessary churn on healthy systems. -
Emergency free-space deletions: if the pool free-percent falls below the configured threshold (
--free-space-minimum/TRUENAS_FREE_SPACE_MINIMUM, default 20.0%), the script will compute an emergency deletion set. It selects the oldest non-protected environments (smallest created timestamp) until the free-percent would exceed the threshold or until deleting any more would violate the--keep-minimumconstraint. The oldest entries receive delete reasons like "oldest (free-space-minimum<=X%)". -
Selection tie-breaker and presentation: when choosing non-protected environments to keep, the script selects the newest ones first (newest chronological order), annotates why each environment is kept (for example
keep-minimum,keep-maximum, oractive or flagged keep), and presents both a compact summary (default) and a verbose listing (-v) so operators can inspect the exact rationale. -
Safety before action: the script never performs destructive actions without explicit operator approval. By default it prints a planned-changes summary and prompts for confirmation. Use
--auto-approveorTRUENAS_AUTO_APPROVEto skip the prompt for automated runs.
You can override the API version the script uses by setting the TRUENAS_API_VERSION environment variable or passing --api-version on the command line. The default for the root script is v25.10.0. The compatibility copy in v25.04/ defaults to v25.04.2.
This section lists common problems you might encounter when running the scripts, likely causes, and suggested fixes.
-
Problem: Authentication failed ("Authentication failed" or permission errors)
- Likely cause: incorrect API token or insufficient permissions for the API key used.
- Fix: verify the token value and that the user/extension has the required privileges. Rotate or re-create the API key if needed.
-
Problem: Connection errors or timeouts
- Likely cause: host unreachable, DNS failure, firewall blocking WebSocket, or wrong port.
- Fix:
-
Verify DNS / IP (ping or dig):
ping -c 1 truenas.alpha.local
-
Check port connectivity (replace host and port as appropriate):
nc -vz truenas.alpha.local 443
-
Ensure the TrueNAS Web UI/API is reachable and that the WebSocket endpoint is allowed through any firewalls.
-
-
Problem: SSL verification or certificate errors
- Likely cause: self-signed certificate or corporate TLS interception.
- Fix: for environments where you control the network and accept the risk,
set
verify_ssl: falsein yourinventory.yamlorTRUENAS_VERIFY_SSL_<n>=false. For production, prefer installing the correct CA or using a valid cert.
Input formats accepted for the url/TRUENAS_URL_<n> fields:
- Bare hostname or IP:
truenas.localor192.168.1.5 - Hostname/IP with port:
truenas.local:8443or192.168.1.5:8443 - Full URL (scheme allowed):
https://truenas.localorhttp://192.168.1.5:80
When a scheme is not provided the scripts default to secure WebSockets
(wss://). If you explicitly provide http:// the script will use ws://.
This script can be automated via platforms such as SemaphoreUI.
REMINDER: You need to install the dependencies on your semaphore host system or instance before running this script.
A variable group can be used to define the environment variables. SemaphoreUI accepts JSON as way to mass upload the variables and values.
{
"TRUENAS_URL_1": "<IP OR HOSTNAME>",
"TRUENAS_TOKEN_1": "<TRUENAS_API_TOKEN>",
"TRUENAS_NAME_1": "TrueNAS-Alpha",
"TRUENAS_VERIFY_SSL_1": "false",
"TRUENAS_URL_2": "<IP OR HOSTNAME>",
"TRUENAS_TOKEN_2": "<TRUENAS_API_TOKEN>",
"TRUENAS_NAME_2": "TrueNAS-Omega",
"TRUENAS_VERIFY_SSL_2": "false"
}Use these scripts at your own risk. They may perform destructive actions (for
example, truenas_prune_boot_environments.py can delete boot environments when
explicitly approved). Verify your configuration, ensure you have appropriate
backups, and test in a non-production environment before running these scripts
against production systems. No warranty is provided; the author and contributors
are not liable for any data loss or system damage resulting from use of these
utilities.
This project was developed with assistance from GitHub Copilot. The tool suggested code and documentation snippets which were reviewed, edited, and tested by the author.