如何在不中断现有客户端的情况下扩展terraform模块输入变量模式?
我有一个具有以下输入变量的模块:如何在不中断现有客户端的情况下扩展terraform模块输入变量模式?,terraform,Terraform,我有一个具有以下输入变量的模块: 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 })) } 现在我想
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
}))
}
现在我想向模式添加一个新属性-无自定义主机名绑定
。新版本将是:
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)
。建议Terraform使用对象变量中的可选参数:不幸的是,截至2020年5月30日,在这方面没有任何进展。
这是他们回购协议中投票最多的问题,我们所能做的就是继续投票,希望很快就能实施
你是对的。考虑到你的选择,你的偏好,以及我认为你有第四个选择的事实,这些选择是不可能的,或者是纯粹的黑客行为
variable "apsvc_map" {
description = "The App Services sharing the same App Service Plan. Maps an App Service name to its properties."
default = {}
type = map(object({
identity_ids = list(string),
disabled = bool
}))
}
variable "no_custom_hostname_binding" {
description = "Whether or not an App Service should disable hostname binding. Maps an App Service name to an override of the no_custom_hostname_binding property."
type = map(bool)
}
从那里,您可以像这样使用它:
lookup(var.no_custom_hostname_binding[local.awsvpc_map_key], null)
no_custom_hostname_binding = {
"vpc_key" = true
}
并声明如下所示的覆盖:
lookup(var.no_custom_hostname_binding[local.awsvpc_map_key], null)
no_custom_hostname_binding = {
"vpc_key" = true
}
在需要知道该参数的表达式中。这不是非常优雅,但是如果没有可选参数,您就没有很多好的选择
您可以按照此模式添加所需的任意多个可选覆盖,以后也可以添加更多,而不会中断客户端。加入俱乐部:+1,并给他们留下一条评论,说明您为什么需要它,希望它很快就会实现。。。这是他们报告中投票率最高的问题,真是糟糕透顶。请把你的评论推广到回答上来,这样我就可以相信你了。真的不是很优雅。。。正如一位用户所说:
。。。只要你能忍受它给你带来的可怕的负罪感。