我有一个带有以下输入变量的模块:
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
值(value)。但是,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)
就我而言。
最佳答案
已为 Terraform 建议了对象变量中的可选参数:
https://github.com/hashicorp/terraform/issues/19898
不幸的是,截至 2020 年 5 月 30 日,这方面没有任何进展。
这是他们 repo 中投票最多的问题,我们所能做的就是继续投票,希望很快就会实现。
你是对的,替代方案是不可能的,或者是简单的
关于terraform - 如何在不破坏现有客户端的情况下扩展 terraform 模块输入变量模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62105031/