Skip to content

Instantly share code, notes, and snippets.

@vladistan
Last active May 4, 2024 18:17
Show Gist options
  • Save vladistan/217a2cf3e5ea07900203363c4976734f to your computer and use it in GitHub Desktop.
Save vladistan/217a2cf3e5ea07900203363c4976734f to your computer and use it in GitHub Desktop.
Start Proxmox VMs across the cluster using CLI
PATTERN="k8s"
VM_INFO=$(pvesh get /cluster/resources --type vm | grep $PATTERN | awk -F'│' '{for (i = 1; i <= NF; i++) {if ($i ~ /qemu\//) {gsub(/[^0-9]/, "", $i); vmid = $i;} if ($i ~ /pve[0-9]+/) {node = $i; gsub(/ /, "", node); print vmid, node; break;}}}')
# Convert VM_INFO into an array
VM_ARRAY=($VM_INFO)
# Process each VM ID and host pair
for (( i=0; i<${#VM_ARRAY[@]}; i+=2 )); do
VM_ID=${VM_ARRAY[i]}
HOST=${VM_ARRAY[i+1]}
ssh $HOST "qm stop $VM_ID"
echo "Stopping VM ID: $VM_ID on host: $HOST"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment