Tags to dsh (clustershell / pdsh) groups tool?

Jun 12, 2025
1
0
1
Hi folks,

I'm looking for a way to generate dsh hostgroups (as used by clustershell / pdsh / dsh and similar "ssh in parallel to multiple machines" tools) from tags.

I don't see a direct API call to get tags -> vmids, so it appears that I'm going to have to iterate over each node and VM (either by cli `qm config $vmid | grep ^tags` or API (GET /api2/json/nodes/{node}/qemu/{vmid}/config)

1) has anybody done this before so I don't reinvent the wheel?
2) are there any better ways to gather tag info?

Many thanks
Andrew
 
Hi @elwell2000 , welcome to the forum.

AI is not a bad thing when you ask it the right questions:

Code:
#!/bin/bash

# Description:
#   Generates a CSV list of tag,vmid,name for all VMs in a PVE cluster using pvesh.
#   Output format: tag,vmid,name
#   VMs with multiple tags will produce multiple lines (one per tag).

# Output file
OUTPUT_FILE="pve_vm_tags.csv"

# Write CSV header
echo "tag,vmid,name" > "$OUTPUT_FILE"

# Iterate over all nodes in the cluster
for node in $(pvesh get /nodes --output-format=json | jq -r '.[].node'); do
  # Get all QEMU VMs on this node
  vms=$(pvesh get /nodes/$node/qemu --output-format=json)

  echo "$vms" | jq -c '.[]' | while read -r vm; do
    vmid=$(echo "$vm" | jq -r '.vmid')
    name=$(echo "$vm" | jq -r '.name // empty')

    # Get tags (can be empty)
    tags=$(pvesh get /nodes/$node/qemu/$vmid/config --output-format=json | jq -r '.tags // ""')

    # Split tags by ';' and write each as a line
    IFS=';' read -ra tag_array <<< "$tags"
    if [[ ${#tag_array[@]} -eq 0 || -z "${tag_array[0]}" ]]; then
      echo ",$vmid,$name" >> "$OUTPUT_FILE"
    else
      for tag in "${tag_array[@]}"; do
        echo "$tag,$vmid,$name" >> "$OUTPUT_FILE"
      done
    fi
  done
done

echo "CSV saved to $OUTPUT_FILE"


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox