Skip to content

Instantly share code, notes, and snippets.

@holysugar
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save holysugar/391b359c2f531b4fc506 to your computer and use it in GitHub Desktop.

Select an option

Save holysugar/391b359c2f531b4fc506 to your computer and use it in GitHub Desktop.
GCEでメタデータからインスタンス名を引く
#!/bin/sh
# ここでは environment と role というメタデータがあると仮定
environment=$1
role=$2
if [ -z "$role" ]; then
echo "[usage] $0 environment role"
exit 1
fi
gcloud compute instances list --format json |
jq -r "map(select(.metadata.items)) |
map(select(.metadata.items | from_entries | .environment == \"${environment}\" and .role == \"${role}\")) |
.[].name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment