首页 > 解决方案 > 如何使用 gcloud cli 获取操作系统类型

问题描述

我的gcp项目中有几个实例正在运行。

所以我需要遍历每个实例并需要获取操作系统版本,如果操作系统版本匹配某个值,我需要执行另一个gcloud命令来获取安装的库。

所以我使用这个命令来迭代每个实例:

$(gcloud compute instances list --format="value(name)")

// which gives the output as below

name: temp-1
---
name: temp-2
---
name: test1

我也需要摆脱name:

  1. 我需要使用每个实例的 linux 发行版(ubuntu、centos/rhel)/etc/os-release

是否可以 ssh(没有真正的 ssh-ing)并awk -F= '/^NAME/{print $2}' /etc/os-release在每个服务器(temp-1、temp-2、test1)中执行命令并将结果返回到命令行?

标签: google-cloud-platformgcloud

解决方案


我看到了几种方法来做到这一点。您可以使用以下任一命令:

$ gcloud compute ssh --zone us-central1-a instance-1 --command "uname -a"
Linux instance-1 4.19.0-11-cloud-amd64 #1 SMP Debian 4.19.146-1 (2020-09-17) x86_64 GNU/Linux

$ gcloud compute ssh --zone us-central1-a instance-1 -- uname -a
Linux instance-1 4.19.0-11-cloud-amd64 #1 SMP Debian 4.19.146-1 (2020-09-17) x86_64 GNU/Linux
Connection to n.n.n.n closed.

或者,您可以大规模使用 VM Manager 产品:


推荐阅读