【发布时间】:2020-05-30 15:57:43
【问题描述】:
我有一个带有以下输入变量的模块:
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 抱怨的错误中了解到,因为我没有在输入中指定新属性的值。
所以,有三种解决方案:
- 更新所有现有代码以添加新属性 - 不可能。
- 给新版本的模块打上不同的标签,让新代码引用新标签,而旧代码继续引用旧标签——从长远来看会导致标签泛滥,产生各种奇怪的笛卡尔乘法标签名称中的特征。最终 - 不可能。
- 通过注释掉可选属性来放松输入变量架构并在代码中使用
try。
最后一个选项并不理想,因为模块的文档不会列出可选属性。但从代码管理的角度来看 - 它是最好的。
所以问题是 - 可以将输入对象属性定义为可选的吗?理想情况下,它应该包含默认值,但我现在可以使用 try 方法。
编辑 1
我实际上认为我可以在对象中传递未知属性,但没有。一旦给出了模式,它就没什么了。因此,在我的情况下,唯一向后兼容的解决方案是使用 map(any)。
【问题讨论】:
-
加入俱乐部:github.com/hashicorp/terraform/issues/19898 +1,并给他们留言为什么你需要它,希望这将很快实施......这是他们回购中最受好评的问题跨度>
-
真糟糕。请宣传您的评论以回答,所以我可以相信你。
标签: terraform