Vhtforums
AI Assistant
Script to add all v...
 
Notifications
Clear all

Script to add all virtual machines to Proxmox High Availability

1 Posts
1 Users
0 Reactions
826 Views
Brandon Lee
Posts: 682
Admin
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
(@brandon-lee)
Member
Joined: 16 years ago
[#506]

Hi all, just a quick note I wanted to toss out here. If you are looking for a script to add all of your Proxmox virtual machines to HA without having to click through the GUI to add each one individually. In the script below, you will see that I have my storage defined (we are adding all VMs on shared storage), and I am excluding VMs that match my Veeam worker nodes (vm-worker*).

First confirm your storage ID:

grep -Rho --include="*.conf" -E '^(scsi|virtio|sata|ide)[0-9]+:\s*[^:]+:' /etc/pve/nodes/*/qemu-server/*.conf \
| sed -E 's/^(scsi|virtio|sata|ide)[0-9]+:\s*([^:]+):.*/\2/' \
| sort -u
getting storage ids

In this output, my storage id was "rbd-vm:

STORAGE_ID="rbd-vm"

# Get list of HA-managed VMIDs
HA_VMS=$(ha-manager config | awk '/^vm:/{print $1}' | sed 's/vm://')

for cfg in /etc/pve/nodes/*/qemu-server/*.conf; do
  id=$(basename "$cfg" .conf)

  # Skip if already HA-managed
  if echo "$HA_VMS" | grep -qx "$id"; then
    continue
  fi

  # Skip templates
  if grep -q '^template: 1' "$cfg"; then
    continue
  fi

  # Skip vbr-worker VMs
  name=$(awk '/^name:/{print $2}' "$cfg")
  if [[ "$name" == vbr-worker* ]]; then
    continue
  fi

  # Only include Ceph RBD-backed VMs
  if grep -q "${STORAGE_ID}:" "$cfg"; then
    ha-manager add "vm:$id" --state started
    echo "Added vm:$id ($name) to HA"
  fi
done