Skip to content

Commit

Permalink
Merge pull request #152 from damnsam/vmname-fqdn-and-instance-start-n…
Browse files Browse the repository at this point in the history
…umber-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"
  • Loading branch information
Arman-Keyoumarsi committed Jun 22, 2023
2 parents 76a01f9 + 3c56208 commit 00b55db
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
29 changes: 24 additions & 5 deletions examples/example-vmname.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,40 @@ 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", ""]
}
dc = "Datacenter"
datastore = "Data Store name(use datastore_cluster for datastore cluster)"
}
# Vmname Output -> advancevm001.somedomain.com, advancevm002dev.somedomain.com
# Vmname Output -> advancevm005, advancevm006

6 changes: 3 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 + var.vmstartcount)}${var.fqdnvmname == true ? ".${var.domain}" : ""}"

resource_pool_id = var.vmrp != "" ? data.vsphere_resource_pool.pool[0].id : var.vmrpid
folder = var.vmfolder
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion tests/sanity/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
13 changes: 12 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,22 @@ 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
}

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."
}
Expand Down Expand Up @@ -307,7 +318,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"
}

Expand Down

0 comments on commit 00b55db

Please sign in to comment.