【问题标题】:How can I get a specific metadata value of a Google Compute Engine instance?如何获取 Google Compute Engine 实例的特定元数据值?
【发布时间】:2014-03-18 18:28:58
【问题描述】:

我想知道如何获取 GCE 中给定实例的特定元数据的值。

现在我可以查看所有实例属性:

gcutil getinstance instance-test

但它返回所有元数据,我在 gcutil 文档中找不到“过滤器”选项。

有什么想法吗?

【问题讨论】:

  • 我不知道 gcutil 的输出是什么样的,但 Unix Way (TM) 会将输出通过管道传输到单独的“过滤器”程序(grep、sed、awk、perl脚本等)
  • 现在我像这样使用 sed:gcutil getinstance instance-test 2> /dev/null | grep ".*METADATA.*[a-zA-Z0-9-]*.*" | sed "s/ //g" | sed "s/|//g" | sed "s/METADATA//g" 但这个解决方案只有在我们的元数据值只包含字母、数字和连字符时才有效。
  • 如果你想要几个输出示例:Storing and Retrieving Metadata

标签: google-compute-engine gcutil


【解决方案1】:

您可以使用--format 标志将 gcutil 输出为 JSON 格式而不是表格格式(请参阅 gcutil --help):

--format: <table|sparse|json|csv|names>: Format for command output. Options include:
  table: formatted table output
  sparse: simpler table output
  json: raw json output (formerly --print_json)
  csv: csv format with header
  names: list of resource names only, no header
  (default: 'table')

例如:

gcutil --format=json getinstance instance-test

将其与jq 结合使用以获得简单而强大的命令:

$ gcutil --format=json --service_version=v1 --project="<project>" \
      --zone="<zone>" getinstance "<instance>" | jq '.metadata.kind'
"compute#metadata"

【讨论】:

  • 非常感谢。我使用的是旧版本的 gcutil 工具,没有格式选项。
猜你喜欢
  • 2016-03-13
  • 1970-01-01
  • 2014-11-22
  • 2014-11-12
  • 1970-01-01
  • 2016-02-27
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
相关资源
最近更新 更多