hellom
there is a couple of time im trying to make this work but i dont succeed my self and need your precious help.
i’m using the plugin GitHub - Terraform-VMWare-Modules/terraform-vsphere-vm: Terraform vSphere module for provisioning Virtual Machines and i try to get multiple elements for the same tag category. It works manually but impossible to make it work with this module.
in the module i define tags like this
tags = {
"site" = "xxx"
"apps" = "docker"
}
and it works but if i want multiple elements in apps category it doesnt works
So far i understand that i have to change the structure to put map of list like this:
tags = {
"site" = ["xxx"]
"apps" = ["nginx", "docker"]
}
but i dont succeed to configure the data to get this informations
data "vsphere_tag_category" "category" {
count = var.tags != null ? length(var.tags) : 0
name = keys(var.tags)[count.index]
depends_on = [var.tag_depends_on]
}
data "vsphere_tag" "tag" {
count = var.tags != null ? length(var.tags) : 0
name = var.tags[keys(var.tags)[count.index]]
category_id = data.vsphere_tag_category.category[count.index].id
#depends_on = [var.tag_depends_on]
I tried to make a flattern in a local variable but i dont make it work
Help will be more than welcome
im getting crazy trying to make it with a map of list, so im thinking that it may be more easier to make a string with a delimiter that i can split after like this:
tags = {
"site" = "xxx"
"apps" = "docker, nginx"
}
then i’m trying something like this:
locals {
tagid = [for item in var.tags: item.id]
}
data "vsphere_tag" "tag" {
for_each = var.tags
name = split(",", each.value[count.index])
category_id = data.vsphere_tag_category.category[count.index].id
I dont understand how to get a count index + for_each for key and values
I may go in the wrong way but i feel its the simpliest solution…
making the variable tag way doesnt seems easier in the and
tags = {
"site" = "xxx"
"apps" = "docker, nginx"
}
Im trying back the one with lists
tags = {
"site" = ["xxx"]
"apps" = ["nginx", "docker"]
}
If i build a local that way:
locals {
test3 = merge([
for key, values in var.tags : {
for value in values :
"${key}-${value}" => {
"cat" = key
"sub" = value
}
}
]...)
}
I succeed to get all the values:
for_each = local.test3
provisioner "local-exec" {
command = <<-EOT
echo ${each.value.cat} - ${each.value.sub}
EOT
but now i dont see how to configure the data objects in order to get this values
data "vsphere_tag_category" "category" {
count = var.tags != null ? length(var.tags) : 0
name = keys(var.tags)[count.index]
depends_on = [var.tag_depends_on]
}
data "vsphere_tag" "tag" {
count = var.tags != null ? length(var.tags) : 0
name = var.tags[keys(var.tags)[count.index]]
category_id = data.vsphere_tag_category.category[count.index].id
depends_on = [var.tag_depends_on]
}
the goal is then to asign vsphere_tag.tag???.id into the tags var in vspehere in order to asign a unique id for each key.value element
Thanks for any help on that
i feel that im very near, i changed the 2 data blocks that way:
data "vsphere_tag_category" "category" {
for_each = local.test3
name = each.value.cat
depends_on = [var.tag_depends_on]
}
data "vsphere_tag" "tag" {
for_each = local.test3
name = each.value.sub
category_id = data.vsphere_tag_category.category[each.key].id
depends_on = [var.tag_depends_on]
}
I suceed to get id with :
for_each = local.test3
provisioner "local-exec" {
command = <<-EOT
echo ${data.vsphere_tag_category.category[each.key].id} ${data.vsphere_tag.tag[each.key].id}
EOT
The probleme is now to assign this into the vm creation module
the original code is :
tags = var.tag_ids != null ? var.tag_ids : data.vsphere_tag.tag[*].id
but i get this error:
│ Error: Unsupported attribute
│
│ on .terraform/modules/vm-gitlab-runner-prod/main.tf line 92, in resource "vsphere_virtual_machine" "vm":
│ 92: tags = var.tag_ids != null ? var.tag_ids : data.vsphere_tag.tag[*].id
│
│ This object does not have an attribute named "id".
Any idea ?