Terraform -为ignore_changes添加异常



我目前有一个模块,其中包含许多可配置的属性块

resource "example_resource" "instance1" {

block1 {
property1 = var.variable1      # Should generate a diff if changed
property2 = var.variable2      # Ignore
property3 = var.variable3      # Ignore
....
....
propertyN = var.variableN      # Ignore
}

lifecycle {
ignore_changes = [
block1[0].property2, block1[0].property3, ... ,block1[0].propertyN
]
}
}

一旦资源生成,block1中的许多属性可能会由于与用户的交互而发生变化。我想在运行terraform plan时忽略这些更改,除了一些异常,如果将来更改这些异常应该会产生差异。(例如,在上面的资源中,如果property1被改变,它应该产生一个差异,但不是为其他)

忽略这些变化可以使用lifecycle块中的ignore_changes来完成。但似乎可以做到以上几点。将整个block1参数添加到此将导致忽略内部的所有更改,或者我们必须将块中所有被忽略的属性逐一添加到ignore_changes块中,正如我在示例中提到的那样。

手动这样做会使事情更难维护,因为当一个新属性被添加/删除到块中时,您将不得不不断添加/删除新属性。那么,是否可以配置ignore_changes块来忽略所有更改并特别添加所需的异常?

公立小学我不相信这个问题是特定于某个资源的,但我试图实现这个概念的资源是Azure应用程序服务资源,特别是其中的site_config块。

我能想到的最简单的方法是列出该块中所有可能的单个属性,除了那些您不想忽略更改的属性。这仍然是乏味和丑陋的。

这里有一个更聪明的(未经测试的)方法来尝试

#get the existing resource
data "example_resource" "instance1" {
}
resource "example_resource" "instance1" {
block1 {
...
}
lifecycle {
#transform the list of properties so the values all start with block[0].
ignore_changes = [for prop in local.ignore_change_props : "block[0].${prop}"] 
}
}
locals {
#these properties we want to exclude from ignore_changes
change_exceptions = ["property1", "property10"],
#get all the properties from the data block in a map, then remove properties to be excluded
ignore_change_props = setsubtract(keys(data.example_resource.instance1.block1), local.change_exceptions)
}

最新更新