Using tags in CLI?

Dunuin

Distinguished Member
Jun 30, 2020
14,356
4,213
243
Germany
Hi,

Yesterday I wrote a bash hook script to automate workarounds that circumvent problems I got with backups, making it possible to backup VMs that share the same PCIe device and allow to shutdown a PBS without making the webUI unusable.

For that I had to group VMs that share a PCIe device. Right now I just hardcoded the VMIDs in a bash array. But this means I will have to edit the hook script every time I add/remove a VM.

I now had the idea that I could make use of the new tagging feature to group my VMs via the webUI by assigning them the same tag.

But I wasn't able to find out how to work with tags from CLI.

Isn't that supported yet?

What I would need is something like a qm getVmidsByTag "mytag" which would output a list of all VMIDs that are tagged with "mytag".

And something like a qm getTags 100 which would output a list of all tags that the VM with VMID 100 is using.

If those CLI commands doesn't already exist, would it be possible to do something like that using the API by calling pvesh?
 
Last edited:
Hi,
this would require to extend qm list to output tags (or even better, make it configurable what it outputs) and to allow filtering. Feel free to create a feature request on the bug tracker after checking for existing ones: https://bugzilla.proxmox.com/

For now, you could use something like:
Code:
pvesh get /cluster/resources --type vm --output-format json | jq -r 'map(select(.type == "qemu" and .node == "your-node" and .tags and (.tags | contains("your-tag")))) | map(.vmid) | join(" ")'
of course replacing your-tag and your-node with what you need.

EDIT: Note that if the tag is a substring of another, using contains() will lead to false positives. There is also match() which takes a regex, so you can check for surrounding commas/end-of-line/beginning-of-line to match the tag exactly, i.e. match("(^|,|;)your-tag(,|;|$)").
 
Last edited:

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!