├── .gitignore ├── README.md ├── terraform.tfvars ├── main.tf └── variables.tf /.gitignore: -------------------------------------------------------------------------------- 1 | # Local .terraform directories 2 | **/.terraform/* 3 | # .tfstate files 4 | *.tfstate 5 | *.tfstate.* 6 | # .tfvars files 7 | *.tfvars 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # How to deploy VMware vSphere Virtual Machines using Terraform 2 | 3 | Blog Post --> https://medium.com/@gmusumeci/deploying-vmware-vsphere-virtual-machines-with-packer-terraform-d0211f72b7f5 4 | 5 | * **main.tf** --> create virtual machines 6 | 7 | * **variables.tf** --> variables file 8 | 9 | * **terraform.tfvars** --> update vSphere credentials and other settings 10 | -------------------------------------------------------------------------------- /terraform.tfvars: -------------------------------------------------------------------------------- 1 | # ======================== # 2 | # VMware VMs configuration # 3 | # ======================== # 4 | 5 | vm-count = "2" 6 | vm-name = "kopik8snode" 7 | vm-template-name = "CentOS7-Template" 8 | vm-cpu = 2 9 | vm-ram = 4096 10 | vm-guest-id = "centos7_64Guest" 11 | 12 | # ============================ # 13 | # VMware vSphere configuration # 14 | # ============================ # 15 | 16 | # VMware vCenter IP/FQDN 17 | vsphere-vcenter = "10.100.123.10" 18 | 19 | # VMware vSphere username used to deploy the infrastructure 20 | vsphere-user = "administrator@kopicloud.local" 21 | 22 | # VMware vSphere password used to deploy the infrastructure 23 | vsphere-password = "Th1sIsN0tAPassw0rd" 24 | 25 | # Skip the verification of the vCenter SSL certificate (true/false) 26 | vsphere-unverified-ssl = "true" 27 | 28 | # vSphere datacenter name where the infrastructure will be deployed 29 | vsphere-datacenter = "KopiCloud-DC" 30 | 31 | # vSphere cluster name where the infrastructure will be deployed 32 | vsphere-cluster = "KopiCloud-Cluster" 33 | 34 | # vSphere Datastore used to deploy VMs 35 | vm-datastore = "KopiCloud-Datastore-01" 36 | 37 | # vSphere Network used to deploy VMs 38 | vm-network = "VM Network" 39 | 40 | # Linux virtual machine domain name 41 | vm-domain = "kopicloud.local" 42 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | # =================== # 2 | # Deploying VMware VM # 3 | # =================== # 4 | 5 | # Connect to VMware vSphere vCenter 6 | provider "vsphere" { 7 | user = var.vsphere-user 8 | password = var.vsphere-password 9 | vsphere_server = var.vsphere-vcenter 10 | 11 | # If you have a self-signed cert 12 | allow_unverified_ssl = var.vsphere-unverified-ssl 13 | } 14 | 15 | # Define VMware vSphere 16 | data "vsphere_datacenter" "dc" { 17 | name = var.vsphere-datacenter 18 | } 19 | 20 | data "vsphere_datastore" "datastore" { 21 | name = var.vm-datastore 22 | datacenter_id = data.vsphere_datacenter.dc.id 23 | } 24 | 25 | data "vsphere_compute_cluster" "cluster" { 26 | name = var.vsphere-cluster 27 | datacenter_id = data.vsphere_datacenter.dc.id 28 | } 29 | 30 | data "vsphere_network" "network" { 31 | name = var.vm-network 32 | datacenter_id = data.vsphere_datacenter.dc.id 33 | } 34 | 35 | data "vsphere_virtual_machine" "template" { 36 | name = "/${var.vsphere-datacenter}/vm/${var.vsphere-template-folder}/${var.vm-template-name}" 37 | datacenter_id = data.vsphere_datacenter.dc.id 38 | } 39 | 40 | # Create VMs 41 | resource "vsphere_virtual_machine" "vm" { 42 | count = var.vm-count 43 | 44 | name = "${var.vm-name}-${count.index + 1}" 45 | resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id 46 | datastore_id = data.vsphere_datastore.datastore.id 47 | 48 | num_cpus = var.vm-cpu 49 | memory = var.vm-ram 50 | guest_id = var.vm-guest-id 51 | 52 | network_interface { 53 | network_id = data.vsphere_network.network.id 54 | } 55 | 56 | disk { 57 | label = "${var.vm-name}-${count.index + 1}-disk" 58 | size = 25 59 | } 60 | 61 | clone { 62 | template_uuid = data.vsphere_virtual_machine.template.id 63 | 64 | customize { 65 | timeout = 0 66 | 67 | linux_options { 68 | host_name = "node-${count.index + 1}" 69 | domain = var.vm-domain 70 | } 71 | 72 | network_interface {} 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | #===========================# 2 | # VMware vCenter connection # 3 | #===========================# 4 | 5 | variable "vsphere-user" { 6 | type = string 7 | description = "VMware vSphere user name" 8 | } 9 | 10 | variable "vsphere-password" { 11 | type = string 12 | description = "VMware vSphere password" 13 | } 14 | 15 | variable "vsphere-vcenter" { 16 | type = string 17 | description = "VMWare vCenter server FQDN / IP" 18 | } 19 | 20 | variable "vsphere-unverified-ssl" { 21 | type = string 22 | description = "Is the VMware vCenter using a self signed certificate (true/false)" 23 | } 24 | 25 | variable "vsphere-datacenter" { 26 | type = string 27 | description = "VMWare vSphere datacenter" 28 | } 29 | 30 | variable "vsphere-cluster" { 31 | type = string 32 | description = "VMWare vSphere cluster" 33 | default = "" 34 | } 35 | 36 | variable "vsphere-template-folder" { 37 | type = string 38 | description = "Template folder" 39 | default = "Templates" 40 | } 41 | 42 | #================================# 43 | # VMware vSphere virtual machine # 44 | #================================# 45 | 46 | variable "vm-count" { 47 | type = string 48 | description = "Number of VM" 49 | default = 1 50 | } 51 | 52 | variable "vm-name-prefix" { 53 | type = string 54 | description = "Name of VM prefix" 55 | default = "playtftest" 56 | } 57 | 58 | variable "vm-datastore" { 59 | type = string 60 | description = "Datastore used for the vSphere virtual machines" 61 | } 62 | 63 | variable "vm-network" { 64 | type = string 65 | description = "Network used for the vSphere virtual machines" 66 | } 67 | 68 | variable "vm-linked-clone" { 69 | type = string 70 | description = "Use linked clone to create the vSphere virtual machine from the template (true/false). If you would like to use the linked clone feature, your template need to have one and only one snapshot" 71 | default = "false" 72 | } 73 | 74 | variable "vm-cpu" { 75 | type = string 76 | description = "Number of vCPU for the vSphere virtual machines" 77 | default = "2" 78 | } 79 | 80 | variable "vm-ram" { 81 | type = string 82 | description = "Amount of RAM for the vSphere virtual machines (example: 2048)" 83 | } 84 | 85 | variable "vm-name" { 86 | type = string 87 | description = "The name of the vSphere virtual machines and the hostname of the machine" 88 | } 89 | 90 | variable "vm-guest-id" { 91 | type = string 92 | description = "The ID of virtual machines operating system" 93 | } 94 | 95 | variable "vm-template-name" { 96 | type = string 97 | description = "The template to clone to create the VM" 98 | } 99 | 100 | variable "vm-domain" { 101 | type = string 102 | description = "Linux virtual machine domain name for the machine. This, along with host_name, make up the FQDN of the virtual machine" 103 | default = "" 104 | } 105 | --------------------------------------------------------------------------------