How To Setup Ansible Dynamic Inventory For Google Cloud

The best way to orchestrate VM instances in Google cloud using Ansible is through Dynamic inventory script. It is a python script which queries GCE during the Ansible run time. This allows you not to hardcode any instance information in the inventory file. And the management of VM instances through tags will be very easy.

Ansible Dynamic Inventory On Google Cloud

This article talks about setting up the Dynamic Inventory script for Google Cloud. Lets get started.

Prerequisites:

  1. You should have pip installed
  2. Ansible installed
  3. Google Service Account json with permissions to provision GCP resources.
READ  How to Setup Ansible AWS Dynamic Inventory

Step 1: Install lib-cloud module

Step 2: Create a dedicated inventory directory and change the folder permission. Also copy your GCP service account json to /opt/ansible directory.

Step 3: Clone the ansible repo from github. It has the dynamic inventory script and its configuration file.

Step 4:  Copy the gce.py file to the inventory directory.

Step 5:  Copy the gce.ini config file to ansible directory.

Step 6: Open gce.ini file and configure the following values from the service accout json file. A service account json will look like the following.

In gce.ini you have to configure the following values.

Step 7: Export GCE_INI_PATH variable.

Step 8: Test if the dynamic inventory is working by executing the python inventory script.

The above command should list all the GCE instances for the configured project.

Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.