I have made a vsphere module to deploy some vm’s on vcenter with terraform. I use the code below to create the vms.
The issue I keep having is creating multiple tags per vm, one tag is working fine.
I have the variable vtags, which is a list of strings that I would like to attach to the vm.
When I use vtags[0]. The first tag (webserver1) gets created without an issue. How do I create both tags (webserver1 and nginx1 ) for a vm?
I tried it with count instead of for_each but It seems to defiate me further away to a solution.
Can somebody give some tips to point me in the right direction?
create-vms.tf:
module "vsphere" {
source = "../vsphere"
omgeving = "developmine"
stack_name = "stackab"
vms = {
bla-01 = {
vmname = "test-01"
vmip = "10.222.111.156"
vtemplate = "rhel83-v2"
vtags = [ "webserver1", "nginx1" ]
vcpu = {
develop = "1"
developmine = "1"
}
vmemory = {
develop = "1024"
developmine = "1024"
}
vdisk = {
develop = "20"
developmine = 30
}
}
}
}
variables.tf:
variable "omgeving" {
default=[]
}
variable "stack_name" {
default=[]
}
variable "vmname" {
default=[]
}
variable "vmip" {
default=[]
}
variable "vsphere" {
default=[]
}
variable "vtemplate" {
default=[]
}
variable "naam" {
default=[]
}
variable "rol" {
default=[]
}
variable "vms" {
type = map(object({
vmname = string
vmip = string
vtemplate = string
vtags = string
vmemory = map(string)
vtags = list(string)
vcpu = map(number)
vdisk = map(number)
}))
}
main.tf:
...
resource "vsphere_tag" "tag" {
for_each = var.vms
category_id = data.vsphere_tag_category.iac.id
name = each.value.vtags[1]
}
resource "vsphere_virtual_machine" "vm_linux" {
for_each = var.vms
tags = [vsphere_tag.tag[each.key].id]
...