From afd258f65c3043345b461faa9df14e3b0d571265 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Thu, 15 Jun 2023 11:09:59 -0400 Subject: [PATCH 1/4] add fqdnvmname functionality --- main.tf | 2 +- variables.tf | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/main.tf b/main.tf index 0fc1bcc..b45e504 100644 --- a/main.tf +++ b/main.tf @@ -80,7 +80,7 @@ locals { resource "vsphere_virtual_machine" "vm" { count = var.instances depends_on = [var.vm_depends_on] - name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + 1) + name = "${var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)}${var.fqdnvmname == true ? ".${var.domain}" : ""}" resource_pool_id = var.vmrp != "" ? data.vsphere_resource_pool.pool[0].id : var.vmrpid folder = var.vmfolder diff --git a/variables.tf b/variables.tf index 22c4c93..6c8fbf2 100644 --- a/variables.tf +++ b/variables.tf @@ -130,6 +130,12 @@ variable "staticvmname" { default = null } +variable "fqdnvmname" { + description = "If true, the vm will be created using domain variable appended" + type = bool + default = false +} + variable "vmtemp" { description = "Name of the template available in the vSphere." } @@ -307,7 +313,7 @@ variable "hw_clock_utc" { } variable "domain" { - description = "default VM domain for linux guest customization." + description = "default VM domain for linux guest customization and fqdn name (if fqdnvmname is true)." default = "Development.com" } From c21de86ca83c55084e3301f3dfa0737aff104f10 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Thu, 15 Jun 2023 11:15:33 -0400 Subject: [PATCH 2/4] Add vmstartcount functionality --- main.tf | 6 +++--- variables.tf | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/main.tf b/main.tf index b45e504..f261727 100644 --- a/main.tf +++ b/main.tf @@ -80,7 +80,7 @@ locals { resource "vsphere_virtual_machine" "vm" { count = var.instances depends_on = [var.vm_depends_on] - name = "${var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)}${var.fqdnvmname == true ? ".${var.domain}" : ""}" + name = "${var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + var.vmstartcount)}${var.fqdnvmname == true ? ".${var.domain}" : ""}" resource_pool_id = var.vmrp != "" ? data.vsphere_resource_pool.pool[0].id : var.vmrpid folder = var.vmfolder @@ -218,7 +218,7 @@ resource "vsphere_virtual_machine" "vm" { dynamic "linux_options" { for_each = var.is_windows_image ? [] : [1] content { - host_name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + 1) + host_name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + var.vmstartcount) domain = var.domain hw_clock_utc = var.hw_clock_utc } @@ -227,7 +227,7 @@ resource "vsphere_virtual_machine" "vm" { dynamic "windows_options" { for_each = var.is_windows_image ? [1] : [] content { - computer_name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + 1) + computer_name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + var.vmstartcount) admin_password = var.local_adminpass workgroup = var.workgroup join_domain = var.windomain diff --git a/variables.tf b/variables.tf index 6c8fbf2..8dac4f0 100644 --- a/variables.tf +++ b/variables.tf @@ -125,6 +125,11 @@ variable "vmnameformat" { default = "%02d" } +variable "vmstartcount" { + description = "vmname start count value. default is set to 1. example: a value of 4 (with default format and 2 instances) will make first instance suffix 04 and second instance suffix 05" + default = 1 +} + 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 From eb2ee104bea93bb4615cdaf8284d70cd742f539c Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Thu, 15 Jun 2023 11:45:52 -0400 Subject: [PATCH 3/4] provide examples for fqdnvmname & vmstartcount --- examples/example-vmname.tf | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/examples/example-vmname.tf b/examples/example-vmname.tf index 76b2a14..228a341 100644 --- a/examples/example-vmname.tf +++ b/examples/example-vmname.tf @@ -54,16 +54,34 @@ module "example-server-multi" { # //Example of appending domain name to vm name -variable "domain" { - default = "somedomain.com" +module "example-server-fqdnvmname" { + source = "Terraform-VMWare-Modules/vm/vsphere" + version = "Latest X.X.X" + vmtemp = "TemplateName" + instances = 2 + vmname = "advancevm" + vmnameformat = "%03d" + domain = "somedomain.com" + fqdnvmname = true + 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)" } -module "example-server-multi" { +# Vmname Output -> advancevm001.somedomain.com, advancevm002.somedomain.com +# +//Example of using a starting number other than "1" for the vmname with multiple instances + +module "example-server-vmstartcount" { source = "Terraform-VMWare-Modules/vm/vsphere" version = "Latest X.X.X" vmtemp = "TemplateName" instances = 2 + vmstartcount = 5 vmname = "advancevm" - vmnameformat = "%03d.${var.domain}" + vmnameformat = "%03d" vmrp = "esxi/Resources" network = { "Name of the Port Group in vSphere" = ["10.13.113.2", ""] @@ -71,4 +89,5 @@ module "example-server-multi" { dc = "Datacenter" datastore = "Data Store name(use datastore_cluster for datastore cluster)" } -# Vmname Output -> advancevm001.somedomain.com, advancevm002dev.somedomain.com +# Vmname Output -> advancevm005, advancevm006 + From 3c56208cea0100f9b82a9bc1532f89e6e43f2772 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Thu, 15 Jun 2023 13:20:22 -0400 Subject: [PATCH 4/4] Update sanity tf for fqdnvmname/vmstartcount vars --- tests/sanity/main.tf | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/sanity/main.tf b/tests/sanity/main.tf index b938959..2687554 100644 --- a/tests/sanity/main.tf +++ b/tests/sanity/main.tf @@ -52,7 +52,10 @@ module "example-server-basic" { datastore = each.value.datastore #starting of static values instances = 2 - vmnameformat = "%03d${var.env}.somedomain.com" + vmstartcount = 5 + vmnameformat = "%03d${var.env}" + domain = "somedomain.com" + fqdnvmname = true vmname = "terraform-sanitytest" annotation = "Terraform Sanity Test" tag_depends_on = [vsphere_tag.tag.id]