Authenticating to Google Cloud as oneself within Ansible
In my day job at Cottage Labs, we're developing a Google Cloud deployment infrastructure for a client, e using Ansible for configuration management.
Ansible has a whole suite of modules for managing Google Cloud resources. Each of these takes the same parameters for authentication, but they don't have the best of explanations:
Until now I'd been setting the GOOGLE_APPLICATION_CREDENTIALS environment variable within my playbooks, pointing at a fixed path containing the JSON-formatted key for a service account. This works fine when you have the Owner role within the Google Cloud project, but when you're an Editor you don't have the permissions to assign your Editor role to your service account. It also feels messy —when it's you deploying as yourself, in person, you should be authenticated as yourself. The service account is intended to be used by an automated process, and is not linked to any user.
First off, we ensure that GOOGLE_APPLICATION_CREDENTIALS is not set.
Next, we run:
This will pop up a browser window asking which of your Google accounts you want to authenticate as. On your terminal you'll then see that the gcloud tool has created a default credentials file:
Finally, we choose the application auth_kind in our Ansible playbook tasks:
By mapping Google Cloud projects to Ansible hosts, …
… we can then deploy to multiple projects with ease: