Terraform 标记关键点中的地形插值

Terraform 标记关键点中的地形插值,terraform,Terraform,我使用Terraform创建虚拟机,其名称基于count.index。我想创建标记,其中键是基于count.index动态生成的。我就是不能让它工作 对于VM01,标记应为“PatchCycle01=centos”;对于VM02,标记应为“PatchCycle02=centos” 我尝试了上面的代码,但没有成功。有什么建议吗?钥匙需要像这样建造: tags = { "PatchCycle${format("%02d", count.index + 1)}&quo

我使用Terraform创建虚拟机,其名称基于
count.index
。我想创建标记,其中键是基于
count.index
动态生成的。我就是不能让它工作

对于VM01,标记应为“PatchCycle01=centos”;对于VM02,标记应为“PatchCycle02=centos”


我尝试了上面的代码,但没有成功。有什么建议吗?

钥匙需要像这样建造:

tags = {
  "PatchCycle${format("%02d", count.index + 1)}" = "CentOS"
}
  tags = {
    "PatchCycle${format("%02d", count.index + 1)}" = "CentOS"
  }
如果您使用的是比Terraform 0.12更早的产品,则必须使用现在已弃用的:

tags = "${map(
  "PatchCycle${format("%02d", count.index + 1)}", "CentOS"}"
)

您只需正确引用左侧(地图的键)即可:

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"
}
  tags = {
    "PatchCycle${format("%02d", count.index + 1)}" = "CentOS"
  }

谢谢你的建议@alian,我试过了,它没有插入“${format(“%02d”,count.index+1)}”。。并按原样创建标记。因此,创建的标记是“tags.PatchCycle${format”(%02d),count.index+1)}:“=>”@user3362908-odd。文档表明map函数已被弃用,
{
}
语法将其替换。现在我建议使用map,尽管它已被弃用。这里完全不需要
map
函数。即使在使用
map
功能时,您也可以像使用该功能一样引用左侧;0.11需要
map
功能。由于
{
..
}
语法现在支持任意键表达式,因此0.12以后的版本不推荐使用它。还要注意的是,Terraform 0.12要求表达式通过插入到字符串中来给出!