just to do something that really should be part of the core API...
You're using the "core" API to get this info...
pvesh get /cluster/resources -type vm --noborder | awk '{if($13 == "your_vm_name") print $14}'
This is rather brittle and, e.g., doesn't work if there's a HA state, then it'd be the $14 column.
If you really are not allowed, or do not want, to install such a basic tool like
jq
you can always use existing interpreters like python (outputting all nodes, if there are multiple guests named the same way):
Code:
pvesh get /cluster/resources -type vm --noborder --output-format json-pretty | python3 -c "import json,sys;j=json.load(sys.stdin);print(next((d['node'] for d in j if d['name']=='YOUR_VM_NAME'), 'Guest not found'))"
Or Perl, just outputting the first it finds:
Code:
pvesh get /cluster/resources -type vm --noborder --output-format json | perl -MJSON -ne 'my $j=decode_json($_); for my $res ($j->@*) { if ($res->{type} =~ /qemu|lxc/ && $res->{name} eq "YOUR_VM_NAME") { print $res->{node}."\n"; last; }}'
Or Perl but getting all nodes for guests with the same name:
Code:
pvesh get /cluster/resources -type vm --noborder --output-format json | perl -MJSON -ne 'my $j=decode_json($_); print "$_->{node}\n" for grep { $_->{type} =~ /qemu|lxc/ && $_->{name} eq "YOUR_VM_NAME" } $j->@*;'
Both Python and Perl are widely available and installed by default on Proxmox VE, as both have native JSON modules it's way better to use such a structured format than some brittle AWK parsing.
One can also create a small shell script that makes this easier to use:
Bash:
#!/bin/sh
name="$1"
if [ -z "$name" ]; then
printf "usage: $0 <guest-name>\n"
exit 1
fi
pvesh get /cluster/resources -type vm --noborder --output-format json \
| NAME="$name" perl -MJSON -ne 'my $j=decode_json($_); print "$_->{node}\n" for grep { $_->{type} =~ /qemu|lxc/ && $_->{name} eq $ENV{"NAME"}} $j->@*;'
E.g., save this as
/usr/local/bin/find-guest-node
on Proxmox VE nodes, make it executable (
chmod +x /usr/local/bin/find-guest-node
) and then you can use
find-guest-node "vm name"
to get the node(s).
The pvesh command itself seems oddly thought out. It prints a column called
type that are invalid values as it's
type input.
The only thing slightly odd is that the input type's "vm" should be named "guest", as it includes all our virtual guest types.
That the return type uses the value of the specific type has also its reasons though, we had other implementations for some types like CT was openvz a long time ago. And while we always support both VMs and CTs, the underlying technology might change, thus the "different" types so that frontends could decide how to operate on a guest, as depending on the actual type different features and such are available. So input asks for guests, which makes sense as VMIDs are a single namespace between all guests, and output gives you the specific type; we see no issue there (besides, as already mentioned, that the input "vm" should be "guest", but just breaking API for that seem a lot of churn for a lot of projects that depend on us..)