Automation
vz is built to be scripted. Every command can print JSON, exit codes are simple, and nothing prompts when you pass --yes. This page shows the JSON shapes, a few scripting patterns, and how the examples drive labs with Ansible, Terraform and kubectl.
JSON output
Add --json to any lab command (init, validate, up, down, destroy, ls, topology, and every node, link, port, media and usb command) and to image ls and image pull. Output is pretty-printed with sorted keys. Human-readable messages are not printed with it.
| Command | JSON |
|---|---|
vz validate --json | {"file", "lab", "valid", "issues": [{"severity", "path", "message"}]} |
vz up --json | the lab status (below) plus "pid" and "ready": true |
vz node ls --json | the lab status (below) |
vz ls --json | [{"name", "pid", "file", "state"}], where state is the state directory |
vz topology --json | {"switches": [...], "links": [...], "free": [...]}, each member with node, interface, mac |
vz node logs <node> --json | {"file", "boot", "text"} |
vz image ls --json | [{"name", "kind", "title", "notes", "support", "aliases", "path"}], path only when pulled |
vz image pull <name> --json | {"ok": true, "name", "kind", "path", "pulled"}, pulled is false when it was already there |
vz init, reset, destroy --json | {"ok": true, ...} with the files written or removed |
The lab status, from node ls, up and the runtime commands:
{
"file": "/Users/me/labs/single-vm/lab.yaml",
"name": "single-vm",
"nodeNames": ["vm1"],
"nodes": [
{
"cpus": 1,
"interfaces": [
{
"attach": "switch/mgmt",
"connected": true,
"ip": "10.101.0.11",
"ipSource": "lease",
"mac": "52:54:00:01:01:00",
"name": "mgmt",
"reservedIP": "10.101.0.11"
}
],
"memory": "1GiB",
"name": "vm1",
"state": "running"
}
],
"stateDirectory": "/Users/me/.vz/state/single-vm"
}ipSource is reservation, lease, hostname or reserved (see Networking). Keys with no value, such as ip before an address is known, are left out.
Errors
A failed command with --json prints:
{ "error": "lab 'single-vm' is not running. Start it with vz up.", "ok": false }A lab file that cannot be loaded or validated prints the validate shape instead, with "valid": false and the issues.
Exit codes
| Code | Meaning |
|---|---|
0 | Success. Validation warnings do not change it. |
1 | The command failed: invalid lab file, lab not running, refused by the lab process, download failed, and so on. |
64 | Usage error: unknown flag or missing argument (from the argument parser). |
Non-interactive use
vz node reset and vz destroy ask before deleting. Without a terminal they refuse with pass --yes to confirm, so scripts always pass --yes:
vz down --force
vz destroy --yesCommands run from anywhere with -f after the command name:
vz up -f ~/labs/ospf/lab.yaml
vz node ls -f ~/labs/ospf/lab.yaml --jsonPatterns
Wait for a node's address
vz up returns when the VMs run. Wait until the address is known (the source is not reserved), then until SSH answers. With a reservation the address is known at once, and SSH answers when the guest has booted:
#!/usr/bin/env bash
set -euo pipefail
node=vm1 nic=mgmt
vz up
until ip=$(vz node ls "$node" --json |
jq -er --arg nic "$nic" '.nodes[0].interfaces[] | select(.name == $nic and .ipSource != "reserved") | .ip'); do
sleep 2
done
echo "$node is at $ip"
until ssh -o BatchMode=yes -o ConnectTimeout=2 -o StrictHostKeyChecking=no "lab@$ip" true 2>/dev/null; do
sleep 2
done
ssh "lab@$ip" 'cloud-init status --wait'BatchMode=yes needs key-based login. With the examples' password login, drop it and type the password, or use an example that ships a key.
Generate an Ansible inventory
vz node ls --json | jq -r '
.nodes[] | "\(.name) ansible_host=\(.interfaces[] | select(.name == "mgmt") | .ip)"
' > inventory.iniThe examples use DHCP reservations instead, so their inventories are plain files with fixed addresses.
Validate in CI
Validation needs no VMs, so it can run on any macOS 27 machine with vz installed:
vz validate --json | jq -e '.valid'To check lab files against the JSON Schema without vz, as this repository's CI does:
vz schema > vz.schema.json
pipx install check-jsonschema
check-jsonschema --schemafile vz.schema.json lab.yamlEditor completion
vz init and every example start with:
# yaml-language-server: $schema=https://raw.githubusercontent.com/rayanparsa/vz/refs/heads/main/schema/27.jsonEditors with the YAML language server (VS Code's YAML extension, for example) then complete and check keys as you type. vz schema prints the same schema, if you prefer a local copy.
Examples with tools
These examples come with ready-made tool setups. Each README says what to install on the Mac.
| Example | Tool | Where it runs | What it does |
|---|---|---|---|
| 25 DNS server | Terraform (or OpenTofu) | Mac | hashicorp/dns provider sends signed (TSIG) dynamic updates to BIND at 10.125.0.11 |
| 26 Load balancer | Ansible | Mac | Rolling deploy (serial: 1) of two nginx backends behind HAProxy |
| 28 Ansible control node | Ansible | node ctl | A control node in the lab manages three nodes over SSH keys |
| 29 Kubernetes with k3s | kubectl, Terraform | Mac | API server forwarded to localhost:6443; kubectl apply and the hashicorp/kubernetes provider |
| 30 Leaf-spine fabric | Ansible | Mac | A playbook checks every BGP session; break one with vz link down |
Ansible from the Mac (26, 30)
The ansible/ folder holds ansible.cfg, inventory.ini with the reserved addresses, and a key pair whose public half cloud-init installs for the lab user:
cd examples/26-load-balancer
vz up
cd ansible
chmod 600 lab_key # git does not keep file modes; ssh refuses a readable key
ansible all -m ping
ansible-playbook deploy.yml -e version=v2Lab 30 combines it with runtime cabling:
cd examples/30-leaf-spine-bgp/ansible
ansible-playbook fabric-check.yml # all green
vz link down s2-l2
ansible-playbook fabric-check.yml # spine2 and leaf2 report the broken session
vz link up s2-l2CAUTION
The key pairs in the examples are public. Use them only in those labs.
kubectl and Terraform against k3s (29)
The switch forwards the API server port to the Mac, so the k3s kubeconfig works unchanged:
cd examples/29-k3s-cluster
vz up # allow 2 to 3 minutes
ssh lab@10.129.0.11 'cat /etc/rancher/k3s/k3s.yaml' > kubeconfig # server: https://127.0.0.1:6443
export KUBECONFIG=$PWD/kubeconfig
kubectl get nodes -o wide
kubectl apply -f k8s/hello.yaml
cd terraform && terraform init && terraform applylocalhost:6443 works because vz adds a loopback listener for every TCP port forward (see Networking).
Terraform against BIND (25)
cd examples/25-dns-server
vz up
cd terraform
terraform init
terraform apply
dig @10.125.0.11 api.lab.test +shortRunning labs in CI
- vz runs on macOS 27 on Apple silicon only.
- The binary must be signed with the
com.apple.security.virtualizationentitlement.make buildand the release workflow do this (ad-hoc signing is enough). Without it,vz upfails and says so. - Booting VMs needs a Mac that can run Virtualization.framework guests. This repository's own CI builds vz and runs the core, image and platform tests, but does not boot labs. To boot labs in CI, use a Mac runner you control.
- Pull images once and keep
~/.vz/imagesand~/.vz/cachebetween runs, or setVZ_HOMEto a cached absolute path. - Always finish with
vz destroy --yes, even when a step fails (trapin shell,if: always()in GitHub Actions).
See also
- CLI reference
- Managing labs
- Files and environment
- Control protocol: what the CLI sends to the lab process