How can I find the number of Compute Engine Instances with a COS image vs the number of instances with a non-COS image?

I am able to identify all the images that are available to my Compute Engine instances and can identify the sourceImage of a single instance however I need to always state a zone so I am struggling to figure out how to build on the command

gcloud compute disks describe 'instance-name' --zone='zone'

to be able to get an understanding of the number of instances with a Cos image and the number of instances running with a non-Cos image.

Any ideas on how to find an answer?

I have got to this point:

for i in $(gcloud compute instances list | awk '{print $1}' | awk 'NR>1'); do echo INSTANCE: $i && echo "--" && gcloud compute disks describe $i --zone=europe-west1-b| grep sourceImage   && echo ""; done

this would allow me to find the sourceImage for all the VMs in a project for a specific zone but i would want the information for all VMs and so all zones need to be covered - can i make this work with a loop for zones too?

1 answer

  • answered 2021-06-16 14:47 guillaume blaquiere

    You need to loop on the zones, something like that

    for zone in $(gcloud compute zones list --format='value(name)')
      do gcloud compute disks describe 'instance-name' --zone=$zone