Quantcast
Channel: DevopsCube
Viewing all articles
Browse latest Browse all 17

How To Setup Ansible Dynamic Inventory For Google Cloud

$
0
0

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.

[irp posts=”725″ name=”How to Setup Ansible AWS Dynamic Inventory”]

Step 1: Install lib-cloud module

pip install apache-libcloud

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

sudo mkdir -p /opt/ansible/inventory
chmod -R 755 /opt/ansible

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

git clone https://github.com/ansible/ansible

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

sudo cp ansible/contrib/inventory/gce.py /opt/ansible/inventory/

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

sudo cp ansible/contrib/inventory/gce.ini /opt/ansible

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.

{
  "type": "service_account",
  "project_id": "devopscube-sandbox",
  "private_key_id": "sdfkjhsadfkjansdf9asdf87eraksd",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBaksdhfjkasdljf sALDIFUHW8klhklSDGKAPISD GIAJDGHIJLSDGJAFSHGJN;MLASDKJHFGHAILFN DGALIJDFHG;ALSDN J Lkhawu8a2 87356801w tljasdbjkh=\n-----END PRIVATE KEY-----\n",
  "client_email": "ansible-provisioning@devopscube-sandbox.iam.gserviceaccount.com",
  "client_id": "32453948568273645823",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/ansible-provisioning%40devopscube-sandbox.iam.gserviceaccount.com"
}

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

gce_service_account_email_address = ansible-provisioning@devopscube-sandbox.iam.gserviceaccount.com
gce_service_account_pem_file_path = /opt/ansible/service-account.json
gce_project_id = devopscube-sandbox

Step 7: Export GCE_INI_PATH variable.

export GCE_INI_PATH=/opt/ansible/gce.ini

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

cd /opt/ansible/inventory/
./gce.py --list

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

The post How To Setup Ansible Dynamic Inventory For Google Cloud appeared first on DevopsCube.


Viewing all articles
Browse latest Browse all 17

Trending Articles