Skip to content

Commit 7ed6d70

Browse files
committed
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.
1 parent 024a626 commit 7ed6d70

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

main.tf

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,14 @@ data "vsphere_virtual_machine" "template" {
4343
}
4444

4545
data "vsphere_tag_category" "category" {
46-
count = var.tags != null ? length(var.tags) : 0
47-
name = keys(var.tags)[count.index]
48-
depends_on = [var.vm_depends_on]
46+
count = var.tags != null ? length(var.tags) : 0
47+
name = keys(var.tags)[count.index]
4948
}
5049

5150
data "vsphere_tag" "tag" {
5251
count = var.tags != null ? length(var.tags) : 0
5352
name = var.tags[keys(var.tags)[count.index]]
5453
category_id = "${data.vsphere_tag_category.category[count.index].id}"
55-
depends_on = [var.vm_depends_on]
5654
}
5755

5856
locals {
@@ -68,7 +66,7 @@ resource "vsphere_virtual_machine" "Linux" {
6866

6967
resource_pool_id = data.vsphere_resource_pool.pool.id
7068
folder = var.vmfolder
71-
tags = data.vsphere_tag.tag[*].id
69+
tags = var.tag_ids != null ? var.tag_ids : data.vsphere_tag.tag[*].id
7270
custom_attributes = var.custom_attributes
7371
annotation = var.annotation
7472
extra_config = var.extra_config
@@ -163,7 +161,7 @@ resource "vsphere_virtual_machine" "Windows" {
163161

164162
resource_pool_id = data.vsphere_resource_pool.pool.id
165163
folder = var.vmfolder
166-
tags = data.vsphere_tag.tag[*].id
164+
tags = var.tag_ids != null ? var.tag_ids : data.vsphere_tag.tag[*].id
167165
custom_attributes = var.custom_attributes
168166
annotation = var.annotation
169167
extra_config = var.extra_config

variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,17 @@ variable "vmdns" {
9090

9191
#Global Customization Variables
9292
variable "tags" {
93-
description = "The names of any tags to attach to this resource. They shoud already exist"
93+
description = "The names of any tags to attach to this resource. They must already exist."
9494
type = map
9595
default = null
9696
}
9797

98+
variable "tag_ids" {
99+
description = "The ids of any tags to attach to this resource. They must already exist."
100+
type = list
101+
default = null
102+
}
103+
98104
variable "custom_attributes" {
99105
description = "Map of custom attribute ids to attribute value strings to set for virtual machine."
100106
type = map

0 commit comments

Comments
 (0)