azure 如果条件为真,则使用for循环为变量赋值

pprl5pva  于 2023-08-07  发布在  其他
关注(0)|答案(2)|浏览(92)

我需要将short_id赋给变量short_location_name,
我有Mapshort_location_names在下面的terrfaorm代码,如果“var.location”是“westus”,我需要分配short_location_names“wus”
我的代码:

locals {
  short_location_names = {
      "westus": "wus",
      "eastus": "eus",
      "westus2": "wus2",
      "eastus2": "eus2"
  }
  short_location_name = {
    for location, short_id in local.short_location_names: location == var.location ? short_location_name => short_id
  }

字符串
我试了上面的代码,我得到错误缺少虚假陈述,如何修复?

ibrsph3r

ibrsph3r1#

使用lookup函数:

short_location_name = lookup(local.short_location_names, var.location, null)

字符串
或者是

short_location_name = { var.location: lookup(local.short_location_names, var.location, null) }


或者简单地local.short_location_names[var.location],如果该值将始终存在于Map中。

brccelvz

brccelvz2#

工作溶液

locals {
 short_location_name  = "${lookup(local.region_code, var.location, "null")}"

 region_code = {

 "East US 2" = "use2"
 "eastus2" = "use2"
 "West US 2" = "usw2"
 "westus2" = "usw2"
 
 }
}

字符串

相关问题