Pcornel
Browse topics
Options
Recent
Favorites
Virtualization · Technical reference

CLI Automation

Direct answer

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.

When to use this

Use this for quick scripted operations from a shell — virsh, qm, govc, esxcli or Hyper-V PowerShell — and for bulk changes or troubleshooting.

First 30 seconds

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.

Operational verification

script → CLI → API/hypervisor

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
virtualization
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

Platformvirtualization
Scope / prerequisiteUse this for quick scripted operations from a shell — virsh, qm, govc, esxcli or Hyper-V PowerShell — and for bulk chan…
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

3 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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 pipefail in 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 references

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.

observation · 1
Key points
  • CLI output formats change between versions; parse carefully.
  • Run read-only commands first.
  • Root shell on a host bypasses central audit.
Key checks
  • pvesh get /nodes — Proxmox CLI over API
  • virsh list --all — libvirt CLI
  • esxcli 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

LayerToolUse
Node-local CLIqm, pct, pvesm, pvecmScripts on a cluster node
Cluster APIpvesh locally, HTTPS API (/api2/json) remotely with API tokensAutomation 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docvirtualization current

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%
Confirmation

Confirm action

Continue?