From 7ed6d70c9d8f2cbef2402e4239bc35250a7bccff Mon Sep 17 00:00:00 2001 From: Tobias Rueetschi Date: Wed, 8 Jul 2020 13:50:39 +0200 Subject: [PATCH 1/2] data lazy loading isn't a good idea. If data sources are loaded lazy, terraform will always ask for a change, even there is none, as it doesn't know the data objects. Defining another variable tag_ids and add them directly to the vm instead of using a data source is the better way, if the tags are created with terraform. --- main.tf | 10 ++++------ variables.tf | 8 +++++++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/main.tf b/main.tf index 3f2b9c9..e9d700b 100644 --- a/main.tf +++ b/main.tf @@ -43,16 +43,14 @@ data "vsphere_virtual_machine" "template" { } data "vsphere_tag_category" "category" { - count = var.tags != null ? length(var.tags) : 0 - name = keys(var.tags)[count.index] - depends_on = [var.vm_depends_on] + count = var.tags != null ? length(var.tags) : 0 + name = keys(var.tags)[count.index] } 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.vm_depends_on] } locals { @@ -68,7 +66,7 @@ resource "vsphere_virtual_machine" "Linux" { resource_pool_id = data.vsphere_resource_pool.pool.id folder = var.vmfolder - tags = data.vsphere_tag.tag[*].id + tags = var.tag_ids != null ? var.tag_ids : data.vsphere_tag.tag[*].id custom_attributes = var.custom_attributes annotation = var.annotation extra_config = var.extra_config @@ -163,7 +161,7 @@ resource "vsphere_virtual_machine" "Windows" { resource_pool_id = data.vsphere_resource_pool.pool.id folder = var.vmfolder - tags = data.vsphere_tag.tag[*].id + tags = var.tag_ids != null ? var.tag_ids : data.vsphere_tag.tag[*].id custom_attributes = var.custom_attributes annotation = var.annotation extra_config = var.extra_config diff --git a/variables.tf b/variables.tf index da17b90..0a50b8a 100644 --- a/variables.tf +++ b/variables.tf @@ -90,11 +90,17 @@ variable "vmdns" { #Global Customization Variables variable "tags" { - description = "The names of any tags to attach to this resource. They shoud already exist" + description = "The names of any tags to attach to this resource. They must already exist." type = map default = null } +variable "tag_ids" { + description = "The ids of any tags to attach to this resource. They must already exist." + type = list + default = null +} + variable "custom_attributes" { description = "Map of custom attribute ids to attribute value strings to set for virtual machine." type = map From 92bd720c8f1fb31671d93ef243892c2c6d62d62a Mon Sep 17 00:00:00 2001 From: tr Date: Mon, 20 Jul 2020 08:17:18 +0200 Subject: [PATCH 2/2] Add a variable for data vsphere_tag depends_on --- main.tf | 6 ++++-- variables.tf | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/main.tf b/main.tf index e9d700b..684b529 100644 --- a/main.tf +++ b/main.tf @@ -43,14 +43,16 @@ data "vsphere_virtual_machine" "template" { } data "vsphere_tag_category" "category" { - count = var.tags != null ? length(var.tags) : 0 - name = keys(var.tags)[count.index] + 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] } locals { diff --git a/variables.tf b/variables.tf index 0a50b8a..2988058 100644 --- a/variables.tf +++ b/variables.tf @@ -351,3 +351,9 @@ variable "vm_depends_on" { type = any default = null } + +variable "tag_depends_on" { + description = "Add any external depend on module here like tag_depends_on = [vsphere_tag.foo.id]" + type = any + default = null +}