Skip to content

Commit 00b55db

Browse files
Merge pull request #152 from damnsam/vmname-fqdn-and-instance-start-number-support
Ability to create VMs using the FQDN as the vm's name as seen in VMware Ability to create multiple instances using a starting number other than "1"
2 parents 76a01f9 + 3c56208 commit 00b55db

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

examples/example-vmname.tf

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,40 @@ module "example-server-multi" {
5454
#
5555
//Example of appending domain name to vm name
5656

57-
variable "domain" {
58-
default = "somedomain.com"
57+
module "example-server-fqdnvmname" {
58+
source = "Terraform-VMWare-Modules/vm/vsphere"
59+
version = "Latest X.X.X"
60+
vmtemp = "TemplateName"
61+
instances = 2
62+
vmname = "advancevm"
63+
vmnameformat = "%03d"
64+
domain = "somedomain.com"
65+
fqdnvmname = true
66+
vmrp = "esxi/Resources"
67+
network = {
68+
"Name of the Port Group in vSphere" = ["10.13.113.2", ""]
69+
}
70+
dc = "Datacenter"
71+
datastore = "Data Store name(use datastore_cluster for datastore cluster)"
5972
}
60-
module "example-server-multi" {
73+
# Vmname Output -> advancevm001.somedomain.com, advancevm002.somedomain.com
74+
#
75+
//Example of using a starting number other than "1" for the vmname with multiple instances
76+
77+
module "example-server-vmstartcount" {
6178
source = "Terraform-VMWare-Modules/vm/vsphere"
6279
version = "Latest X.X.X"
6380
vmtemp = "TemplateName"
6481
instances = 2
82+
vmstartcount = 5
6583
vmname = "advancevm"
66-
vmnameformat = "%03d.${var.domain}"
84+
vmnameformat = "%03d"
6785
vmrp = "esxi/Resources"
6886
network = {
6987
"Name of the Port Group in vSphere" = ["10.13.113.2", ""]
7088
}
7189
dc = "Datacenter"
7290
datastore = "Data Store name(use datastore_cluster for datastore cluster)"
7391
}
74-
# Vmname Output -> advancevm001.somedomain.com, advancevm002dev.somedomain.com
92+
# Vmname Output -> advancevm005, advancevm006
93+

main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ locals {
8080
resource "vsphere_virtual_machine" "vm" {
8181
count = var.instances
8282
depends_on = [var.vm_depends_on]
83-
name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
83+
name = "${var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + var.vmstartcount)}${var.fqdnvmname == true ? ".${var.domain}" : ""}"
8484

8585
resource_pool_id = var.vmrp != "" ? data.vsphere_resource_pool.pool[0].id : var.vmrpid
8686
folder = var.vmfolder
@@ -218,7 +218,7 @@ resource "vsphere_virtual_machine" "vm" {
218218
dynamic "linux_options" {
219219
for_each = var.is_windows_image ? [] : [1]
220220
content {
221-
host_name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
221+
host_name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + var.vmstartcount)
222222
domain = var.domain
223223
hw_clock_utc = var.hw_clock_utc
224224
}
@@ -227,7 +227,7 @@ resource "vsphere_virtual_machine" "vm" {
227227
dynamic "windows_options" {
228228
for_each = var.is_windows_image ? [1] : []
229229
content {
230-
computer_name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
230+
computer_name = var.staticvmname != null ? var.staticvmname : format("${var.vmname}${var.vmnameformat}", count.index + var.vmstartcount)
231231
admin_password = var.local_adminpass
232232
workgroup = var.workgroup
233233
join_domain = var.windomain

tests/sanity/main.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ module "example-server-basic" {
5252
datastore = each.value.datastore
5353
#starting of static values
5454
instances = 2
55-
vmnameformat = "%03d${var.env}.somedomain.com"
55+
vmstartcount = 5
56+
vmnameformat = "%03d${var.env}"
57+
domain = "somedomain.com"
58+
fqdnvmname = true
5659
vmname = "terraform-sanitytest"
5760
annotation = "Terraform Sanity Test"
5861
tag_depends_on = [vsphere_tag.tag.id]

variables.tf

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,22 @@ variable "vmnameformat" {
125125
default = "%02d"
126126
}
127127

128+
variable "vmstartcount" {
129+
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"
130+
default = 1
131+
}
132+
128133
variable "staticvmname" {
129134
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"
130135
default = null
131136
}
132137

138+
variable "fqdnvmname" {
139+
description = "If true, the vm will be created using domain variable appended"
140+
type = bool
141+
default = false
142+
}
143+
133144
variable "vmtemp" {
134145
description = "Name of the template available in the vSphere."
135146
}
@@ -307,7 +318,7 @@ variable "hw_clock_utc" {
307318
}
308319

309320
variable "domain" {
310-
description = "default VM domain for linux guest customization."
321+
description = "default VM domain for linux guest customization and fqdn name (if fqdnvmname is true)."
311322
default = "Development.com"
312323
}
313324

0 commit comments

Comments
 (0)