|
| 1 | +--- |
| 2 | +# Copyright (c) 2024 2025 Oracle and/or its affiliates. |
| 3 | +# This software is made available to you under the terms of the Universal Permissive License (UPL), Version 1.0. |
| 4 | +# The Universal Permissive License (UPL), Version 1.0 (see COPYING or https://oss.oracle.com/licenses/upl) |
| 5 | +# See LICENSE.TXT for details. |
| 6 | + |
| 7 | +- name: Launch an instance |
| 8 | + oracle.oci.oci_compute_instance: |
| 9 | + availability_domain: "{{ my_availability_domain }}" |
| 10 | + compartment_id: "{{ my_compartment_id }}" |
| 11 | + name: "{{ item.value.instance_name | default('instance-'~timestamp) }}" |
| 12 | + image_id: "{{ ol_image_id }}" |
| 13 | + shape: "{{ instance_shape }}" |
| 14 | + shape_config: |
| 15 | + ocpus: "{{ item.value.instance_ocpus }}" |
| 16 | + memory_in_gbs: "{{ item.value.instance_memory }}" |
| 17 | + create_vnic_details: |
| 18 | + assign_public_ip: true |
| 19 | + hostname_label: "{{ item.value.instance_name | default('instance-'~timestamp) }}" |
| 20 | + display_name: "public" |
| 21 | + subnet_id: "{{ my_subnet1_id }}" |
| 22 | + metadata: |
| 23 | + ssh_authorized_keys: "{{ lookup('file', lookup('env', 'HOME') + '/.ssh/' + private_key + '.pub') }}" |
| 24 | + agent_config: |
| 25 | + is_monitoring_disabled: false |
| 26 | + is_management_disabled: false |
| 27 | + are_all_plugins_disabled: false |
| 28 | + plugins_config: |
| 29 | + - |
| 30 | + name: "OS Management Service Agent" |
| 31 | + desired_state: DISABLED |
| 32 | + key_by: [compartment_id, availability_domain, display_name] |
| 33 | + register: result |
| 34 | + vars: |
| 35 | + timestamp: "{{ now().strftime('%Y%m%d-%H%M%S') }}" |
| 36 | + retries: 10 |
| 37 | + delay: 30 |
| 38 | + until: result is not failed |
| 39 | + |
| 40 | +- name: Print instance details |
| 41 | + ansible.builtin.debug: |
| 42 | + msg: |
| 43 | + - "Launched a new instance:" |
| 44 | + - "{{ result }}" |
| 45 | + when: debug_enabled |
| 46 | + |
| 47 | +- name: Set the compute instance id |
| 48 | + ansible.builtin.set_fact: |
| 49 | + my_instance_id: "{{ result.instance.id }}" |
| 50 | + |
| 51 | +- name: Set the compute instance display_name |
| 52 | + ansible.builtin.set_fact: |
| 53 | + instance_display_name: "{{ result.instance.display_name }}" |
| 54 | + |
| 55 | +# - name: Get the primary vnic attachment details of instance |
| 56 | +# oracle.oci.oci_compute_vnic_attachment_facts: |
| 57 | +# compartment_id: "{{ my_compartment_id }}" |
| 58 | +# instance_id: "{{ my_instance_id }}" |
| 59 | +# register: result |
| 60 | +# retries: 10 |
| 61 | +# delay: 30 |
| 62 | +# until: result is not failed |
| 63 | + |
| 64 | +# - name: Get primary vnic details |
| 65 | +# oracle.oci.oci_network_vnic_facts: |
| 66 | +# vnic_id: "{{ result.vnic_attachments[0].vnic_id }}" |
| 67 | +# register: result |
| 68 | +# retries: 10 |
| 69 | +# delay: 30 |
| 70 | +# until: result is not failed |
| 71 | + |
| 72 | +- name: Set the instance primary private ip address |
| 73 | + ansible.builtin.set_fact: |
| 74 | + instance_private_ip: "{{ result.instance.primary_private_ip }}" |
| 75 | + |
| 76 | +- name: Set the instance primary public ip address |
| 77 | + ansible.builtin.set_fact: |
| 78 | + instance_public_ip: "{{ result.instance.primary_public_ip }}" |
| 79 | + |
| 80 | +- name: Add secondary private subnet vnic_attachment |
| 81 | + when: item.value.type == "engine" |
| 82 | + block: |
| 83 | + - name: Create subnet2 vnic_attachment |
| 84 | + oracle.oci.oci_compute_vnic_attachment: |
| 85 | + compartment_id: "{{ my_compartment_id }}" |
| 86 | + create_vnic_details: |
| 87 | + assign_public_ip: false |
| 88 | + assign_private_dns_record: true |
| 89 | + display_name: "vdsm" |
| 90 | + hostname_label: "vdsm" |
| 91 | + subnet_id: "{{ my_subnet2_id }}" |
| 92 | + display_name: "vdsm" |
| 93 | + instance_id: "{{ my_instance_id }}" |
| 94 | + key_by: [compartment_id, display_name] |
| 95 | + register: engine_subnet2 |
| 96 | + retries: 10 |
| 97 | + delay: 30 |
| 98 | + until: engine_subnet2 is not failed |
| 99 | + |
| 100 | + - name: Print vnic_attachment for subnet2 |
| 101 | + ansible.builtin.debug: |
| 102 | + var: engine_subnet2 |
| 103 | + when: debug_enabled |
| 104 | + |
| 105 | +- name: Add secondary private subnet vnic_attachment |
| 106 | + when: item.value.type == "kvm" |
| 107 | + block: |
| 108 | + - name: Create subnet2 vnic_attachment |
| 109 | + oracle.oci.oci_compute_vnic_attachment: |
| 110 | + compartment_id: "{{ my_compartment_id }}" |
| 111 | + create_vnic_details: |
| 112 | + assign_public_ip: false |
| 113 | + assign_private_dns_record: true |
| 114 | + display_name: "vdsm{{ item.value.instance_name[-2:] }}" |
| 115 | + hostname_label: "vdsm{{ item.value.instance_name[-2:] }}" |
| 116 | + subnet_id: "{{ my_subnet2_id }}" |
| 117 | + display_name: "vdsm{{ item.value.instance_name[-2:] }}" |
| 118 | + instance_id: "{{ my_instance_id }}" |
| 119 | + key_by: [compartment_id, display_name] |
| 120 | + register: kvm_subnet2 |
| 121 | + retries: 10 |
| 122 | + delay: 30 |
| 123 | + until: kvm_subnet2 is not failed |
| 124 | + |
| 125 | + - name: Print vnic_attachment for subnet2 |
| 126 | + ansible.builtin.debug: |
| 127 | + var: kvm_subnet2 |
| 128 | + when: debug_enabled |
| 129 | + |
| 130 | +- name: Add vlan vnic to instance |
| 131 | + when: item.value.type == "kvm" |
| 132 | + block: |
| 133 | + - name: Create vlan vnic_attachment |
| 134 | + oracle.oci.oci_compute_vnic_attachment: |
| 135 | + compartment_id: "{{ my_compartment_id }}" |
| 136 | + create_vnic_details: |
| 137 | + assign_public_ip: false |
| 138 | + display_name: "l2-vm-network" |
| 139 | + vlan_id: "{{ my_vlan_id }}" |
| 140 | + display_name: "l2-vm-network" |
| 141 | + instance_id: "{{ my_instance_id }}" |
| 142 | + register: kvm_vlan |
| 143 | + retries: 10 |
| 144 | + delay: 30 |
| 145 | + until: kvm_vlan is not failed |
| 146 | + |
| 147 | + - name: Print vnic_attachment for vlan |
| 148 | + ansible.builtin.debug: |
| 149 | + var: kvm_vlan |
| 150 | + when: debug_enabled |
| 151 | + |
| 152 | +- name: Add block volumes for vm storage domains |
| 153 | + ansible.builtin.include_tasks: create_block_storage.yml |
| 154 | + loop: |
| 155 | + - "amd-storage-domain-01" |
| 156 | + - "amd-storage-domain-02" |
| 157 | + loop_control: |
| 158 | + loop_var: storage_name |
| 159 | + when: |
| 160 | + - item.value.type == "kvm" |
| 161 | + |
| 162 | +# - name: Add shared block volume to kvm nodes for vm storage |
| 163 | +# when: |
| 164 | +# - item.value.type == "kvm" |
| 165 | +# - add_vm_block_storage |
| 166 | +# block: |
| 167 | +# - name: Create block volume for vm storage |
| 168 | +# oracle.oci.oci_blockstorage_volume: |
| 169 | +# compartment_id: "{{ my_compartment_id }}" |
| 170 | +# availability_domain: "{{ my_availability_domain }}" |
| 171 | +# display_name: "amd-storage-domain-01" |
| 172 | +# size_in_gbs: "{{ blk_volume_size_in_gbs }}" |
| 173 | +# key_by: [compartment_id, display_name] |
| 174 | +# register: kvm_create_block |
| 175 | +# retries: 10 |
| 176 | +# delay: 30 |
| 177 | +# until: kvm_create_block is not failed |
| 178 | + |
| 179 | +# - name: Set the block storage block volume id |
| 180 | +# ansible.builtin.set_fact: |
| 181 | +# volume_id: "{{ kvm_create_block.volume.id }}" |
| 182 | + |
| 183 | +# - name: Attach shared block volume for vm storage |
| 184 | +# oracle.oci.oci_compute_volume_attachment: |
| 185 | +# instance_id: "{{ my_instance_id }}" |
| 186 | +# type: paravirtualized |
| 187 | +# volume_id: "{{ volume_id }}" |
| 188 | +# compartment_id: "{{ my_compartment_id }}" |
| 189 | +# is_read_only: false |
| 190 | +# is_shareable: true |
| 191 | +# register: kvm_add_block |
| 192 | +# retries: 10 |
| 193 | +# delay: 30 |
| 194 | +# until: kvm_add_block is not failed |
| 195 | + |
| 196 | +- name: Print the public and private ip of the newly created instance |
| 197 | + ansible.builtin.debug: |
| 198 | + msg: |
| 199 | + - "Instance name: {{ instance_display_name }}" |
| 200 | + - " public ip: {{ instance_public_ip }}" |
| 201 | + - " private ip: {{ instance_private_ip }}" |
| 202 | + when: debug_enabled |
| 203 | + |
| 204 | +- name: Add host to in-memory host file |
| 205 | + ansible.builtin.add_host: |
| 206 | + name: "{{ instance_display_name }}" |
| 207 | + groups: "{{ item.value.type }}" |
| 208 | + ansible_user: opc |
| 209 | + ansible_private_key_file: "{{ lookup('env', 'HOME') + '/.ssh/' + private_key }}" |
| 210 | + ansible_ssh_common_args: "-o StrictHostKeyChecking=no" |
| 211 | + ansible_host: "{{ instance_public_ip }}" |
| 212 | + ansible_port: 22 |
| 213 | + instance_ocid: "{{ my_instance_id }}" |
| 214 | + |
| 215 | +- name: Create host ini file |
| 216 | + ansible.builtin.lineinfile: |
| 217 | + path: hosts |
| 218 | + regexp: '^\[{{ host_group }}' |
| 219 | + line: "[{{ host_group }}]" |
| 220 | + create: true |
| 221 | + mode: "0664" |
| 222 | + delegate_to: localhost |
| 223 | + loop: |
| 224 | + - engine |
| 225 | + - kvm |
| 226 | + loop_control: |
| 227 | + loop_var: host_group |
| 228 | + |
| 229 | +- name: Add host to ini host file |
| 230 | + ansible.builtin.lineinfile: |
| 231 | + path: hosts |
| 232 | + regexp: '^{{ instance_name }}' |
| 233 | + line: >- |
| 234 | + {{ instance_name }} |
| 235 | + ansible_host={{ instance_ansible_host }} |
| 236 | + ansible_user={{ instance_ansible_user }} |
| 237 | + ansible_private_key_file={{ instance_ansible_private_key_file }} |
| 238 | + ansible_ssh_common_args={{ instance_ansible_ssh_common_args | quote }} |
| 239 | + insertafter: '^\[{{ item.value.type }}\]$' |
| 240 | + create: true |
| 241 | + mode: "664" |
| 242 | + delegate_to: localhost |
| 243 | + vars: |
| 244 | + instance_name: "{{ instance_display_name }}" |
| 245 | + instance_ansible_user: opc |
| 246 | + instance_ansible_private_key_file: "{{ lookup('env', 'HOME') + '/.ssh/' + private_key }}" |
| 247 | + instance_ansible_ssh_common_args: "-o StrictHostKeyChecking=no" |
| 248 | + instance_ansible_host: "{{ instance_public_ip }}" |
| 249 | + instance_ansible_port: 22 |
0 commit comments