Skip to content

Commit 163dc8c

Browse files
committed
Ensure VM is removed on failure
1 parent db576c1 commit 163dc8c

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
- name: Safely create GCP compute instance
3+
block:
4+
- name: Create compute instance
5+
google.cloud.gcp_compute_instance:
6+
name: "{{ vm_name }}"
7+
project: "{{ gcp_project }}"
8+
auth_kind: "{{ gcp_auth_kind }}"
9+
service_account_file: "{{ gcp_service_account_file }}"
10+
zone: "{{ gcp_zone }}"
11+
machine_type: "{{ vm_machine_type | default('e2-standard-2') }}"
12+
disks:
13+
- auto_delete: true
14+
boot: true
15+
initialize_params:
16+
disk_size_gb: "{{ vm_disk_size | default(20) }}"
17+
source_image: "{{ gcp_source_image }}"
18+
network_interfaces:
19+
- network: "{{ network }}"
20+
access_configs:
21+
- name: External NAT
22+
type: ONE_TO_ONE_NAT
23+
labels: "{{ gcp_default_labels | combine(gcp_extra_labels) }}"
24+
metadata: "{{ gcp_meta_data | default({}) }}"
25+
register: instance_result
26+
rescue:
27+
- name: Remove failed GCP compute instance
28+
google.cloud.gcp_compute_instance:
29+
name: "{{ vm_name }}"
30+
project: "{{ gcp_project }}"
31+
auth_kind: "{{ gcp_auth_kind }}"
32+
service_account_file: "{{ gcp_service_account_file }}"
33+
zone: "{{ gcp_zone }}"
34+
state: absent

ansible/roles/create-vm/tasks/create-gcp-vm.yml

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,8 @@
5151
register: network
5252

5353
- name: Create GCP VM
54-
google.cloud.gcp_compute_instance:
55-
name: "{{ vm_name }}"
56-
project: "{{ gcp_project }}"
57-
auth_kind: "{{ gcp_auth_kind }}"
58-
service_account_file: "{{ gcp_service_account_file }}"
59-
zone: "{{ gcp_zone }}"
60-
machine_type: "{{ vm_machine_type | default('e2-standard-2') }}"
61-
disks:
62-
- auto_delete: true
63-
boot: true
64-
initialize_params:
65-
disk_size_gb: "{{ vm_disk_size | default(20) }}"
66-
source_image: "{{ gcp_source_image }}"
67-
network_interfaces:
68-
- network: "{{ network }}"
69-
access_configs:
70-
- name: External NAT
71-
type: ONE_TO_ONE_NAT
72-
labels: "{{ gcp_default_labels | combine(gcp_extra_labels) }}"
73-
metadata: "{{ gcp_meta_data | default({}) }}"
54+
ansible.builtin.include_tasks:
55+
file: create-gcp-instance.yml
7456

7557
# This looks like a lot, and its mostly because ansible support
7658
# for "breaking" from a loop is not all that great at the moment.

0 commit comments

Comments
 (0)