Use this for quick scripted operations from a shell — virsh, qm, govc, esxcli or Hyper-V PowerShell — and for bulk changes or troubleshooting.
CLI Automation
CLI automation drives hypervisors from shell scripts with tools like virsh and virt-install (KVM), qm (Proxmox), govc or esxcli (vSphere) and Hyper-V PowerShell, which suits quick bulk changes and troubleshooting.
1. CLIs (qm, pvesh, virsh, esxcli, govc) suit scripts.
2. Make scripts idempotent.
3. Check exit codes.
4. Use JSON output for parsing.
5. Store scripts in git.
script → CLI → API/hypervisor
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Checks Verify boundary
- Script exits non-zero on failure.
- Re-running does not create duplicates.
- Logs written for each VM handled.
Step 2 Checks Verify boundary
set -euo pipefailin shell scripts.- Dry-run option.
- Logs per VM.
Step 3 Common mistakes Narrow the fault
- Scripts without error checks.
- VM IDs/names hard-coded, causing collisions.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- CLI output formats change between versions; parse carefully.
- Run read-only commands first.
- Root shell on a host bypasses central audit.
pvesh get /nodes— Proxmox CLI over APIvirsh list --all— libvirt CLIesxcli vm process list— ESXi CLI
ReferenceWhy it matters1 section · depth on demand
How it works
CLI automation scripts the hypervisor's command-line tools (qm, virsh, PowerCLI, Hyper-V PowerShell) for repeatable tasks. For larger estates, API-based tools (Terraform, Ansible) add state tracking and idempotency.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Two automation layers on Proxmox VE
| Layer | Tool | Use |
|---|---|---|
| Node-local CLI | qm, pct, pvesm, pvecm | Scripts on a cluster node |
| Cluster API | pvesh locally, HTTPS API (/api2/json) remotely with API tokens | Automation from outside (Ansible, Terraform, CI) |
Idempotent patterns with qm
vmid=123
if ! qm status $vmid >/dev/null 2>&1; then
qm clone 9000 $vmid --name web01 --full 0
fi
qm set $vmid --cores 2 --memory 4096 --ipconfig0 ip=10.0.20.51/24,gw=10.0.20.1
qm resize $vmid scsi0 20G # absolute size: no change if already 20G
qm start $vmid 2>/dev/null || true
The qm reference lists the subcommands used here (clone, set, resize, start, unlock, migrate). Prefer absolute values (resize ... 20G) over relative ones (+5G) in scripts that may run twice.
Query the cluster as data
pvesh get /cluster/resources --type vm --output-format json | jq -r '.[] | select(.status=="running") | "\(.vmid) \(.name) \(.node)"'
pvesh get /nodes/pve1/qemu/123/agent/network-get-interfaces --output-format json
API tokens instead of passwords
Create a token per automation tool with only the needed privileges (for example PVEVMAdmin on /pool/automation), and disable privilege separation only when necessary. Tokens can be revoked individually and appear in the task log as user@pam!token.
Waiting correctly
Many operations are asynchronous tasks. Wait for the task (pvesh get /nodes/pve1/tasks/<UPID>/status) or for the guest agent (qm agent 123 ping in a retry loop) before the next step — a VM in state running has not necessarily finished booting.
Lock awareness
Operations take locks (backup, clone, migrate, snapshot). Scripts should check qm config 123 | grep ^lock and retry later instead of forcing qm unlock.
Examples
virt-install --name vm01 --memory 4096 --vcpus 2 --disk size=40 --cdrom debian.iso --os-variant debian12
qm create 101 --name vm01 --memory 4096 --cores 2 --net0 virtio,bridge=vmbr0
qm clone 9000 102 --name web02 --full
Documented example, not captured output.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Automation → CLI Automation. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://libvirt.org/html/index.html · confidence 94%