|
| 1 | +--- |
| 2 | +# Copyright (c) 2024 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: "{{ instance_ocpus }}" |
| 16 | + memory_in_gbs: "{{ instance_memory }}" |
| 17 | + create_vnic_details: |
| 18 | + assign_public_ip: true |
| 19 | + hostname_label: "{{ item.value.instance_name | default('instance-'~timestamp) }}" |
| 20 | + subnet_id: "{{ my_subnet_id }}" |
| 21 | + metadata: |
| 22 | + ssh_authorized_keys: "{{ lookup('file', lookup('env', 'HOME') + '/.ssh/' + private_key + '.pub') }}" |
| 23 | + agent_config: |
| 24 | + is_monitoring_disabled: false |
| 25 | + is_management_disabled: false |
| 26 | + are_all_plugins_disabled: false |
| 27 | + plugins_config: |
| 28 | + - |
| 29 | + name: "OS Management Service Agent" |
| 30 | + desired_state: DISABLED |
| 31 | + register: result |
| 32 | + vars: |
| 33 | + timestamp: "{{ now().strftime('%Y%m%d-%H%M%S') }}" |
| 34 | + retries: 10 |
| 35 | + delay: 30 |
| 36 | + until: result is not failed |
| 37 | + |
| 38 | +- name: Print instance details |
| 39 | + ansible.builtin.debug: |
| 40 | + msg: |
| 41 | + - "Launched a new instance:" |
| 42 | + - "{{ result }}" |
| 43 | + when: debug_enabled |
| 44 | + |
| 45 | +- name: Set the compute instance id |
| 46 | + ansible.builtin.set_fact: |
| 47 | + instance_id: "{{ result.instance.id }}" |
| 48 | + |
| 49 | +- name: Set the compute instance display_name |
| 50 | + ansible.builtin.set_fact: |
| 51 | + instance_display_name: "{{ result.instance.display_name }}" |
| 52 | + |
| 53 | +- name: Get the vnic attachment details of instance |
| 54 | + oracle.oci.oci_compute_vnic_attachment_facts: |
| 55 | + compartment_id: "{{ my_compartment_id }}" |
| 56 | + instance_id: "{{ instance_id }}" |
| 57 | + register: result |
| 58 | + retries: 10 |
| 59 | + delay: 30 |
| 60 | + until: result is not failed |
| 61 | + |
| 62 | +- name: Get vnic details |
| 63 | + oracle.oci.oci_network_vnic_facts: |
| 64 | + id: "{{ result.vnic_attachments[0].vnic_id }}" |
| 65 | + register: result |
| 66 | + retries: 10 |
| 67 | + delay: 30 |
| 68 | + until: result is not failed |
| 69 | + |
| 70 | +- name: Set the instance private ip address |
| 71 | + ansible.builtin.set_fact: |
| 72 | + instance_private_ip: "{{ result.vnic.private_ip }}" |
| 73 | + |
| 74 | +- name: Set the instance public ip address |
| 75 | + ansible.builtin.set_fact: |
| 76 | + instance_public_ip: "{{ result.vnic.public_ip }}" |
| 77 | + |
| 78 | +- name: Add block storage to an instance |
| 79 | + ansible.builtin.include_tasks: "block.yml" |
| 80 | + loop: "{{ query('sequence', 'start=1 end='+(block_count)|string) }}" |
| 81 | + loop_control: |
| 82 | + extended: true |
| 83 | + vars: |
| 84 | + block_devices: |
| 85 | + - b |
| 86 | + - c |
| 87 | + - d |
| 88 | + - e |
| 89 | + - f |
| 90 | + |
| 91 | +- name: Print the public and private ip of the newly created instance |
| 92 | + ansible.builtin.debug: |
| 93 | + msg: |
| 94 | + - "Instance name: {{ instance_display_name }}" |
| 95 | + - " public ip: {{ instance_public_ip }}" |
| 96 | + - " private ip: {{ instance_private_ip }}" |
| 97 | + when: debug_enabled |
| 98 | + |
| 99 | +- name: Add host to in-memory host file |
| 100 | + ansible.builtin.add_host: |
| 101 | + name: "{{ instance_display_name }}" |
| 102 | + groups: "{{ item.value.type }}" |
| 103 | + ansible_user: opc |
| 104 | + ansible_private_key_file: "{{ lookup('env', 'HOME') + '/.ssh/' + private_key }}" |
| 105 | + ansible_ssh_common_args: "-o StrictHostKeyChecking=no" |
| 106 | + ansible_host: "{{ instance_public_ip }}" |
| 107 | + ansible_port: 22 |
| 108 | + instance_ocid: "{{ instance_id }}" |
0 commit comments