1

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?

ellefc
  • 233
  • 2
  • 9

1 Answers1

1

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
done
guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76
  • I have updated the question - could i include this loop to pick up all the VMs? – ellefc Jun 16 '21 at 15:32
  • I got to this but it isn't quite working; for i in $(gcloud compute instances list | awk '{print $1}' | awk 'NR>1'): for zone in $(gcloud compute zones list --format='value(name)'); do echo INSTANCE: $i && echo "--" && gcloud compute disks describe $i --zone=$zone | grep sourceImage && echo ""; done; – ellefc Jun 16 '21 at 15:50