我有一个带有以下输入变量的模块:
variable "apsvc_map" {
description = "The App Services sharing the same App Service Plan. Maps an App Service name to its properties."
type = map(object({
identity_ids = list(string),
disabled = bool
}))
}
现在,我想向架构添加一个新属性- no_custom_hostname_binding
。新版本将是:
variable "apsvc_map" {
description = "The App Services sharing the same App Service Plan. Maps an App Service name to its properties."
type = map(object({
identity_ids = list(string),
disabled = bool
no_custom_hostname_binding = bool
}))
}
借助该try
功能,可以使此更改在模块代码中向后兼容,因为省略新属性等同于为其提供false
值。
但是,terraform严格对待此模式,并且在没有新字段的情况下不允许传递输入:
2020-05-30T15:34:20.8061749Z Error: Invalid value for module argument
2020-05-30T15:34:20.8062005Z
2020-05-30T15:34:20.8062205Z on ..\..\modules\web\main.tf line 47, in module "web":
2020-05-30T15:34:20.8062336Z 47: apsvc_map = {
2020-05-30T15:34:20.8062484Z 48: dfhub = {
2020-05-30T15:34:20.8062727Z 49: disabled = false
2020-05-30T15:34:20.8065156Z 50: identity_ids = [local.identity_id]
2020-05-30T15:34:20.8065370Z 51: }
2020-05-30T15:34:20.8065459Z 52: }
2020-05-30T15:34:20.8065538Z
我从terraform抱怨的错误中了解到,因为我没有在输入中指定new属性的值。
因此,有三种解决方案:
try
在代码中使用来放宽输入变量架构。最后一个选项并不理想,因为该模块的文档不会列出可选属性。但是从代码管理的角度来看,这是最好的。
所以问题是-输入对象属性可以定义为可选的吗?理想情况下,它应该包含默认值,但是我现在可以使用该try
方法。
编辑1
我实际上以为可以在对象中传递未知的属性,但不能。一旦给出了模式,就无非就是了。因此,唯一向后兼容的解决方案是map(any)
在我的情况下使用。
已为Terraform建议了对象变量中的可选参数:https :
//github.com/hashicorp/terraform/issues/19898
不幸的是,截至2020年5月30日,这方面还没有任何进展。
这是他们的回购计划中最受争议的问题,我们所能做的就是继续投票,并希望很快会实施。
而且您是对的,其他选择完全是不可能的,或者是毫无用处的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句