How to avoid that Terraform wipe vSphere Tags on VM when importing an existing VM

297 views Asked by At

I've got a question related to Terraform & the vSphere provider (any versions) related to importing VMs that are tagged with vSphere Tags, considering that the number of tags and their categories varies from one VM to another and that I'd like to avoid to have a specific tf template per imported VMs... Let's take a concrete example:

  • VM0 is having no Tags
  • VM1 is having one Tag T1 of category 1
  • VM2 is having two Tags T1 & T2 of respective category 1 & 2 I only care about tag Category 1 in my solution (this means I may want to tag a VM with a tag of category 1 for special purpose). So, my problem is that if I use a template with one Tag category & one Tag in the tf template that I use for importing the VM
...
data "vsphere_tag_category" "category1" {
  name = "${var.vm_myuse_category_tag}"
}
data "vsphere_tag" "tag1" {
  name        = "${var.vm_myuse_tag}"
  category_id = "${data.vsphere_tag_category.category1.id}"
}
...
resource "vsphere_virtual_machine" "vm" {
  name                 = "${var.name}"
  ...
  tags = [
    "${data.vsphere_tag.tag1.id}"
  ]
}

Import of all 3 VMs will work but when I'm trying to modify, let say the memory size of the VM, then the plan & apply will:

  • fail for VM0
  • work for VM1
  • wipe the second tag for VM2

Would someone have any hints on how to handle this situation ? Thx beforehand

0

There are 0 answers