同时向多个 GCP 计算实例添加标签

add labels to multiple GCP compute instances at the same time

我可以同时使用 gcloud 更新多个 GCP 实例吗?我想为我的所有实例添加标签,但我没有看到像 AWS 的 awscli.

那样同时为多个实例添加标签的方法

我能做到

gcloud compute instances update test-instance --update-labels test=foo

想要

gcloud compute instances update test-instance1 test-instance2 --update-labels test=foo

据我所知,没有。然而,使用 shell(例如 bash)枚举实例列表和 运行 每个实例的命令是直接的:

for INSTANCE in "test-instance1" "test-instance2"
do
  gcloud compute instances update ${INSTANCE} --update-labels ...
done

该命令似乎 支持 --async 标志,因此您需要按顺序 运行 这些命令,除非您将它们放入后台。

这种方法不如发出一个影响多个更新的命令(服务端)那么优雅,但我怀疑这是一个相对不常见的要求,并且对于较少数量的实例,现有命令可能就足够了。

您可以将更新与例如实例列表:

for INSTANCE in $(gcloud compute instances list --format="value(name)")
...

您可以向 Google 提交功能请求:

https://issuetracker.google.com/issues/new?component=187143&template=1163129