-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreate-gc-instance-by-name.yml
89 lines (82 loc) · 2.57 KB
/
create-gc-instance-by-name.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
---
- name: Create GCP resources and then create a GCP instance
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Create a disk mapped from a RHEL-9 image for the VM
google.cloud.gcp_compute_disk:
name: "{{ vm_name }}-disk"
size_gb: 50
source_image: "projects/rhel-cloud/global/images/rhel-9-v20231010"
zone: "{{ gcp_zone }}"
project: "{{ gcp_project }}"
auth_kind: serviceaccount
state: present
register: vmdisk
- name: Create a Google Virtual Private Cloud (VPC) network
google.cloud.gcp_compute_network:
name: network-instance
auto_create_subnetworks: 'false'
project: "{{ gcp_project }}"
auth_kind: serviceaccount
state: present
register: network
- name: Create a subnetwork
google.cloud.gcp_compute_subnetwork:
name: instancenet
region: "{{ gcp_region }}"
network: "{{ network }}"
ip_cidr_range: 172.16.0.0/16
project: "{{ gcp_project }}"
auth_kind: serviceaccount
state: present
register: vpcsubnet
- name: Create a firewall rule to allow SSH
google.cloud.gcp_compute_firewall:
name: allowssh
allowed:
- ip_protocol: tcp
ports:
- '22'
- '80'
- '443'
project: "{{ gcp_project }}"
network: "{{ network }}"
auth_kind: serviceaccount
state: present
register: sshallowed
- name: Create an IPv4 public IP address for the VM
google.cloud.gcp_compute_address:
name: "{{ vm_name }}-ip"
region: "{{ gcp_region }}"
project: "{{ gcp_project }}"
auth_kind: serviceaccount
state: present
register: vmip
- name: Create the VM instances
google.cloud.gcp_compute_instance:
name: "{{ vm_name }}"
machine_type: n2d-standard-2
disks:
- auto_delete: 'true'
boot: 'true'
source: "{{ vmdisk }}"
- auto_delete: 'true'
interface: NVME
type: SCRATCH
initialize_params:
disk_type: local-ssd
labels:
environment: production
network_interfaces:
- network: "{{ network }}"
access_configs:
- name: External NAT
nat_ip: "{{ vmip }}"
type: ONE_TO_ONE_NAT
subnetwork: "{{ vpcsubnet }}"
zone: "{{ gcp_zone }}"
project: "{{ gcp_project }}"
auth_kind: serviceaccount
state: present