Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a list of all project owners from GCP

I am trying to get a list of all GCP projects on the domain and the project owners and export it to a CSV so I can throw it into a google sheet. Getting a list is simple enough, but I can't find a way to get the owners for each project.

like image 685
SL8t7 Avatar asked Sep 14 '25 23:09

SL8t7


1 Answers

ROLE="roles/owner"
for PROJECT in $(\
  gcloud projects list \
  --format="value(projectId)" \
  --filter="projectId ~ something")
do
  printf "%s:\n" ${PROJECT}
  gcloud projects get-iam-policy ${PROJECT} \
  --flatten="bindings[].members[]" \
  --filter="bindings.role=${ROLE}" \
  --format="value(bindings.members)"
  printf "\n"
done

For completeness, using the excellent jq which is both more general-purpose and -- I think -- easier to use:

for PROJECT in $(\
  gcloud projects list \
  --format="value(projectId)" \
  --filter="projectId ~ something")
do
  printf "%s:\n" ${PROJECT}
  gcloud projects get-iam-policy ${PROJECT} --format="json" \
  | jq -r '.bindings[] | select(.role=="roles/owner") | .members[]'
  printf "\n"
done
like image 86
DazWilkin Avatar answered Sep 17 '25 19:09

DazWilkin