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:

  1. Install Ansible: Ensure you have Ansible installed on your local machine. You can follow this Answer for installation purpose.

  2. 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:

    1. Create a service account in your GCP project and download the service account key JSON file.

    2. Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to the downloaded JSON file. This step allows Ansible to use the service account for authentication.

1 of 3
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/service-account-key.json
Setting the credentials environment variable
  1. Install the google-auth module: Ansible does not include the GCP modules by default. You can install them using the following command:

ansible-galaxy collection install google.cloud
Command to install google-auth module
  1. 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 
main.yml contents

Instructions for customizing the Google Cloud Compute Engine instance:

  • Replace credentials.json:

    • Before running the playbook, ensure you replace friendly-slate-393106-fa20bf4cfa3e.json with 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 image variable. Replace centos-7 with 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_type variable to set the desired machine type for the instance. For example, you can change n2-standard-2 to a different machine type that suits your needs.

  • Define region:

    • Modify the zone and region variables to select the desired region for your instance. The current configuration uses us-central1-a as the zone and us-central1 as the region.

  1. Run the playbook: Save the playbook file with a .yaml extension (e.g., main.yaml). Then, run the playbook using the ansible-playbook command:

ansible-playbook main.yaml
Command to run Ansible playbook

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

1 of 2

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.

Copyright ©2024 Educative, Inc. All rights reserved