|
4 | 4 | # The Universal Permissive License (UPL), Version 1.0 (see COPYING or https://oss.oracle.com/licenses/upl) |
5 | 5 | # See LICENSE.TXT for details. |
6 | 6 |
|
7 | | -- name: Terminate instances and delete OCI resources |
| 7 | +- name: Terminate instances and delete oci resources |
8 | 8 | hosts: localhost |
9 | 9 | vars_files: |
10 | 10 | - default_vars.yml |
11 | 11 |
|
12 | 12 | tasks: |
13 | 13 |
|
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 |
15 | 29 | oracle.oci.oci_compute_instance: |
16 | | - id: "{{ hostvars[item]['instance_ocid'] }}" |
| 30 | + id: "{{ item.value }}" |
17 | 31 | 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.*') }}" |
19 | 39 |
|
20 | 40 | - name: Delete the vlan |
21 | 41 | oracle.oci.oci_network_vlan: |
22 | | - vlan_id: "{{ my_vlan_id }}" |
| 42 | + vlan_id: "{{ ocid_state.vlan_ocid }}" |
23 | 43 | state: absent |
24 | 44 |
|
25 | 45 | - name: Delete network_security_group |
26 | 46 | 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 }}" |
28 | 48 | state: absent |
29 | 49 |
|
30 | 50 | - name: Delete the subnet2 |
31 | 51 | oracle.oci.oci_network_subnet: |
32 | | - id: "{{ my_subnet2_id }}" |
| 52 | + id: "{{ ocid_state.private_subnet_ocid }}" |
33 | 53 | state: absent |
34 | 54 |
|
35 | 55 | - name: Delete the subnet1 |
36 | 56 | oracle.oci.oci_network_subnet: |
37 | | - id: "{{ my_subnet1_id }}" |
| 57 | + id: "{{ ocid_state.public_subnet_ocid }}" |
38 | 58 | state: absent |
39 | 59 |
|
40 | 60 | - name: Delete the security list |
41 | 61 | oracle.oci.oci_network_security_list: |
42 | | - id: "{{ my_security_list_id }}" |
| 62 | + id: "{{ ocid_state.security_list_ocid }}" |
43 | 63 | state: absent |
44 | 64 |
|
45 | 65 | - name: Delete the private route table |
46 | 66 | oracle.oci.oci_network_route_table: |
47 | | - id: "{{ my_private_rt_id }}" |
| 67 | + id: "{{ ocid_state.private_route_table_ocid }}" |
48 | 68 | state: absent |
49 | 69 |
|
50 | 70 | - name: Delete the public route table |
51 | 71 | oracle.oci.oci_network_route_table: |
52 | | - id: "{{ my_public_rt_id }}" |
| 72 | + id: "{{ ocid_state.public_route_table_ocid }}" |
53 | 73 | state: absent |
54 | 74 |
|
55 | 75 | - name: Delete the Service Gateway |
56 | 76 | oracle.oci.oci_network_service_gateway: |
57 | | - id: "{{ my_service_gateway_id }}" |
| 77 | + id: "{{ ocid_state.service_gateway_ocid }}" |
58 | 78 | state: absent |
59 | 79 |
|
60 | 80 | - name: Delete the Internet Gateway |
61 | 81 | oracle.oci.oci_network_internet_gateway: |
62 | | - id: "{{ my_internet_gateway_id }}" |
| 82 | + id: "{{ ocid_state.internet_gateway_ocid }}" |
63 | 83 | state: absent |
64 | 84 |
|
65 | 85 | - name: Delete the VCN |
66 | 86 | oracle.oci.oci_network_vcn: |
67 | | - vcn_id: "{{ my_vcn_id }}" |
| 87 | + vcn_id: "{{ ocid_state.vcn_ocid }}" |
68 | 88 | state: absent |
69 | 89 |
|
| 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 | + |
70 | 96 | - name: Remove artifacts |
71 | 97 | ansible.builtin.file: |
72 | 98 | state: absent |
|
75 | 101 | - oci_vars.yml |
76 | 102 | - buffer |
77 | 103 | - hosts |
| 104 | + - .ansible-state |
| 105 | + when: confirm_removal.user_response | lower in ['y', 'yes'] |
0 commit comments