Terraform VMware naming convention

Hi All,

Just wanted to post a quick question and see if it was even possible for this to be achieved by normal terraform functionality. I wanted my VMs to have the following naming convention(where each of these is an individual VM):
TEST-TS01A
TEST-TS01B
TEST-TS01C
TEST-TS01D
TEST-TS02A
TEST-TS02B
TEST-TS02C
TEST-TS02D
TEST-TS03A
TEST-TS03B
TEST-TS03C
TEST-TS03D

I can easily achieve this via Powershell script, but that seems like a heavy-handed solution. Any advice would be appreciated.

Hi @sanzen193!

In order to answer this, I’d need a little more information on what those names mean. It looks like they are composed from a few different parts:

TEST
TS
02
B

…but without knowing what those components mean and where their values are coming from, it’s impossible to think about ways to construct it.

Naturally you could just make a set of those literal names and create a resource whose instances are based on it, but I assume you have something more dynamic in mind:

locals {
  vm_names = toset([
    "TEST-TS01A",
    "TEST-TS01B",
    "TEST-TS01C",
    # etc, etc
  ])
}

resource "vsphere_virtual_machine" "example" {
  for_each = local.vm_names

  name = each.value
  # ...
}

If the individual parts of what you shared are static and you just want to combine all the combinations dynamically, you could use setproduct to create a shorter variant of the above:

locals {
  vm_count_per_letter = 3
  vm_letters = toset(["A", "B", "C", "D"])
  vm_numbers = range(1, local.vm_count_per_letter + 1)
  vm_names = toset([
    for pair in setproduct(local.vm_numbers, local.vm_letters) :
    format("TEST-TS%02d%s", pair[0], pair[1])
  ])
}

resource "vsphere_virtual_machine" "example" {
  for_each = local.vm_names

  name = each.value
  # ...
}

If the above doesn’t help, please share a little more information about what these names represent and I can try for a more specific example.

Thank you for the response! The amount of vm_names will be dynamic, depending on the amount of VMs in the count. So if I need 8 VMs, it would yield the following:

TEST-TS01A
TEST-TS01B
TEST-TS01C
TEST-TS01D
TEST-TS02A
TEST-TS02B
TEST-TS02C
TEST-TS02D

I wrote this in powershell, just to demonstrate:

$totalVMs = 8
[int]$totalVMs = $totalVMs / 4
$test = (1…$totalVMs)

$count = 1
$innercount = 0
$index = 0
$namingConvention = ‘TEST-TS’
$letters = “A”, “B”, “C”, “D”
$array = @()

foreach ($total in $test){

while($innercount -le 3){
    $leadingcount = "{0:00}" -f $count
    
    $fullname = "$namingConvention$leadingcount" + "$($letters[$index])"
    
    write-host $fullname
    $innercount++
    $index++
    $array+=$fullname 
}
$innercount = 0
$count++
$index = 0 

}

Thanks in advance

To give you some context, I am creating a certain number of VMs using Terraform to give a specific naming convention - which you have seen above. By parsing the number out of the name of the VM, I am assigning it to a specific vLan and assigning a second adapter on each VM to that vLan.

So for example all VMs Test-TS01A through D with have a second adapter on vLan-01. All VMs Test-TS02A through D will be on vLan-02. Hope that helps