|
| 1 | +# Oracle Cloud VMware Solution - Terraform Automation |
| 2 | + |
| 3 | +An OCVS (Oracle Cloud VMware Solution) environment comprises multiple hosts (Bare Metal Servers). |
| 4 | +This means that some operations need to be repeated multiple times |
| 5 | +and therefore it would be easier to automate. |
| 6 | + |
| 7 | +Terraform allows you to automate operations / modifications towards your |
| 8 | +OCVS environment in an easy way. Using the Terraform data sources, you can easily read |
| 9 | +out all servers in your OCVS environment and execute modifications against them |
| 10 | + |
| 11 | +```tf |
| 12 | +#example of getting all Hosts that are part of an OCVS environment |
| 13 | +data oci_ocvp_esxi_hosts export_esxi_hosts{ |
| 14 | + sddc_id = var.SDDC_OCID |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +### New VLAN Assignment to your OCVS environment |
| 19 | + |
| 20 | +If you want to have a new extra VLAN for OCVS environment, you would need to do the following steps |
| 21 | +before you can use the VLAN on your Distributed Switch. |
| 22 | +1. Create the VLAN in the VCN |
| 23 | +2. For every ESXi hosts add a vNIC attached to this VLAN on nic0 |
| 24 | +3. For every ESXi hosts add a vNIC attached to this VLAN on nic3 |
| 25 | + |
| 26 | +Doing this manually can be error prone, so using Terraform is a much easier way. |
| 27 | + |
| 28 | +Here is a terraform example of creating a VLAN and assigning it to every ESXi host. |
| 29 | + |
| 30 | +```tf |
| 31 | +variable region {default = "your_region"} #example: eu-frankfurt-1 |
| 32 | +variable SDDC_OCID {default = "OCID_of_your_SDDC"} |
| 33 | +variable VCN_OCID { default = "OCID_of_your_VCN_That_the_VLAN_will_be_created_into"} |
| 34 | +variable VLAN_Name { default = "VLAN-Name"} |
| 35 | +variable VLAN_CIDR { default = "VLAN_CIDR_RANGE"} #example: 192.168.1.0/24 -> Range needs to be within VCN range |
| 36 | +variable VLAN_TAG_ID { default = 2000} # Change number for unique VLAN ID |
| 37 | +
|
| 38 | +provider oci { |
| 39 | + region = var.region |
| 40 | +} |
| 41 | +
|
| 42 | +#Get VCN information to leverage the same compartment ID |
| 43 | +data "oci_core_vcn" "test_vcn" { |
| 44 | + vcn_id = var.VCN_OCID |
| 45 | +} |
| 46 | +
|
| 47 | +# Create VLAN in the specified VCN |
| 48 | +resource "oci_core_vlan" "new_vlan" { |
| 49 | + cidr_block = var.VLAN_CIDR |
| 50 | + compartment_id = data.oci_core_vcn.test_vcn.compartment_id |
| 51 | + vcn_id = var.VCN_OCID |
| 52 | + display_name =var.VLAN_Name |
| 53 | + vlan_tag = var.VLAN_TAG_ID |
| 54 | +} |
| 55 | +
|
| 56 | +# Get all hosts attached to the OCVS environment |
| 57 | +data oci_ocvp_esxi_hosts export_esxi_hosts { |
| 58 | + sddc_id = var.SDDC_OCID |
| 59 | +} |
| 60 | +
|
| 61 | +# Get VLAN OCID so it can be assigned to the ESXi hosts |
| 62 | +data oci_core_vlan attach_vlan { |
| 63 | + vlan_id = oci_core_vlan.new_vlan.id |
| 64 | +} |
| 65 | +
|
| 66 | +# For Each ESXi hosts create VNIC and attach to VLAN on NIC0 |
| 67 | +resource "oci_core_vnic_attachment" "vnic_attachment-nic0" { |
| 68 | + count = length(data.oci_ocvp_esxi_hosts.export_esxi_hosts.esxi_host_collection) |
| 69 | + create_vnic_details { |
| 70 | + display_name = "${data.oci_core_vlan.attach_vlan.display_name}-Nic0" |
| 71 | + vlan_id = oci_core_vlan.new_vlan.id |
| 72 | + } |
| 73 | + instance_id = data.oci_ocvp_esxi_hosts.export_esxi_hosts.esxi_host_collection[count.index].compute_instance_id |
| 74 | + nic_index = 0 |
| 75 | +} |
| 76 | +
|
| 77 | +# For Each ESXi hosts create VNIC and attach to VLAN on NIC1 |
| 78 | +resource "oci_core_vnic_attachment" "vnic_attachment-nic1" { |
| 79 | + count = length(data.oci_ocvp_esxi_hosts.export_esxi_hosts.esxi_host_collection) |
| 80 | + create_vnic_details { |
| 81 | + display_name = "${data.oci_core_vlan.attach_vlan.display_name}-Nic1" |
| 82 | + vlan_id = oci_core_vlan.new_vlan.id |
| 83 | + } |
| 84 | + instance_id = data.oci_ocvp_esxi_hosts.export_esxi_hosts.esxi_host_collection[count.index].compute_instance_id |
| 85 | + nic_index = 1 |
| 86 | +} |
| 87 | +``` |
0 commit comments