bashformattinggcloud

gcloud command output formatting with previous variables of the another gcloud command


I have:

var=$(gcloud compute instance-groups list-instances ig --region=europe-west1 --format="csv[no-heading](NAME)")

gcloud compute instance-groups describe ig --format="csv[no-heading](name,$var)"

Get error:

**ERROR**: (gcloud.compute.instance-groups.describe) Non-empty key name expected [default csv[no-heading](name, *HERE* '')]

Any idea to resolve this?

Thanks in advance Regards


Solution

  • You can get all the information you need from the first command:

    NAME="..." # Instance Group name
    REGION="..."
    PROJECT="..."
    
    gcloud compute instance-groups list-instances ${NAME} \
    --region=${REGION} \
    --project=${PROJECT} \
    --format="value(format(\"${NAME},{0}\",instance))"
    

    This uses the format projection to generate a CSV formatted line combining the Instance Group name (${NAME}) with the instance (name).

    Bash will replace the value of ${NAME} with its value before it invokes gcloud but the result is (I think) what you want.