Skip to content

Commit

Permalink
Merge pull request #86 from Terraform-VMWare-Modules/rc/3.0.0
Browse files Browse the repository at this point in the history
Refactored VM name to include static option
  • Loading branch information
Arman-Keyoumarsi authored Apr 23, 2021
2 parents 150511b + 375dc71 commit c4f25cd
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 31 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module "example-server-windowsvm-advanced" {
instances = 2
vmname = "AdvancedVM"
vmnameformat = "%03d" #To use three decimal with leading zero vmnames will be AdvancedVM001,AdvancedVM002
vmdomain = "somedomain.com"
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 ["",""]
"Second Network Card" = ["", ""]
Expand All @@ -112,7 +112,7 @@ module "example-server-windowsvm-advanced" {
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
vmdns = ["192.168.0.2", "192.168.0.1"]
dns_server_list = ["192.168.0.2", "192.168.0.1"]
vmgateway = "192.168.0.1"
enable_disk_uuid = true
auto_logon = true
Expand Down
4 changes: 2 additions & 2 deletions examples/example-Windows-data_disk.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module "example-server-windowsvm-advanced" {
vmtemp = "TemplateName"
instances = 2
vmname = "AdvancedVM"
vmdomain = "somedomain.com"
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 ["",""]
}
Expand All @@ -34,7 +34,7 @@ module "example-server-windowsvm-advanced" {
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
vmdns = ["192.168.0.2", "192.168.0.1"]
dns_server_list = ["192.168.0.2", "192.168.0.1"]
vmgateway = "192.168.0.1"
enable_disk_uuid = true
orgname = "Terraform-Module"
Expand Down
4 changes: 2 additions & 2 deletions examples/example-linux-Network.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ module "example-server-linuxvm-advanced" {
vmtemp = "TemplateName"
instances = 2
vmname = "AdvancedVM"
vmdomain = "somedomain.com"
domain = "somedomain.com"
ipv4submask = ["24", "8"]
network = {
"Network01" = ["10.13.113.2", "10.13.113.3"] # To use DHCP create Empty list ["",""]
"Network02" = ["", ""] #Second Network will use the DHCP
}
disk_datastore = "vsanDatastore"
vmdns = ["192.168.0.2", "192.168.0.1"]
dns_server_list = ["192.168.0.2", "192.168.0.1"]
vmgateway = "192.168.0.1"
network_type = ["vmxnet3", "vmxnet3"]
}
Expand Down
4 changes: 2 additions & 2 deletions examples/example-linux-depend_on.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ module "example-server-linuxvm-advanced" {
cpu_hot_remove_enabled = true
memory_hot_add_enabled = true
vmname = "AdvancedVM"
vmdomain = "somedomain.com"
domain = "somedomain.com"
ipv4submask = ["24", "8"]
network = {
"Network01" = ["10.13.113.2", "10.13.113.3"] # To use DHCP create Empty list ["",""]
"Network02" = ["", ""] #Second Network will use the DHCP
}
vmdns = ["192.168.0.2", "192.168.0.1"]
dns_server_list = ["192.168.0.2", "192.168.0.1"]
vmgateway = "192.168.0.1"
network_type = ["vmxnet3", "vmxnet3"]
tags = {
Expand Down
42 changes: 29 additions & 13 deletions examples/example-vmname.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Single VM deployment with literal name
module "example-server-single" {
source = "Terraform-VMWare-Modules/vm/vsphere"
version = "Latest X.X.X"
vmtemp = "TemplateName"
instances = 1
vmname = "liternalvmname"
vmrp = "esxi/Resources"
source = "Terraform-VMWare-Modules/vm/vsphere"
version = "Latest X.X.X"
vmtemp = "TemplateName"
staticvmname = "liternalvmname"
vmrp = "esxi/Resources"
network = {
"Name of the Port Group in vSphere" = ["10.13.113.2"]
}
Expand All @@ -14,25 +13,42 @@ module "example-server-single" {
}

# Vmname Output -> liternalvmname
//Sclae out Static VMs
variable "name" {
default = ["staticvmname", "staticvmname01"]
}

module "example-server-single" {
source = "Terraform-VMWare-Modules/vm/vsphere"
for_each = toset(var.name)
version = "Latest X.X.X"
vmtemp = "TemplateName"
staticvmname = "liternalvmname"
vmrp = "esxi/Resources"
network = {
"Name of the Port Group in vSphere" = ["10.13.113.2"]
}
dc = "Datacenter"
datastore = "Data Store name(use datastore_cluster for datastore cluster)"
}
// Example of multiple VM deployment with complex naming standard
# Define Environment Variable to switch between Environments
variable "env" {
default = "dev"
}
module "example-server-multi" {
source = "Terraform-VMWare-Modules/vm/vsphere"
version = "Latest X.X.X"
vmtemp = "TemplateName"
instances = 2
vmname = "advancevm"
source = "Terraform-VMWare-Modules/vm/vsphere"
version = "Latest X.X.X"
vmtemp = "TemplateName"
instances = 2
vmname = "advancevm"
vmnameformat = "%03d${var.env}"
vmrp = "esxi/Resources"
vmrp = "esxi/Resources"
network = {
"Name of the Port Group in vSphere" = ["10.13.113.2", ""]
}
dc = "Datacenter"
datastore = "Data Store name(use datastore_cluster for datastore cluster)"
}

# Vmname Output -> advancevm001dev, advancevm002dev
# Vmname Output -> advancevm001dev, advancevm002dev
14 changes: 7 additions & 7 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ locals {
resource "vsphere_virtual_machine" "vm" {
count = var.instances
depends_on = [var.vm_depends_on]
name = var.instances == 1 ? var.vmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
resource_pool_id = data.vsphere_resource_pool.pool.id
folder = var.vmfolder
Expand Down Expand Up @@ -139,18 +139,18 @@ resource "vsphere_virtual_machine" "vm" {

customize {
dynamic "linux_options" {
for_each = var.is_windows_image ? [] : [1]
for_each = var.is_windows_image ? [] : [1]
content {
host_name = var.instances == 1 ? var.vmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
domain = var.vmdomain
host_name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
domain = var.domain
hw_clock_utc = var.hw_clock_utc
}
}
dynamic "windows_options" {
for_each = var.is_windows_image ? [1] : []
for_each = var.is_windows_image ? [1] : []
content {
computer_name = var.instances == 1 ? var.vmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
computer_name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
admin_password = var.local_adminpass
workgroup = var.workgroup
join_domain = var.windomain
Expand All @@ -173,7 +173,7 @@ resource "vsphere_virtual_machine" "vm" {
ipv4_netmask = "%{if length(var.ipv4submask) == 1}${var.ipv4submask[0]}%{else}${var.ipv4submask[network_interface.key]}%{endif}"
}
}
dns_server_list = var.vmdns
dns_server_list = var.dns_server_list
dns_suffix_list = var.dns_suffix_list
ipv4_gateway = var.vmgateway
}
Expand Down
4 changes: 4 additions & 0 deletions tests/smoke/apply.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
terraform init
terraform fmt
terraform validate
terraform apply -var-file="private.tfvars"
11 changes: 8 additions & 3 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ variable "storage_policy_id" {

###########################################
variable "vmname" {
description = "The name of the virtual machine used to deploy the vms. When only one VM is requested, `vmname` is simply the name of the deployed VM."
description = "The name of the virtual machine used to deploy the vms. This name can scale out based on number of instances and vmnameformat - example can be found under exampel folder"
default = "terraformvm"
}

Expand All @@ -102,6 +102,11 @@ variable "vmnameformat" {
default = "%02d"
}

variable "staticvmname" {
description = "Static name of the virtual machin. When this option is used VM can not scale out using instance variable. You can use for_each outside the module to deploy multiple static vms with different names"
default = null
}

variable "vmtemp" {
description = "Name of the template available in the vSphere."
}
Expand Down Expand Up @@ -144,7 +149,7 @@ variable "vmgateway" {
default = null
}

variable "vmdns" {
variable "dns_server_list" {
type = list(string)
default = null
}
Expand Down Expand Up @@ -244,7 +249,7 @@ variable "hw_clock_utc" {
default = true
}

variable "vmdomain" {
variable "domain" {
description = "default VM domain for linux guest customization."
default = "Development.com"
}
Expand Down

0 comments on commit c4f25cd

Please sign in to comment.