diff --git a/README.md b/README.md index dae6b86..7debcd8 100644 --- a/README.md +++ b/README.md @@ -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" = ["", ""] @@ -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 diff --git a/examples/example-Windows-data_disk.tf b/examples/example-Windows-data_disk.tf index c41d24a..373a0ca 100644 --- a/examples/example-Windows-data_disk.tf +++ b/examples/example-Windows-data_disk.tf @@ -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 ["",""] } @@ -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" diff --git a/examples/example-linux-Network.tf b/examples/example-linux-Network.tf index 9f08647..41e6f3c 100644 --- a/examples/example-linux-Network.tf +++ b/examples/example-linux-Network.tf @@ -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"] } diff --git a/examples/example-linux-depend_on.tf b/examples/example-linux-depend_on.tf index 4d3ece7..e54f217 100644 --- a/examples/example-linux-depend_on.tf +++ b/examples/example-linux-depend_on.tf @@ -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 = { diff --git a/examples/example-vmname.tf b/examples/example-vmname.tf index 50513a4..66e2535 100644 --- a/examples/example-vmname.tf +++ b/examples/example-vmname.tf @@ -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"] } @@ -14,20 +13,37 @@ 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", ""] } @@ -35,4 +51,4 @@ source = "Terraform-VMWare-Modules/vm/vsphere" datastore = "Data Store name(use datastore_cluster for datastore cluster)" } -# Vmname Output -> advancevm001dev, advancevm002dev \ No newline at end of file +# Vmname Output -> advancevm001dev, advancevm002dev diff --git a/main.tf b/main.tf index f971a18..60eb827 100644 --- a/main.tf +++ b/main.tf @@ -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 @@ -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 @@ -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 } diff --git a/tests/smoke/apply.sh b/tests/smoke/apply.sh new file mode 100644 index 0000000..5e66bfa --- /dev/null +++ b/tests/smoke/apply.sh @@ -0,0 +1,4 @@ +terraform init +terraform fmt +terraform validate +terraform apply -var-file="private.tfvars" diff --git a/variables.tf b/variables.tf index 156ac9f..7bd4ed4 100644 --- a/variables.tf +++ b/variables.tf @@ -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" } @@ -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." } @@ -144,7 +149,7 @@ variable "vmgateway" { default = null } -variable "vmdns" { +variable "dns_server_list" { type = list(string) default = null } @@ -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" }