2

I was wondering how to get the value of a specific metadata for a given instance in GCE.

For now I can view all the instance property with :

gcutil getinstance instance-test

But it returns all the metadata and I can not find a "filter" option in the gcutil documentation.

Any ideas ?

Benoît Sauvère
  • 701
  • 7
  • 23
  • 1
    I don't know what the output from gcutil looks like, but the Unix Way (TM) would be to pipe the output into a separate "filter" program (grep, sed, awk, a perl script, etc.) – Solomon Slow Mar 18 '14 at 18:39
  • For now I use sed like that : `gcutil getinstance instance-test 2> /dev/null | grep ".*METADATA.*[a-zA-Z0-9-]*.*" | sed "s/ //g" | sed "s/|//g" | sed "s/METADATA//g"` But this solution works only if our metadata value contains only letters, numbers and hyphen. – Benoît Sauvère Mar 18 '14 at 18:51
  • 1
    If you want few output examples : [Storing and Retrieving Metadata](https://developers.google.com/compute/docs/metadata#updatinginstancemetadata) – Benoît Sauvère Mar 18 '14 at 18:53

1 Answers1

4

You can have gcutil output into JSON format instead of tabular format by using the --format flag (see 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')

For example:

gcutil --format=json getinstance instance-test

Combine this with the jq for easy, powerful commands:

$ gcutil --format=json --service_version=v1 --project="<project>" \
      --zone="<zone>" getinstance "<instance>" | jq '.metadata.kind'
"compute#metadata"
jterrace
  • 64,866
  • 22
  • 157
  • 202