Terraform automation: vCenter Server

To deploy the vCenter Server in our nested home labs and we want to deploy these lab instances multiple times we need to pre-install and pre-configure the vCenter server a little bit. When we have a fully installed vCenter Server instance we can clone this in each lab instance that we create. In this article: The nested labs' project overview/introduction I explain how many lab instances I can create (or replicate).

There are plenty of blog articles on how to deploy a vCenter Server and there are a few ways to do this. I use the CLI deployment where we have to use a.JSON file with input parameters to deploy the full vCenter Server. I learned this a few years ago from my colleague Wesley van Ede.

So before I started the deploy I prepared the .JSON file:

vcsatemplate.json

CLICK ON EXPAND ===> ON THE RIGHT ===> TO SEE THE OUTPUT (vcsatemplate.json code) ===> :

{
    “__version”: “2.13.0”,
    “__comments": "Sample template to deploy a vCenter Server Appliance with an embedded Platform Services Controller on a vCenter Server instance.”,
    “new_vcsa”: {
        “vc”: {
            “__comments”: [
                “’datacenter’ must end with a datacenter name, and only with a datacenter name. “,
                “’target’ must end with an ESXi hostname, a cluster name, or a resource pool name. “,
                “The item ‘Resources’ must precede the resource pool name. “,
                “All names are case-sensitive. “,
                “For details and examples, refer to template help, i.e. vcsa-deploy {install

Other prerequisites that need to be in place are:

  • Create “L1-APP-MGMT11” NSX-T Segment on NSX-T infra
  • Clone the control server VM and make sure a new DNS zone “lab1.local” on the DNS server
  • Created new A record for new VCSA deployment on the control server

NOTE: Before deploying the vCenter Server (template) the Control Server needs to be online and the DNS + DNS records need to be configured because the vCenter Server needs this for the initial deployment. When the install is done you can turn it off for the full lab deployment (clone) later.

This installation is done using the CLI deployment tool using the command:

E:\vcsa-cli-installer\win32>vcsa-deploy.exe install C:\Scripts\PowerCLI\Terraform\VCSA\vcsatemplate.json —accept-eula —no-ssl-certificate-verification


After deployment, I turned off the VCSA and downgraded the memory from 16 GB to 8 GB.

Now that the vCenter Server (template) is fully installed we are ready to clone it with Terraform. The scripts for cloning the vCenter Server can be found below:

\❯ tree
├── main.tf    
├── terraform.tfvars
├── variables.tf


terraform.tfvars

CLICK ON EXPAND ===> ON THE RIGHT ===> TO SEE THE OUTPUT (terraform.tfvars code) ===> :

vsphere_user = “administrator@vsphere.local"
vsphere_password = “<my vCenter Server Password>"
vsphere_server = "vcsa-01.home.local”
vsphere_datacenter = “HOME”
vsphere_datastore = “vsanDatastore”
vsphere_resource_pool = “Lab1”
vsphere_network = “L1-APP-MGMT11”
vsphere_virtual_machine_template = “vcsa-template”
vsphere_virtual_machine_name = “l1-vcsa”

variables.tf

CLICK ON EXPAND ===> ON THE RIGHT ===> TO SEE THE OUTPUT (variables.tf code) ===> :

# vsphere login account. defaults to admin account
variable "vsphere_user" {
  default = "administrator@vsphere.local”
}

# vsphere account password. empty by default.
variable “vsphere_password” {
  default = “<my vCenter Server Password>” 
}

# vsphere server, defaults to localhost
variable “vsphere_server” {
  default = “vcsa-01.home.local”
}

# vsphere datacenter the virtual machine will be deployed to. empty by default.
variable “vsphere_datacenter” {}

# vsphere resource pool the virtual machine will be deployed to. empty by default.
variable “vsphere_resource_pool” {}

# vsphere datastore the virtual machine will be deployed to. empty by default.
variable “vsphere_datastore” {}

# vsphere network the virtual machine will be connected to. empty by default.
variable “vsphere_network” {}

# vsphere virtual machine template that the virtual machine will be cloned from. empty by default.
variable "vsphere_virtual_machine_template" {}

# the name of the vsphere virtual machine that is created. empty by default.
variable “vsphere_virtual_machine_name” {}

main.tf

CLICK ON EXPAND ===> ON THE RIGHT ===> TO SEE THE OUTPUT (main.tf code) ===> :

provider “vsphere” {
  user           = "${var.vsphere_user}"
  password       = "${var.vsphere_password}”
  vsphere_server = “${var.vsphere_server}”
  allow_unverified_ssl = true
}

data “vsphere_datacenter” “dc” {
  name = “${var.vsphere_datacenter}”
}

data “vsphere_datastore” “datastore” {
  name          = “${var.vsphere_datastore}”
  datacenter_id = “${data.vsphere_datacenter.dc.id}”
}

data “vsphere_resource_pool” “pool” {
  name          = "${var.vsphere_resource_pool}"
  datacenter_id = “${data.vsphere_datacenter.dc.id}”
}

data “vsphere_network” “network” {
  name          = “${var.vsphere_network}”
  datacenter_id = “${data.vsphere_datacenter.dc.id}”
}

data “vsphere_virtual_machine” “template” {
  name          = “${var.vsphere_virtual_machine_template}”
  datacenter_id = “${data.vsphere_datacenter.dc.id}”
}

resource “vsphere_virtual_machine” “cloned_virtual_machine” {
  name             = “${var.vsphere_virtual_machine_name}”
  resource_pool_id = “${data.vsphere_resource_pool.pool.id}”
  datastore_id     = “${data.vsphere_datastore.datastore.id}”
  num_cpus = 4
  memory   = 8192

  #num_cpus = “${data.vsphere_virtual_machine.template.num_cpus}”
  #memory   = “${data.vsphere_virtual_machine.template.memory}”
  guest_id = “${data.vsphere_virtual_machine.template.guest_id}"

  scsi_type = "${data.vsphere_virtual_machine.template.scsi_type}"

  network_interface {
    network_id   = "${data.vsphere_network.network.id}"
    adapter_type = "${data.vsphere_virtual_machine.template.network_interface_types[0]}"
  }

  disk {
    label = "disk0"
    size  = "12"
 #   unit_number = 0
  }

  disk {
    label = "disk1"
    size  = "2"
    unit_number = 1
  }

    disk {
    label = "disk2"
    size  = "25"
    unit_number = 2
  }

    disk {
    label = "disk3"
    size  = "50"
    unit_number = 3
  }

    disk {
    label = "disk4"
    size  = "10"
    unit_number = 4
  }

    disk {
    label = "disk5"
    size  = "10"
    unit_number = 5
  }

    disk {
    label = "disk6"
    size  = "15"
    unit_number = 6
  }

    disk {
    label = “disk7”
    size  = “25”
    unit_number = 7
  }

    disk {
    label = "disk8"
    size  = “1”
    unit_number = 8
  }

    disk {
    label = “disk9”
    size  = “10”
    unit_number = 9
  }

    disk {
    label = “disk10”
    size  = “10”
    unit_number = 10
  }

    disk {
    label = “disk11”
    size  = “100”
    unit_number = 11
  }

    disk {
    label = “disk12”
    size  = “50”
    unit_number = 12
  }

  clone {
    template_uuid = “${data.vsphere_virtual_machine.template.id}”
  }
}

So we are ready to execute the terraform code on a per-directory basis.

Validate your code:

ihoogendoor-a01:#Test iwanhoogendoorn$ tfenv use 0.11.14
[INFO] Switching to v0.11.14
[INFO] Switching completed
ihoogendoor-a01:Test iwanhoogendoorn$ terraform validate


Plan your code:

ihoogendoor-a01:Test iwanhoogendoorn$ terraform plan


Execute your code to implement the Segments:

ihoogendoor-a01:Test iwanhoogendoorn$ terraform apply


When the segments need to be removed again you can revert the implementation:

ihoogendoor-a01:Test iwanhoogendoorn$ terraform destroy


Sources