Skip to content

Instantly share code, notes, and snippets.

@SirPhemmiey
Last active April 30, 2023 18:27
Show Gist options
  • Save SirPhemmiey/81f21d3e494c1ab4d84854794f1856e3 to your computer and use it in GitHub Desktop.
Save SirPhemmiey/81f21d3e494c1ab4d84854794f1856e3 to your computer and use it in GitHub Desktop.
Basic Ansible code to create a VM instance on GCP
- import_playbook: requirements.yml
- name: Provision a GCP VM
hosts: localhost
gather_facts: false
vars:
gcp_cred_file: <path/to/serviceaccount/json/file>
gcp_project: <project-id>
machine_type: "e2-medium"
gcp_cred_kind: serviceaccount
instance_name: "my-vm-instance"
zone: "us-central1-a"
region: "us-central1"
machine_type: "e2-medium"
image: "https://www.googleapis.com/compute/v1/projects/rocky-linux-cloud/global/images/rocky-linux-8-optimized-gcp-v20230411"
tasks:
- name: Create an external address associated with the 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: Create the GCP VM
gcp_compute_instance:
name: "{{ instance_name }}"
machine_type: "{{ machine_type }}"
zone: "{{ zone }}"
project: "{{ gcp_project }}"
service_account_file: "{{ gcp_cred_file }}"
auth_kind: "{{ gcp_cred_kind }}"
disks:
- auto_delete: true
boot: true
initialize_params:
source_image: "{{ image }}"
network_interfaces:
- access_configs: # if you don't add this then the VM instance will have no external address attached to it
- name: External NAT
nat_ip: "{{ gce_ip }}"
type: ONE_TO_ONE_NAT
register: result
- name: Print the VM's IP address
debug:
var: gce_ip.address
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment