Skip to content

Commit a513064

Browse files
committed
update oci deployment and split terminate plays
1 parent 018dea7 commit a513064

File tree

4 files changed

+183
-67
lines changed

4 files changed

+183
-67
lines changed

olam/block.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
block:
1111
- name: Create block volume
1212
oracle.oci.oci_blockstorage_volume:
13+
config_file_location: "{{ oci_config_file | default(omit) }}"
14+
config_profile_name: "{{ oci_config_section | default(omit) }}"
1315
compartment_id: "{{ my_compartment_id }}"
1416
availability_domain: "{{ my_availability_domain }}"
1517
display_name: "blockvolume-{{ item.value.instance_name | default('instance-'~timestamp) }}"
@@ -27,6 +29,8 @@
2729

2830
- name: Attach the block volume
2931
oracle.oci.oci_compute_volume_attachment:
32+
config_file_location: "{{ oci_config_file | default(omit) }}"
33+
config_profile_name: "{{ oci_config_section | default(omit) }}"
3034
instance_id: "{{ instance_id }}"
3135
type: paravirtualized
3236
volume_id: "{{ volume_id }}"

olam/build.yml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
---
2-
# Copyright (c) 2024 Oracle and/or its affiliates.
2+
# Copyright (c) 2024 2025 Oracle and/or its affiliates.
33
# This software is made available to you under the terms of the Universal Permissive License (UPL), Version 1.0.
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

77
- name: Launch an instance
88
oracle.oci.oci_compute_instance:
9+
config_file_location: "{{ oci_config_file | default(omit) }}"
10+
config_profile_name: "{{ oci_config_section | default(omit) }}"
911
availability_domain: "{{ my_availability_domain }}"
1012
compartment_id: "{{ my_compartment_id }}"
1113
display_name: "{{ item.value.instance_name | default('instance-'~timestamp) }}"
@@ -50,23 +52,33 @@
5052

5153
- name: Set the compute instance id
5254
ansible.builtin.set_fact:
53-
instance_id: "{{ result.instance.id }}"
55+
my_instance_id: "{{ result.instance.id }}"
56+
57+
- name: Add instance id to state file
58+
ansible.builtin.lineinfile:
59+
path: .ansible-state
60+
line: "instance{{ ansible_loop.index0 + 1 }}_ocid: {{ my_instance_id }}"
61+
mode: "0755"
5462

5563
- name: Set the compute instance display_name
5664
ansible.builtin.set_fact:
57-
instance_display_name: "{{ result.instance.display_name }}"
65+
my_instance_display_name: "{{ result.instance.display_name }}"
5866

5967
- name: Get the vnic attachment details of instance
6068
oracle.oci.oci_compute_vnic_attachment_facts:
69+
config_file_location: "{{ oci_config_file | default(omit) }}"
70+
config_profile_name: "{{ oci_config_section | default(omit) }}"
6171
compartment_id: "{{ my_compartment_id }}"
62-
instance_id: "{{ instance_id }}"
72+
instance_id: "{{ my_instance_id }}"
6373
register: result
6474
retries: 10
6575
delay: 30
6676
until: result is not failed
6777

6878
- name: Get vnic details
6979
oracle.oci.oci_network_vnic_facts:
80+
config_file_location: "{{ oci_config_file | default(omit) }}"
81+
config_profile_name: "{{ oci_config_section | default(omit) }}"
7082
id: "{{ result.vnic_attachments[0].vnic_id }}"
7183
register: result
7284
retries: 10
@@ -97,14 +109,14 @@
97109
- name: Print the public and private ip of the newly created instance
98110
ansible.builtin.debug:
99111
msg:
100-
- "Instance name: {{ instance_display_name }}"
112+
- "Instance name: {{ my_instance_display_name }}"
101113
- " public ip: {{ instance_public_ip }}"
102114
- " private ip: {{ instance_private_ip }}"
103115
when: debug_enabled
104116

105117
- name: Add host to in-memory host file
106118
ansible.builtin.add_host:
107-
name: "{{ instance_display_name }}"
119+
name: "{{ my_instance_display_name }}"
108120
groups: "{{ item.value.type }}"
109121
ansible_user: opc
110122
ansible_private_key_file: "{{ lookup('env', 'HOME') + '/.ssh/' + private_key }}"
@@ -143,7 +155,7 @@
143155
mode: "664"
144156
delegate_to: localhost
145157
vars:
146-
instance_name: "{{ instance_display_name }}"
158+
instance_name: "{{ my_instance_display_name }}"
147159
instance_ansible_user: opc
148160
instance_ansible_private_key_file: "{{ lookup('env', 'HOME') + '/.ssh/' + private_key }}"
149161
instance_ansible_ssh_common_args: "-o StrictHostKeyChecking=no"

olam/create_instance.yml

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,10 @@
2626
instance_ocpus: 2
2727
instance_memory: 32
2828
private_key: "id_rsa"
29-
ansible_python_interpreter: "{{ localhost_python_interpreter | default(omit) }}"
3029
debug_enabled: false
3130

3231
tasks:
3332

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-
4433
- name: Get location of oci_config
4534
ansible.builtin.set_fact:
4635
oci_config_file: "{{ lookup('env', 'HOME') + '/.oci/config' }}"
@@ -60,6 +49,8 @@
6049

6150
- name: Get list availbility domains
6251
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) }}"
6354
compartment_id: "{{ my_tenancy_id }}"
6455
region: "{{ my_region_id }}"
6556
register: result
@@ -92,6 +83,18 @@
9283
ansible.builtin.set_fact:
9384
my_compartment_id: "{{ compartment_id | default(env_lookup, true) | default(ini_lookup, true) }}"
9485

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+
9598
- name: Print compartment id
9699
ansible.builtin.debug:
97100
msg: "{{ my_compartment_id }}"
@@ -110,6 +113,8 @@
110113

111114
- name: Create a virtual cloud network
112115
oracle.oci.oci_network_vcn:
116+
config_file_location: "{{ oci_config_file | default(omit) }}"
117+
config_profile_name: "{{ oci_config_section | default(omit) }}"
113118
compartment_id: "{{ my_compartment_id }}"
114119
display_name: "Linuxvirt Virtual Cloud Network"
115120
cidr_blocks: "10.0.0.0/16"
@@ -123,8 +128,16 @@
123128
ansible.builtin.set_fact:
124129
my_vcn_id: "{{ result.vcn.id }}"
125130

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+
126137
- name: Create internet_gateway
127138
oracle.oci.oci_network_internet_gateway:
139+
config_file_location: "{{ oci_config_file | default(omit) }}"
140+
config_profile_name: "{{ oci_config_section | default(omit) }}"
128141
compartment_id: "{{ my_compartment_id }}"
129142
vcn_id: "{{ my_vcn_id }}"
130143
is_enabled: true
@@ -139,8 +152,16 @@
139152
ansible.builtin.set_fact:
140153
my_internet_gateway_id: "{{ result.internet_gateway.id }}"
141154

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+
142161
- name: Create route_table
143162
oracle.oci.oci_network_route_table:
163+
config_file_location: "{{ oci_config_file | default(omit) }}"
164+
config_profile_name: "{{ oci_config_section | default(omit) }}"
144165
compartment_id: "{{ my_compartment_id }}"
145166
vcn_id: "{{ my_vcn_id }}"
146167
display_name: "Linuxvirt Route Table"
@@ -158,6 +179,12 @@
158179
ansible.builtin.set_fact:
159180
my_rt_id: "{{ result.route_table.id }}"
160181

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+
161188
- name: Create ingress rules yaml list
162189
ansible.builtin.template:
163190
src: ingress_security_rules.j2
@@ -192,6 +219,8 @@
192219

193220
- name: Create security_list
194221
oracle.oci.oci_network_security_list:
222+
config_file_location: "{{ oci_config_file | default(omit) }}"
223+
config_profile_name: "{{ oci_config_section | default(omit) }}"
195224
display_name: "Linuxvirt Security List"
196225
compartment_id: "{{ my_compartment_id }}"
197226
vcn_id: "{{ my_vcn_id }}"
@@ -206,8 +235,16 @@
206235
ansible.builtin.set_fact:
207236
my_security_list_id: "{{ result.security_list.id }}"
208237

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+
209244
- name: Create subnet
210245
oracle.oci.oci_network_subnet:
246+
config_file_location: "{{ oci_config_file | default(omit) }}"
247+
config_profile_name: "{{ oci_config_section | default(omit) }}"
211248
compartment_id: "{{ my_compartment_id }}"
212249
vcn_id: "{{ my_vcn_id }}"
213250
cidr_block: "{{ subnet_cidr_block }}"
@@ -225,6 +262,12 @@
225262
ansible.builtin.set_fact:
226263
my_subnet_id: "{{ result.subnet.id }}"
227264

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+
228271
- name: Set subnet domain_name
229272
ansible.builtin.set_fact:
230273
my_subnet_domain_name: "{{ result.subnet.subnet_domain_name }}"
@@ -237,6 +280,8 @@
237280

238281
- name: Get image
239282
oracle.oci.oci_compute_image_facts:
283+
config_file_location: "{{ oci_config_file | default(omit) }}"
284+
config_profile_name: "{{ oci_config_section | default(omit) }}"
240285
compartment_id: "{{ my_compartment_id }}"
241286
operating_system: "{{ os }}"
242287
operating_system_version: "{{ os_version }}"
@@ -260,6 +305,8 @@
260305
- name: Build an instance
261306
ansible.builtin.include_tasks: "build.yml"
262307
loop: "{{ lookup('dict', compute_instances, wantlist=True) }}"
308+
loop_control:
309+
extended: true
263310

264311
- name: Check if instances are available
265312
ansible.builtin.import_playbook: "check_instance_available.yml"
@@ -305,11 +352,9 @@
305352
when: use_olam_pah
306353

307354
- name: Print instances
308-
hosts: all
355+
hosts: all:!localhost
309356
become: true
310-
gather_facts: true
311-
vars_files:
312-
- oci_vars.yml
357+
gather_facts: false
313358

314359
tasks:
315360

@@ -328,48 +373,4 @@
328373
Aborting at this stage requires manual removal of all cloud resources this playbook creates.
329374
330375
- 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

Comments
 (0)