Skip to content

Instantly share code, notes, and snippets.

@8bitnz
Created November 9, 2019 04:54
Show Gist options
  • Save 8bitnz/e4bfe4ed0a875f23cf7f9b26acf78ece to your computer and use it in GitHub Desktop.
Save 8bitnz/e4bfe4ed0a875f23cf7f9b26acf78ece to your computer and use it in GitHub Desktop.
Nelson VMUG vsphere example
# The provider is the system we are connecting to or managing. In this case
# the provider is 'vsphere'. For the vSphere provider, we need to provide the
# vCenter server and credentials to connect. In this example, we are using variables for this
provider "vsphere" {
user = var.vsphere_user
password = var.vsphere_password
vsphere_server = var.vsphere_server
# If you have a self-signed cert
allow_unverified_ssl = true
}
# Terrafrom can use Variables to make your configurations more dynamic, for simplicity, we use
# the three variables below to feed values into the provider.
vsphere_user = "administrator@vsphere.local"
vsphere_password = "yoursecurepassword"
vsphere_server = "vcenterserver.local"
# Terraform is great at provisioning infrastructure, but it doesn't discover existing infrastructure
# out of the box. Below we are using the data type to 'inform' Terrafrom that we have an existing DataCenter
# named A03. We need this, as some of the resources we will create require the datacenter
data "vsphere_datacenter" "datacenter" {
name = "my_dc"
}
# We have two hosts to add to the vCenter, both have ESXi installed, but are yet to be adopted to the
# vCenter. The will be placed into maintenance mode when the are added.
resource "vsphere_host" "host_esx03" {
hostname = "esx03.local"
username = "root"
password = "Password1"
datacenter = "${data.vsphere_datacenter.datacenter.id}"
maintenance = true
}
# The second ESXi host
resource "vsphere_host" "host_esx04" {
hostname = "esx04.local"
username = "root"
password = "Password1"
datacenter = "${data.vsphere_datacenter.datacenter.id}"
maintenance = true
}
# Lastly, we will create our host cluster, add the hosts and enable vSphere HA. The resource seems
# to understand that the ESXi hosts need to be created prior to the cluster creation. Interestingly,
# this creates a state that can no longer be updated.
resource "vsphere_compute_cluster" "compute_cluster" {
name = "my_cluster"
datacenter_id = "${data.vsphere_datacenter.datacenter.id}"
host_system_ids = ["${vsphere_host.host_esx03.id}","${vsphere_host.host_esx04.id}"]
drs_enabled = false
drs_automation_level = "fullyAutomated"
ha_enabled = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment