Automating GCP Instance Management with Ansible

Introduction

Managing cloud resources efficiently is crucial for any organization. Google Cloud Platform (GCP) offers robust infrastructure services, and Ansible, an open-source automation tool, can simplify the process of creating, deleting, and stopping instances. This blog post will guide you through using Ansible to automate GCP instance management.

Prerequisites

Before you begin, ensure you have the following:

  • Google Cloud Platform account with the necessary credentials

  • Ansible installed on your local machine

  • google-auth the library installed for Ansible GCP module support

Setting Up GCP Credentials

Ansible uses service account credentials for GCP. Download the service account key file from the GCP Console.

To work with the GCP modules, you’ll first need to get some credentials in the JSON format:

  1. Create a service account for accessing the GCP console through Ansible and give the owner role.

    First, create a service account:

    1. Open the Service Accounts page.

    2. If prompted, select a project, or create a new one.

    3. Click Add Create service account.

    4. Under Service account details, type a name, ID, and description for the service account, then click Create and continue.

    5. Optional: Under Grant this service account access to the project, select the IAM roles to grant to the service account.

    6. Click Continue.

    7. Optional: Under Grant users access to this service account, add the users or groups that are allowed to use and manage the service account.

    8. Click Done

  1. Download JSON credentials

    Next, create a service account key:

    1. Click the email address for the service account you created.

    2. Click the Keys tab.

    3. In the Add key drop-down list, select Create new key.

    4. Click Create.

    5. Then download the service key (JSON) of the service account in GCP into your virtual machine because here I am using Kali Linux vm in my local machine as like a virtual box.

  1. After downloading this json key service and movie to ansible folder structure.

Install the Gcp support module, gcp cli and Ansible.

  1. Install gcp cli

    To install GCP cli for debian follow this link

    https://cloud.google.com/sdk/docs/install

  2. After installation of GCP cli and set up the gcloud auth setup and give connection to your local vm to gcp console.

  3. Install Ansible and gcp modules.

Ansible Playbook for GCP Instance Creation

Create an Ansible playbook (e.g., gcp_vm_create.yaml) for launching a GCP instance:

---
- name: Create an instance
  hosts: localhost
  gather_facts: false
  vars:
    gcp_project: ashok-198510
    gcp_cred_kind: serviceaccount
    gcp_cred_file: /home/sana/ansible-labs/ansible-vm-gcp/ashok-198510.json
    gcp_source_image: 'projects/debian-cloud/global/images/debian-11-bullseye-v20231212'
    zone: "us-central1-c"
    region: "us-central1"

  tasks:
    - name: create a disk
      gcp_compute_disk:
        name: 'disk-instance'
        size_gb: 50
        source_image: "{{ gcp_source_image }}"
        zone: "{{ zone }}"
        project: "{{ gcp_project }}"
        auth_kind: "{{ gcp_cred_kind }}"
        service_account_file: "{{ gcp_cred_file }}"
        scopes:
          - https://www.googleapis.com/auth/compute
        state: present
      register: disk

    - name: Create GCP VM
      gcp_compute_instance:
        name: 'sample-vm'
        machine_type: "n1-standard-1"
        disks:
          - auto_delete: true
            boot: true
            source: "{{ disk }}"
        network_interfaces:
          - network: null  # Use the default VPC
            access_configs:
              - name: External Nat
                type: ONE_TO_ONE_NAT
        zone: "{{ zone }}"
        project: "{{ gcp_project }}"
        auth_kind: "{{ gcp_cred_kind }}"
        service_account_file: "{{ gcp_cred_file }}"
        state: present
      register: vm

    - name: Show Instance Details
      debug:
        var: vm

Adjust the parameters such as name, machine_type, disk_size_gb, image_family, image_project, project, and zone based on your requirements.

Here where you get this image name gcp_source_image: 'projects/debian-cloud/global/images/debian-11-bullseye-v20231212'

if you want to os you can also can the os.

Execute the playbooks using the following commands:

$ansible-playbook gcp_vm_create.yaml

Ansible Playbook for GCP Instance Deletion

Create another Ansible playbook (e.g., gcp_vm_terminate.yaml) for deleting a GCP instance:

---
- name: Create an instance
  hosts: localhost
  gather_facts: false
  vars:
    gcp_project: ashok-198510
    gcp_cred_kind: serviceaccount
    gcp_cred_file: /home/sana/ansible-labs/ansible-vm-gcp/ashok-198510.json
    gcp_source_image: 'projects/debian-cloud/global/images/debian-11-bullseye-v20231212'
    zone: "us-central1-c"
    region: "us-central1"

  tasks:
    - name: Terminate GCP VM
      gcp_compute_instance:
        name: 'sample-vm'
        zone: "{{ zone }}"
        project: "{{ gcp_project }}"
        auth_kind: "{{ gcp_cred_kind }}"
        service_account_file: "{{ gcp_cred_file }}"
        state: absent
      register: vm

    - name: Terminate a disk
      gcp_compute_disk:
        name: 'disk-instance'
        zone: "{{ zone }}"
        project: "{{ gcp_project }}"
        auth_kind: "{{ gcp_cred_kind }}"
        service_account_file: "{{ gcp_cred_file }}"
        state: absent
      register: disk

    - name: Show Instance Details
      debug:
        var: vm

Replace your-instance-name, your-project, and your-zone with the actual instance name, project, and zone.

Running Ansible Playbooks

Execute the playbooks using the following commands:

$ansible-playbook gcp_vm_terminate.yaml

Conclusion

Automating GCP instance management with Ansible enhances efficiency and consistency in your cloud infrastructure. As you delve deeper into Ansible's capabilities, you can build more complex automation workflows tailored to your organization's needs.

Happy automating on the Google Cloud Platform!

I hope you people like this blog.

If you like this blog please follow these below Links, You will get more content like this in that links.

WhatsApp Group:- https://chat.whatsapp.com/Ii2xKz9vuW93AWt07m4AYj

Telegram:- https://t.me/ExplorewithAshok

LinkedIn: https://www.linkedin.com/in/ashok-sana

Instagram:- https://instagram.com/explorewithashok?igshid=OGQ5ZDc2ODk2ZA==

Linktree:- https://linktr.ee/ashoksana

Did you find this article valuable?

Support ASHOK SANA by becoming a sponsor. Any amount is appreciated!