我使用Terraform创建名称基于count.index
的VM。我想创建一个标签,其中的键是基于count.index
动态创建的。我只是无法正常工作。
对于VM01,标记应为“ PatchCycle01 = centos”,对于VM02,标记应为“ PatchCycle02 = centos”。
tags = {
PatchCycle${format("%02d", count.index + 1)} = "CentOS"
}
我尝试了上面的代码,但是没有用。有什么建议吗?
答案 0 :(得分:1)
您只需要正确引用左侧(地图的键)即可使其工作:
locals {
foo = "foo"
map_test = {
"PatchCycle${local.foo}" = "foo"
}
}
output "foo" {
value = local.map_test
}
应用以上内容将输出以下内容:
foo = {
"PatchCyclefoo" = "foo"
}
因此对于您的示例,您将执行以下操作:
tags = {
"PatchCycle${format("%02d", count.index + 1)}" = "CentOS"
}
答案 1 :(得分:0)
密钥需要使用string template构建,如下所示:
tags = {
"PatchCycle${format("%02d", count.index + 1)}" = "CentOS"
}
如果使用的是Terraform 0.12之前的版本,则必须使用map function,现在已弃用:
tags = "${map(
"PatchCycle${format("%02d", count.index + 1)}", "CentOS"}"
)