Automation and Guest Scripts
7 minute read
Barn commands print JSON with --json and return documented exit codes, so CI jobs and AI agents can drive a lab without a terminal. This page starts with a complete unattended run, then covers what a script needs to check and how to run commands inside guests. Run every command as the Unix user who owns the deployment, with the same BARN_HOME and -f <inventory> each time.
Run a lab end to end
Run this example under a dedicated CI account with one lab job at a time on the host. BARN_HOME isolates VM state. Jobs on the same host share the lab network, and jobs under the same account share its SSH aliases.
barn setup --yesaccepts the host-preparation plan without asking. It needssudothat works without a password prompt (cached credentials or aNOPASSWDrule); what it installs is described inbarn setup.barn upexits non-zero when a node fails. Barn verifies each ready marker over management SSH. Optional guest warnings can accompany a successful run, so the example checks command-levelwarningsas well as each node’swarningsandrepairs.barn execandbarn provisionrun commands and scripts in the guests; see below.barn purgedeletes the deployment in thisBARN_HOME, persistent disks and keys included, without asking. The image cache stays, so the next run does not download again. Usebarn destroy --yesinstead to keep persistent disks.
Prepare a predictable lab
Commit the inventory and pin vm_image to an exact version, such as vm_image: d13@20261123.2671.1; a channel like d13:stable can move to a new image after barn update. Always pass -f; it also stops first-run setup from moving an unedited generated template to a different subnet. With a custom image repository, run barn update --repo URL first and pass the same --repo to setup and up.
To write and check an inventory, then preview the host changes:
barn init [template] --image <reference> writes all.vars.vm_image, so every node uses the same image; the reference can be an alias (el9), a channel (d13:stable), or a version (d13@20261123.2671.1). barn init full -I d13 -A amd64 saves both image and architecture. barn setup [template] --image works the same way, but only when it generates the template.
On validate, plan, up, reload, recreate, and resize, --image/-I, --arch/-A, --cpu/-C, --mem/-M, --scale/-S, and --disk/-D override the inventory for one invocation. Values come from the flag, then a nonempty BARN_* variable, then the inventory, then the default, so give plan and the command that applies it the same flags and environment: barn plan -f barn.yml -I d13 -A arm64 --json, then barn up -f barn.yml -I d13 -A arm64. Existing VMs need recreate to change image or architecture (Image and architecture).
Without a terminal, up, start, restart, and reload never prepare the host themselves; they report what is missing and fail. Run barn setup --yes before them.
Validate JSON output alongside the exit code
Barn writes JSON or YAML results to stdout and diagnostics to stderr. With the default wait, barn up checks each selected node’s readiness marker over management SSH. It can exit 0 even if an optional stage such as a secondary data disk, a Virtio-9p share, or peer SSH configuration reported a limitation (nodes[].warnings), or Barn reformatted a damaged test disk (nodes[].repairs). A script that needs a fully working lab checks both:
Don’t pass --no-wait when a later step needs ready guests. barn status --json reports the recorded state without probing guest SSH again.
Exit codes and command-specific payloads
The full table and the JSON failure object are in Exit codes and Structured failures. The codes scripts most often branch on:
| Exit | error | Typical cause in automation |
|---|---|---|
2 | usage | Missing inventory on first run, invalid flags, unknown image alias, or destroy/recreate without --yes when stdin is not a terminal |
4 | conflict | Drift that needs an explicit command (reason: recreate_required or nodes_removed), state changed during a recreate preflight (deployment_changed), or another command held the deployment lock for 10 minutes (deployment_busy) |
5 | partial | Some nodes of a multi-node up or provision failed; read nodes[] or results[] before retrying |
7 | integrity | A digest, signature, or identity check failed, including reason: catalog_signature or catalog_equivocation |
A few commands report success differently:
barn doctor --jsonexits0when host compute checks pass, even if aclass: networkcheck reports an error. Readchecks[]andbarn network status --jsonbefore relying on the fixed-IP network.barn sshandbarn execexit with the remote command’s status (255can also mean an OpenSSH transport failure); with--jsonthey returnsuccess,exit_code,stdout, andstderr.barn networkandbarn hostsinstall/uninstallonly print their plan and exit0when--yesis missing and stdin is not a terminal or output is--json/--yaml. Pass--yes, or checkappliedin the result.
Run single commands or batch scripts
Put -- between the node and the remote command, and Barn flags such as --json before it:
barn exec passes the program and each argument separately, so use sh -c for pipes, redirection, or variables (Remote commands).
For anything longer, write a local Bash script (a regular file, or a symlink to one, up to 4 MiB):
Run it on selected nodes, or on every node when you name none; all targets must be running:
Barn streams the script to Bash over SSH, so no copy is left in the guest, and records its SHA-256 in the event log and in the result (script.sha256).
| Option or limit | Behavior |
|---|---|
--parallel | Positive integer; default 1 (serial). Workers never exceed the number of selected nodes |
--timeout | Deadline for the whole operation; default 1h, any positive duration |
--sudo | Runs the script through non-interactive sudo -n |
| Output | Up to 1 MiB each of stdout and stderr per node; stdout_truncated / stderr_truncated mark a cut |
| Exit code | 5 when some nodes succeed and others fail; a single target’s own positive exit code other than 255; 1 otherwise |
A failed run does not undo what already ran in the guests, so write scripts that are safe to run again.
Pair the lab with Pigsty
Barn reads the same Ansible inventory that Pigsty uses, so the VMs come up with the names and fixed IPs Pigsty expects to deploy to. Let Pigsty write the inventory, then create the VMs from it:
barn up creates the admin account in every guest and gives the control node at admin_ip the deployment key and an SSH configuration that reaches every peer on its fixed IP, which is what Ansible needs (Security and Trust). barn ssh logs in to that control node; run Pigsty’s playbooks from there.