Terraform-VMWare-Modules / terraform-vsphere-vm

Terraform vSphere module for provisioning Virtual Machines
https://registry.terraform.io/modules/Terraform-VMWare-Modules/vm/vsphere/
MIT License
167 stars 175 forks source link

Terraform vSphere Module

Terraform Version TF Registry Changelog License: MIT

For Virtual Machine Provisioning with (Linux/Windows) customization. Based on Terraform v0.13 and up, this module includes most of the advanced features available in resource vsphere_virtual_machine.

Deploys (Single/Multiple) Virtual Machines to your vSphere environment

This Terraform module deploys single or multiple virtual machines of type (Linux/Windows) with the following features:

Note: For the module to work, it needs several required variables corresponding to existing resources in vSphere. Please refer to the variable section for the list of required variables.

Getting started

The following example contains the bare minimum options to be configured for (Linux/Windows) VM deployment. You can choose between Windows and Linux customization by simply using the is_windows_image boolean switch.

You can also download the entire module and use your predefined variables to map your entire vSphere environment and use it within this module.

First, create a main.tf file.

Next, copy the code below and fill in the required variables.

# Configure the VMware vSphere Provider
provider "vsphere" {
  user           = "fill"
  password       = "fill"
  vsphere_server = "fill"

  # if you have a self-signed cert
  allow_unverified_ssl = true
}

# Deploy 2 linux VMs
module "example-server-linuxvm" {
  source    = "Terraform-VMWare-Modules/vm/vsphere"
  version   = "X.X.X"
  vmtemp    = "VM Template Name (Should Alrerady exist)"
  instances = 2
  vmname    = "example-server-linux"
  vmrp      = "esxi/Resources - or name of a resource pool"
  network = {
    "Name of the Port Group in vSphere" = ["10.13.113.2", "10.13.113.3"] # To use DHCP create Empty list ["",""]; You can also use a CIDR annotation;
  }
  vmgateway = "10.13.113.1"
  dc        = "Datacenter"
  datastore = "Data Store name(use datastore_cluster for datastore cluster)"
}

Finally, run

terraform run

Advanced Usage

The module includes several option switches, which you can use to enable various VM provisioning features.

Note: When deploying a windows server in WorkGroup, we recommend keeping the Local Admin password set to its default and change it later via a script. Unfortunately, Terraform will re-deploy the entire server if you change the local admin password.

Below is an example of windows deployment with some of the available feature sets. For a complete list of available features, please refer to variable.tf

module "example-server-windowsvm-advanced" {
  source            = "Terraform-VMWare-Modules/vm/vsphere"
  version           = "X.X.X"
  dc                = "Datacenter"
  vmrp              = "cluster/Resources" #Works with ESXi/Resources
  vmfolder          = "Cattle"
  datastore_cluster = "Datastore Cluster" #You can use datastore variable instead
  vmtemp            = "TemplateName"
  instances         = 2
  vmname            = "AdvancedVM"
  vmnameformat      = "%03d" #To use three decimal with leading zero vmnames will be AdvancedVM001,AdvancedVM002
  domain            = "somedomain.com"
  network = {
    "Name of the Port Group in vSphere" = ["10.13.113.2", "10.13.113.3"] # To use DHCP create Empty list ["",""]; You can also use a CIDR annotation;
    "Second Network Card"               = ["", ""]
  }
  ipv4submask  = ["24", "8"]
  network_type = ["vmxnet3", "vmxnet3"]
  tags = {
    "terraform-test-category" = "terraform-test-tag"
  }
  data_disk = {
    disk1 = {
      size_gb                   = 30,
      thin_provisioned          = false,
      data_disk_scsi_controller = 0,
    },
    disk2 = {
      size_gb                   = 70,
      thin_provisioned          = true,
      data_disk_scsi_controller = 1,
      datastore_id              = "datastore-90679"
    }
  }
  scsi_bus_sharing = "physicalSharing" // The modes are physicalSharing, virtualSharing, and noSharing
  scsi_type        = "lsilogic"        // Other acceptable value "pvscsi"
  scsi_controller  = 0                 // This will assign OS disk to controller 0
  dns_server_list  = ["192.168.0.2", "192.168.0.1"]
  enable_disk_uuid = true
  vmgateway        = "192.168.0.1"
  auto_logon       = true
  run_once         = ["command01", "command02"] // You can also run Powershell commands
  orgname          = "Terraform-Module"
  workgroup        = "Module-Test"
  is_windows_image = true
  firmware         = "efi"
  local_adminpass  = "Password@Strong"
}

output "vmnames" {
  value = module.example-server-windowsvm-advanced.VM
}

output "vmnameswip" {
  value = module.example-server-windowsvm-advanced.ip
}

Contributing

This module is the work of many contributors. We appreciate your help!

To contribute, please read the contribution guidelines

License

MIT