For some time, Ansible has been my configuration management of choice and we use it for both Screenly and YippieMove. Since both of these services are running on Google Compute Engine, we’re using Ansible’s dynamic inventory for GCE.

(Behind the scenes, this dynamic inventory is using Apache Libcloud, which is a great Python library for interacting with various providers.)

When I first followed Ansible’s Google Cloud Platform Guide I did run into a fair bit of trouble with authentication.

As it turns out, Libcloud is very picky when it comes to the environment variables that you need to set. To solve this, I whipped together a little script that I call on to set the appropriate variable environments for each project I’m working on. This saved me a lot of headache.

The script looks as follows:

#!/bin/bash
export GCE_PROJECT=your-project
export GCE_PEM_FILE_PATH=~/.gce/$GCE_PROJECT\.json
export GCE_EMAIL=$(grep client_email $GCE_PEM_FILE_PATH | sed -e 's/  "client_email": "//g' -e 's/",//g')
gcloud config set project $GCE_PROJECT

Just change GCE_PROJECT to match your setup, and then run:

$ source /path/to/script.sh

You can now run Ansible with the GCE inventory file.

As an added bonus, this also configures gcloud to the same project.

Happy (DevOps) hacking.