Creating Google Cloud Platform(GCP) VM instances with Ansible
Google Cloud VM instances are virtual machines hosted on Google Cloud Platform (GCP) that allow users to deploy and run applications in the cloud. Ansible, on the other hand, is an open-source automation tool used for configuration management, application deployment, and task automation. When combined, Ansible can be leveraged to automate the provisioning and management of Google Cloud VM instances, providing a seamless and consistent approach to infrastructure deployment.
Creating a GCP VM instance
To create a Google Cloud Platform (GCP) VM instance using Ansible, you can follow these steps:
Install Ansible: Ensure you have Ansible installed on your local machine. You can follow this Answer for installation purpose.
Set up GCP authentication: Ansible requires authentication to interact with GCP APIs. You can use a service account key or gcloud CLI authentication. For simplicity, we'll demonstrate using a service account key:
Create a service account in your GCP project and download the service account key JSON file.
Set the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable to point to the downloaded JSON file. This step allows Ansible to use the service account for authentication.
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/service-account-key.json
Install the
google-authmodule: Ansible does not include the GCP modules by default. You can install them using the following command:
ansible-galaxy collection install google.cloud
Write the Ansible playbook: Create an Ansible playbook that defines the GCP VM instance's configuration. Here's an example playbook:
- name: Create Compute Engine instances
hosts: localhost
gather_facts: no
vars:
gcp_project: friendly-slate-393106
gcp_cred_kind: serviceaccount
gcp_cred_file: "friendly-slate-393106-fa20bf4cfa3e.json"
zone: "us-central1-a"
region: "us-central1"
machine_type: "n2-standard-2"
image: "centos-7"
tasks:
- name: Create private IP address to the VM instance
gcp_compute_address:
name: "{{ zone }}-ip"
region: "{{ region }}"
project: "{{ gcp_project }}"
service_account_file: "{{ gcp_cred_file }}"
auth_kind: "{{ gcp_cred_kind }}"
register: gce_ip
- name: Bring up the instance in the zone
gcp_compute_instance:
name: "{{ zone }}"
machine_type: "{{ machine_type }}"
disks:
- auto_delete: true
boot: true
initialize_params:
source_image: "{{ image }}"
network_interfaces:
- access_configs:
- name: External NAT # public IP
nat_ip: "{{ gce_ip }}"
type: ONE_TO_ONE_NAT
tags:
items:
- http-server
- https-server
zone: "{{ zone }}"
project: "{{ gcp_project }}"
service_account_file: "{{ gcp_cred_file }}"
auth_kind: "{{ gcp_cred_kind }}"
register: gce Instructions for customizing the Google Cloud Compute Engine instance:
Replace credentials.json:
Before running the playbook, ensure you replace
friendly-slate-393106-fa20bf4cfa3e.jsonwith your own Google Cloud service account key file (credentials.json). This file contains the credentials required to authenticate with Google Cloud.Choose Linux version:
In the playbook, you can specify your desired Linux version by changing the
imagevariable. Replacecentos-7with the name of the Linux image you want to use. You can find the available images in the Google Cloud Console.Select machine type:
Customize the
machine_typevariable to set the desired machine type for the instance. For example, you can changen2-standard-2to a different machine type that suits your needs.Define region:
Modify the
zoneandregionvariables to select the desired region for your instance. The current configuration usesus-central1-aas the zone andus-central1as the region.
Run the playbook: Save the playbook file with a
.yamlextension (e.g.,main.yaml). Then, run the playbook using theansible-playbookcommand:
ansible-playbook main.yaml
Expected output
The expected output of running this playbook would be the result of the tasks being executed by Ansible. Here's a general overview of what you might expect to see in the output:
Task: Create private IP address to the VM instance
Task: Bring up the instance in the zone
Conclusion
By following the steps provided, users can customize the playbook to create VM instances with their preferred Linux distribution and settings in Google Cloud Platform. The playbook serves as a foundation for automating infrastructure deployment on GCP through Ansible.
Free Resources