Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

How to create an instance on GCP using the Ansible CLI

July 12, 2023
Deepankar Jain
Related topics:
Automation and management
Related products:
Red Hat Ansible Automation PlatformRed Hat Ansible Automation Platform from Google Cloud

    This series covers the end-to-end process of creating an instance on Google Cloud Platform (GCP) using Red Hat Ansible Automation Platform. This 3-part series includes:

    Part 1: How to create an instance on GCP using Ansible CLI

    Part 2: How to create a GCP instance using Ansible Automation

    Part 3: How to create a GCP instance via workflow and Ansible

    By the end of this article, you will have a clear understanding of how to use the Ansible Automation Platform CLI to automate the creation of GCP instances, which will save you time and reduce the risk of manual errors. Let's get started!

    Prerequisites

    • Ansible installed on your system.
    • An active GCP Account with sufficient permissions.
    • Ansible google cloud collection installed on your system.

    We will start by setting up the necessary credentials and roles for our Ansible playbook to access the GCP API. Then we will create a disk, a network, a security group, and an IP address before finally launching the instance.

    How to use Ansible CLI

    • Create a service account in GCP.
    • Generate the credentials for the service account.
    • You should now have a credential.json file that you can use to access your GCP account and launch an instance.
    • Open any editor and copy the following yml into it.
    ---
    
    - name: Create instance in GCP
    
      hosts: localhost
    
      gather_facts: false
    
      vars:
    
        service_account_file: "<path to service account file>"
    
        project: "<SOMETHING>"
    
        network_name: "test-ansible-network"
    
        subnet_name: "test-ansible-subnet"
    
        ip_name: "test-ansible-ip"
    
        disk_name: "test-ansible-disk"
    
        machine_name: "test-ansible"
    
        region: "asia-south2"
    
        zone: "asia-south2-a"
    
        source_image: "projects/ubuntu-os-cloud/global/images/family/ubuntu-1804-lts"
    
        subnet_cidr: "10.0.1.0/24"
    
        disk_size: 10
    
        machine_type: "f1-micro"
    
    
    
      tasks:
    
        - name: Create a disk
    
          google.cloud.gcp_compute_disk:
    
            name: "{{ disk_name }}"
    
            size_gb: "{{ disk_size }}"
    
            source_image: "{{ source_image }}"
    
            zone: "{{ zone }}"
    
            project: "{{ project }}"
    
            auth_kind: serviceaccount
    
            service_account_file: "{{ service_account_file }}"
    
            state: present
    
          register: disk
    
        
    
        - name: Create a Network in GCP
    
          google.cloud.gcp_compute_network:
    
            auth_kind: serviceaccount
    
            project: "{{ project }}"
    
            service_account_file: "{{ service_account_file }}"
    
            name: "{{ network_name }}"
    
            auto_create_subnetworks: false
    
            state: present
    
          register: network
    
    
    
        - name: Create a Subnet in the Network
    
          google.cloud.gcp_compute_subnetwork:
    
            auth_kind: serviceaccount
    
            project: "{{ project }}"
    
            service_account_file: "{{ service_account_file }}"
    
            name: "{{ subnet_name }}"
    
            region: "{{ region }}"
    
            ip_cidr_range: "{{ subnet_cidr }}"
    
            network: "{{ network }}"
    
            state: present
    
          register: subnet
    
    
    
        - name: Reserve a static IP Address
    
          google.cloud.gcp_compute_address:
    
            auth_kind: serviceaccount
    
            project: "{{ project }}"
    
            service_account_file: "{{ service_account_file }}"
    
            name: "{{ ip_name }}"
    
            region: "{{ region }}"
    
            state: present
    
          register: address
    
            
    
        - name: Create an Instance 
    
          google.cloud.gcp_compute_instance:
    
            auth_kind: serviceaccount
    
            project: "{{ project }}"
    
            service_account_file: "{{ service_account_file }}"
    
            state: present
    
            name: "{{ machine_name }}"
    
            machine_type: "{{ machine_type }}"
    
            zone: "{{ zone }}"
    
            disks:
    
              - auto_delete: true
    
                boot: true
    
                source: "{{ disk }}"
    
            network_interfaces:
    
              - network: "{{ network }}"
    
                subnetwork: "{{ subnet }}"
    
                access_configs:
    
                  - name: External NAT
    
                    type: ONE_TO_ONE_NAT
    
                    nat_ip: "{{ address }}"
    
    
    
    
    • Save and close the file.
    • Then open the terminal in the directory where the file is located.
    • Now run the following command:
      ansible-playbook <filename>.yml

    The output:

    PLAY [Create instance in GCP] ************************************************************************************************************************************************************
    
    TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
    ok: [localhost]
    
    TASK [Create a disk] ************************************************************************************************************************************************************************************
    changed: [localhost]
    
    TASK [Create a Network in GCP] ***********************************************************************************************************************************************************************************
    changed: [localhost]
    
    TASK [Create a Subnet in the Network] ********************************************************************************************************************************************************************************************
    changed: [localhost]
    
    TASK [Reserve a static IP Address] *********************************************************************************************************************************************************************************
    changed: [localhost]
    
    TASK [Create an Instance] *******************************************************************************************************************************************
    changed: [localhost]
    
    PLAY RECAP ********************************************************************************************************************************************************************************************************
    localhost                  : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    

    The GCP instance is shown in Figure 1.

    Creating a GCP instance.
    Figure 1: Creating a GCP instance.

    By following the step-by-step guide, you should now have a good understanding of how to use Ansible to automate the creation of a virtual machine. To learn more about Ansible and access additional resources and guides, including diverse examples and use cases, we recommend visiting Red Hat Ansible Automation Platform.

    What’s next?

    In our next article, we will explore how Ansible Automation Platform further eases the process of creating virtual machines by enabling you to define infrastructure as code, track infrastructure changes, and enforce compliance policies. If you're interested in exploring how to use Ansible Automation Platform on Azure, you can also access the lab. This lab allows you to try Ansible Automation Platform on Azure and learn how it can be used to automate infrastructure deployment.

    Get started with Ansible Automation Platform by exploring interactive hands-on labs. Download Ansible Automation Platform at no cost and begin your automation journey. You can refer to An IT executive's guide to automation e-book for a better understanding of the Ansible Automation Platform.

    Last updated: January 11, 2024

    Related Posts

    • How to create an EC2 instance in AWS using Ansible CLI

    • 5 examples of security automation with Ansible

    • How to create an EC2 instance in AWS using Ansible automation

    • Using Ansible to automate Google Cloud Platform

    Recent Posts

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.