Reputation: 317
I am trying to run gcloud compute instances list
on a project and only get a list of instances using a specific boot disk or image.
What I am currently running is:
gcloud compute instances list --project=my-project --filter="disks.initializeParams.sourceImage='image-name'"
However this is returning 0 items, moreover any filter I run into initializeParams returns 0 items. Am I doing something wrong with my filtering? Or is there a better way to get a list of instances using a specific image?
Upvotes: 2
Views: 2272
Reputation: 40136
Using only gcloud
:
gcloud compute disks list \
--project=${PROJECT} \
--format="value(users[].basename(),sourceImage.scope())"
Upvotes: 2
Reputation: 75800
Start to perform a search in JSON mode like this
gcloud compute instances list --format=json
Then, focus on the field that you search for, and copy the JSON path to your filter (don't worry about array or not, put dot to create the hierarchy). Like this (for example)
gcloud compute instances list --format=json \
--filter="disks.licenses=https://www.googleapis.com/compute/v1/projects/debian-cloud/global/licenses/debian-10-buster"
Upvotes: 0
Reputation: 4461
To find which source images were used while creating instances you can follow steps below:
$ gcloud compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
instance-1 us-central1-a e2-medium 10.128.0.47 35.192.46.67 RUNNING
instance-2 us-central1-a e2-medium 10.128.0.48 104.197.166.219 RUNNING
instance-3 us-central1-a e2-medium 10.128.0.49 34.123.122.184 RUNNING
instance-4 us-central1-a e2-medium 10.128.0.50 35.222.216.107 RUNNING
sourceImageId
s in the same way as sourceImage
):$ gcloud compute disks describe instance-1 --zone us-central1-a | grep sourceImage:
sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-10-buster-v20210122
$ gcloud compute disks describe instance-2 --zone us-central1-a | grep sourceImage:
sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-10-buster-v20210122
$ gcloud compute disks describe instance-3 --zone us-central1-a | grep sourceImage:
sourceImage: https://www.googleapis.com/compute/v1/projects/centos-cloud/global/images/centos-7-v20210122
$ gcloud compute disks describe instance-4 --zone us-central1-a | grep sourceImage:
sourceImage: https://www.googleapis.com/compute/v1/projects/test-prj/global/images/custom-image-1
VM SOURCE IMAGE
instance-1 debian-10-buster-v20210122
instance-2 debian-10-buster-v20210122
instance-3 centos-7-v20210122
instance-4 custom-image-1
Also, you can write some script, here an example for GCE VMs in same zone us-central1-a
:
$ cat script.sh
#!/bin/bash
vms=( $(gcloud compute instances list | awk '{print $1}' | grep -v NAME) )
for (( i = 0; i < ${#vms[@]} ; i++ )); do
printf "${vms[$i]} "
eval "gcloud compute disks describe ${vms[$i]} --zone us-central1-a | grep sourceImage: | awk '{print $2}'"
done
$ ./script.sh
instance-1 sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-10-buster-v20210122
instance-2 sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-10-buster-v20210122
instance-3 sourceImage: https://www.googleapis.com/compute/v1/projects/centos-cloud/global/images/centos-7-v20210122
instance-4 sourceImage: https://www.googleapis.com/compute/v1/projects/test-prj/global/images/custom-image-1
$ ./script.sh | grep debian | awk '{print $1}'
instance-1
instance-2
Also, you can look on API requests.
Upvotes: 1