|
26 | 26 | instance_ocpus: 2
|
27 | 27 | instance_memory: 32
|
28 | 28 | private_key: "id_rsa"
|
29 |
| - ansible_python_interpreter: "{{ localhost_python_interpreter | default(omit) }}" |
30 | 29 | debug_enabled: false
|
31 | 30 |
|
32 | 31 | tasks:
|
33 | 32 |
|
34 |
| - # - name: Check if state file exists |
35 |
| - # ansible.builtin.stat: |
36 |
| - # path: /tmp/ansible.state |
37 |
| - # register: state_exists |
38 |
| - |
39 |
| - # - name: Fail if state file already exists |
40 |
| - # ansible.builtin.fail: |
41 |
| - # msg: "Exit instance creation as a state file already exists." |
42 |
| - # when: hosts_exists.stat.exists |
43 |
| - |
44 | 33 | - name: Get location of oci_config
|
45 | 34 | ansible.builtin.set_fact:
|
46 | 35 | oci_config_file: "{{ lookup('env', 'HOME') + '/.oci/config' }}"
|
|
60 | 49 |
|
61 | 50 | - name: Get list availbility domains
|
62 | 51 | oracle.oci.oci_identity_availability_domain_facts:
|
| 52 | + config_file_location: "{{ oci_config_file | default(omit) }}" |
| 53 | + config_profile_name: "{{ oci_config_section | default(omit) }}" |
63 | 54 | compartment_id: "{{ my_tenancy_id }}"
|
64 | 55 | region: "{{ my_region_id }}"
|
65 | 56 | register: result
|
|
92 | 83 | ansible.builtin.set_fact:
|
93 | 84 | my_compartment_id: "{{ compartment_id | default(env_lookup, true) | default(ini_lookup, true) }}"
|
94 | 85 |
|
| 86 | + - name: Create oci state file |
| 87 | + ansible.builtin.file: |
| 88 | + path: .ansible-state |
| 89 | + state: touch |
| 90 | + mode: "0755" |
| 91 | + |
| 92 | + - name: Add compartment id to state file |
| 93 | + ansible.builtin.lineinfile: |
| 94 | + path: .ansible-state |
| 95 | + line: "compartment_ocid: {{ my_compartment_id }}" |
| 96 | + mode: "0755" |
| 97 | + |
95 | 98 | - name: Print compartment id
|
96 | 99 | ansible.builtin.debug:
|
97 | 100 | msg: "{{ my_compartment_id }}"
|
|
110 | 113 |
|
111 | 114 | - name: Create a virtual cloud network
|
112 | 115 | oracle.oci.oci_network_vcn:
|
| 116 | + config_file_location: "{{ oci_config_file | default(omit) }}" |
| 117 | + config_profile_name: "{{ oci_config_section | default(omit) }}" |
113 | 118 | compartment_id: "{{ my_compartment_id }}"
|
114 | 119 | display_name: "Linuxvirt Virtual Cloud Network"
|
115 | 120 | cidr_blocks: "10.0.0.0/16"
|
|
123 | 128 | ansible.builtin.set_fact:
|
124 | 129 | my_vcn_id: "{{ result.vcn.id }}"
|
125 | 130 |
|
| 131 | + - name: Add vcn id to state file |
| 132 | + ansible.builtin.lineinfile: |
| 133 | + path: .ansible-state |
| 134 | + line: "vcn_ocid: {{ my_vcn_id }}" |
| 135 | + mode: "0755" |
| 136 | + |
126 | 137 | - name: Create internet_gateway
|
127 | 138 | oracle.oci.oci_network_internet_gateway:
|
| 139 | + config_file_location: "{{ oci_config_file | default(omit) }}" |
| 140 | + config_profile_name: "{{ oci_config_section | default(omit) }}" |
128 | 141 | compartment_id: "{{ my_compartment_id }}"
|
129 | 142 | vcn_id: "{{ my_vcn_id }}"
|
130 | 143 | is_enabled: true
|
|
139 | 152 | ansible.builtin.set_fact:
|
140 | 153 | my_internet_gateway_id: "{{ result.internet_gateway.id }}"
|
141 | 154 |
|
| 155 | + - name: Add internet_gateway id to state file |
| 156 | + ansible.builtin.lineinfile: |
| 157 | + path: .ansible-state |
| 158 | + line: "internet_gateway_ocid: {{ my_internet_gateway_id }}" |
| 159 | + mode: "0755" |
| 160 | + |
142 | 161 | - name: Create route_table
|
143 | 162 | oracle.oci.oci_network_route_table:
|
| 163 | + config_file_location: "{{ oci_config_file | default(omit) }}" |
| 164 | + config_profile_name: "{{ oci_config_section | default(omit) }}" |
144 | 165 | compartment_id: "{{ my_compartment_id }}"
|
145 | 166 | vcn_id: "{{ my_vcn_id }}"
|
146 | 167 | display_name: "Linuxvirt Route Table"
|
|
158 | 179 | ansible.builtin.set_fact:
|
159 | 180 | my_rt_id: "{{ result.route_table.id }}"
|
160 | 181 |
|
| 182 | + - name: Add route table id to state file |
| 183 | + ansible.builtin.lineinfile: |
| 184 | + path: .ansible-state |
| 185 | + line: "route_table_ocid: {{ my_rt_id }}" |
| 186 | + mode: "0755" |
| 187 | + |
161 | 188 | - name: Create ingress rules yaml list
|
162 | 189 | ansible.builtin.template:
|
163 | 190 | src: ingress_security_rules.j2
|
|
192 | 219 |
|
193 | 220 | - name: Create security_list
|
194 | 221 | oracle.oci.oci_network_security_list:
|
| 222 | + config_file_location: "{{ oci_config_file | default(omit) }}" |
| 223 | + config_profile_name: "{{ oci_config_section | default(omit) }}" |
195 | 224 | display_name: "Linuxvirt Security List"
|
196 | 225 | compartment_id: "{{ my_compartment_id }}"
|
197 | 226 | vcn_id: "{{ my_vcn_id }}"
|
|
206 | 235 | ansible.builtin.set_fact:
|
207 | 236 | my_security_list_id: "{{ result.security_list.id }}"
|
208 | 237 |
|
| 238 | + - name: Add security list id to state file |
| 239 | + ansible.builtin.lineinfile: |
| 240 | + path: .ansible-state |
| 241 | + line: "security_list_ocid: {{ my_security_list_id }}" |
| 242 | + mode: "0755" |
| 243 | + |
209 | 244 | - name: Create subnet
|
210 | 245 | oracle.oci.oci_network_subnet:
|
| 246 | + config_file_location: "{{ oci_config_file | default(omit) }}" |
| 247 | + config_profile_name: "{{ oci_config_section | default(omit) }}" |
211 | 248 | compartment_id: "{{ my_compartment_id }}"
|
212 | 249 | vcn_id: "{{ my_vcn_id }}"
|
213 | 250 | cidr_block: "{{ subnet_cidr_block }}"
|
|
225 | 262 | ansible.builtin.set_fact:
|
226 | 263 | my_subnet_id: "{{ result.subnet.id }}"
|
227 | 264 |
|
| 265 | + - name: Add public subnet id to state file |
| 266 | + ansible.builtin.lineinfile: |
| 267 | + path: .ansible-state |
| 268 | + line: "subnet_ocid: {{ my_subnet_id }}" |
| 269 | + mode: "0755" |
| 270 | + |
228 | 271 | - name: Set subnet domain_name
|
229 | 272 | ansible.builtin.set_fact:
|
230 | 273 | my_subnet_domain_name: "{{ result.subnet.subnet_domain_name }}"
|
|
237 | 280 |
|
238 | 281 | - name: Get image
|
239 | 282 | oracle.oci.oci_compute_image_facts:
|
| 283 | + config_file_location: "{{ oci_config_file | default(omit) }}" |
| 284 | + config_profile_name: "{{ oci_config_section | default(omit) }}" |
240 | 285 | compartment_id: "{{ my_compartment_id }}"
|
241 | 286 | operating_system: "{{ os }}"
|
242 | 287 | operating_system_version: "{{ os_version }}"
|
|
260 | 305 | - name: Build an instance
|
261 | 306 | ansible.builtin.include_tasks: "build.yml"
|
262 | 307 | loop: "{{ lookup('dict', compute_instances, wantlist=True) }}"
|
| 308 | + loop_control: |
| 309 | + extended: true |
263 | 310 |
|
264 | 311 | - name: Check if instances are available
|
265 | 312 | ansible.builtin.import_playbook: "check_instance_available.yml"
|
|
305 | 352 | when: use_olam_pah
|
306 | 353 |
|
307 | 354 | - name: Print instances
|
308 |
| - hosts: all |
| 355 | + hosts: all:!localhost |
309 | 356 | become: true
|
310 |
| - gather_facts: true |
311 |
| - vars_files: |
312 |
| - - oci_vars.yml |
| 357 | + gather_facts: false |
313 | 358 |
|
314 | 359 | tasks:
|
315 | 360 |
|
|
328 | 373 | Aborting at this stage requires manual removal of all cloud resources this playbook creates.
|
329 | 374 |
|
330 | 375 | - name: Terminate instances and delete OCI resources
|
331 |
| - hosts: localhost |
332 |
| - vars_files: |
333 |
| - - default_vars.yml |
334 |
| - vars: |
335 |
| - ansible_python_interpreter: "{{ localhost_python_interpreter | default(omit) }}" |
336 |
| - |
337 |
| - tasks: |
338 |
| - |
339 |
| - - name: Terminate the instances |
340 |
| - oracle.oci.oci_compute_instance: |
341 |
| - id: "{{ hostvars[item]['instance_ocid'] }}" |
342 |
| - state: absent |
343 |
| - loop: "{{ groups['all'] | flatten(levels=1) }}" |
344 |
| - |
345 |
| - - name: Delete the subnet |
346 |
| - oracle.oci.oci_network_subnet: |
347 |
| - id: "{{ my_subnet_id }}" |
348 |
| - state: absent |
349 |
| - |
350 |
| - - name: Delete the security list |
351 |
| - oracle.oci.oci_network_security_list: |
352 |
| - id: "{{ my_security_list_id }}" |
353 |
| - state: absent |
354 |
| - |
355 |
| - - name: Delete the route table |
356 |
| - oracle.oci.oci_network_route_table: |
357 |
| - id: "{{ my_rt_id }}" |
358 |
| - state: absent |
359 |
| - |
360 |
| - - name: Delete the Internet Gateway |
361 |
| - oracle.oci.oci_network_internet_gateway: |
362 |
| - id: "{{ my_internet_gateway_id }}" |
363 |
| - state: absent |
364 |
| - |
365 |
| - - name: Delete the VCN |
366 |
| - oracle.oci.oci_network_vcn: |
367 |
| - vcn_id: "{{ my_vcn_id }}" |
368 |
| - state: absent |
369 |
| - |
370 |
| - - name: Remove artifacts |
371 |
| - ansible.builtin.file: |
372 |
| - state: absent |
373 |
| - path: "{{ item }}" |
374 |
| - loop: |
375 |
| - - oci_vars.yml |
| 376 | + ansible.builtin.import_playbook: "terminate_instance.yml" |
0 commit comments