Skip to content

Commit bf9a1ce

Browse files
committed
fix state var and add delete steps
1 parent c412755 commit bf9a1ce

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

olvm/create_instance.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@
365365
- name: Add private subnet id to state file
366366
ansible.builtin.lineinfile:
367367
path: .ansible-state
368-
line: "compartment_ocid: {{ my_subnet2_id }}"
368+
line: "private_subnet_ocid: {{ my_subnet2_id }}"
369369
mode: "0755"
370370

371371
- name: Set private subnet domain_name

olvm/terminate_instance.yml

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,95 @@
44
# The Universal Permissive License (UPL), Version 1.0 (see COPYING or https://oss.oracle.com/licenses/upl)
55
# See LICENSE.TXT for details.
66

7-
- name: Terminate instances and delete OCI resources
7+
- name: Terminate instances and delete oci resources
88
hosts: localhost
99
vars_files:
1010
- default_vars.yml
1111

1212
tasks:
1313

14-
- name: Terminate the instances
14+
- name: Check if ansible state file exists
15+
ansible.builtin.stat:
16+
path: .ansible-state
17+
register: state_exists
18+
19+
- name: Fail if ansible state file does not exist
20+
ansible.builtin.fail:
21+
msg: "Exit instance termination as an instance doesn't exist."
22+
when: not state_exists.stat.exists
23+
24+
- name: Read ansible.state file
25+
ansible.builtin.set_fact:
26+
ocid_state: "{{ lookup('file', '.ansible-state') | from_yaml }}"
27+
28+
- name: Delete the instances
1529
oracle.oci.oci_compute_instance:
16-
id: "{{ hostvars[item]['instance_ocid'] }}"
30+
id: "{{ item.value }}"
1731
state: absent
18-
loop: "{{ groups['engine'] + groups['kvm'] }}"
32+
loop: "{{ ocid_state | dict2items | selectattr('key', 'match', '^instance.*') }}"
33+
34+
- name: Delete the storage volumes
35+
oracle.oci.oci_blockstorage_volume:
36+
volume_id: "{{ item.value }}"
37+
state: absent
38+
loop: "{{ ocid_state | dict2items | selectattr('key', 'match', '^amd-storage.*') }}"
1939

2040
- name: Delete the vlan
2141
oracle.oci.oci_network_vlan:
22-
vlan_id: "{{ my_vlan_id }}"
42+
vlan_id: "{{ ocid_state.vlan_ocid }}"
2343
state: absent
2444

2545
- name: Delete network_security_group
2646
oracle.oci.oci_network_security_group:
27-
network_security_group_id: "{{ my_l2_vlan_nsg_id }}"
47+
network_security_group_id: "{{ ocid_state.l2_nsg_ocid }}"
2848
state: absent
2949

3050
- name: Delete the subnet2
3151
oracle.oci.oci_network_subnet:
32-
id: "{{ my_subnet2_id }}"
52+
id: "{{ ocid_state.private_subnet_ocid }}"
3353
state: absent
3454

3555
- name: Delete the subnet1
3656
oracle.oci.oci_network_subnet:
37-
id: "{{ my_subnet1_id }}"
57+
id: "{{ ocid_state.public_subnet_ocid }}"
3858
state: absent
3959

4060
- name: Delete the security list
4161
oracle.oci.oci_network_security_list:
42-
id: "{{ my_security_list_id }}"
62+
id: "{{ ocid_state.security_list_ocid }}"
4363
state: absent
4464

4565
- name: Delete the private route table
4666
oracle.oci.oci_network_route_table:
47-
id: "{{ my_private_rt_id }}"
67+
id: "{{ ocid_state.private_route_table_ocid }}"
4868
state: absent
4969

5070
- name: Delete the public route table
5171
oracle.oci.oci_network_route_table:
52-
id: "{{ my_public_rt_id }}"
72+
id: "{{ ocid_state.public_route_table_ocid }}"
5373
state: absent
5474

5575
- name: Delete the Service Gateway
5676
oracle.oci.oci_network_service_gateway:
57-
id: "{{ my_service_gateway_id }}"
77+
id: "{{ ocid_state.service_gateway_ocid }}"
5878
state: absent
5979

6080
- name: Delete the Internet Gateway
6181
oracle.oci.oci_network_internet_gateway:
62-
id: "{{ my_internet_gateway_id }}"
82+
id: "{{ ocid_state.internet_gateway_ocid }}"
6383
state: absent
6484

6585
- name: Delete the VCN
6686
oracle.oci.oci_network_vcn:
67-
vcn_id: "{{ my_vcn_id }}"
87+
vcn_id: "{{ ocid_state.vcn_ocid }}"
6888
state: absent
6989

90+
- name: Prompt user to confirm removal of artifacts
91+
ansible.builtin.pause:
92+
prompt: "Are you sure you want to remove these artifacts? (Y/N)"
93+
echo: true
94+
register: confirm_removal
95+
7096
- name: Remove artifacts
7197
ansible.builtin.file:
7298
state: absent
@@ -75,3 +101,5 @@
75101
- oci_vars.yml
76102
- buffer
77103
- hosts
104+
- .ansible-state
105+
when: confirm_removal.user_response | lower in ['y', 'yes']

0 commit comments

Comments
 (0)