在 Terraform 中列出字符串以供 ARM 模板使用

List to string in Terraform for ARM template use

正在尝试使用列表(字符串)将我的 tfvars 中的订阅字符串化到 TF ARM 部署中。

我想要以下内容:

"scope": [
"/subscriptions/0-1", 
"/subscriptions/0-2"
]

我尝试了以下但没有成功:

"scope": [
    ${join(", ", each.value.subscriptions)} 
]

我收到以下错误 错误:扩展 template_content:寻找值开头的无效字符“/”

dev.tfvars

schedules = {
  01 = {
    name                   = "01" 
    subscriptions          = ["/subscriptions/0-1", "/subscriptions/0-2"]
  }

  02 = {
    name                   = "02" 
    subscriptions          = ["/subscriptions/0-1", "/subscriptions/0-2"]
  }

}

variables.tf

variable "schedules" {
  type = map(object({
    name   = string
    subscriptions          = list(string)
  }))
}

updates.tf

resource "azurerm_resource_group_template_deployment" "updates" {
      for_each            = var.schedules
      name                = each.key 
      resource_group_name = var.rg_name
      deployment_mode     = "Incremental"
      debug_level         = "requestContent"
      template_content = <<TEMPLATE


{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",        
       

          "resources": [
            {
                     
            
                "apiVersion": "2017-05-15-preview",
                "type": "Microsoft.Automation/automationAccounts/softwareUpdateConfigurations",
                "name": "[concat(parameters('automationAccounts_automation_account_name'), '/test-schedule')]",
                "properties": {
                    "updateConfiguration": {
                        "operatingSystem": "Windows",
                        "duration": "PT2H",
                        "windows": {
                            "excludedKbNumbers": [],
                            "includedUpdateClassifications": "Critical, Security",
                            "rebootSetting": "IfRequired"
                        },
                        "targets": {
                          "azureQueries": [
                            {
                              "scope": [
                                
                                
                                ${join("\", \"", each.value.subscriptions)}
                                

                              ],
                              "tagSettings": {
                                "tags": {
                                  "updates": [
                                    "test"
                                  ]
                                },
                                "filterOperator": "All"
                              }
                            }
                          ]
                        }
                    },
                    "scheduleInfo": {
                        "isEnabled": "true",
                        "frequency": "Month",
                        "interval": "1",
                        "startTime": "2022-01-18T01:01:00+11:00",
                        "timeZone":  "Australia/Sydney",
                        "advancedSchedule": {
                            "monthlyOccurrences": [
                              {
                                  "occurrence": "Saturday",
                                  "day": "2"
                              }
                            ]
                          }
                
                    }
                }
            } ]
    }


TEMPLATE
     
}

通常在像您这样的情况下,您会使用 jsonencode 而不是 join

resource "azurerm_resource_group_template_deployment" "updates" {
      for_each            = var.schedules
      name                = each.key 
      resource_group_name = var.rg_name
      deployment_mode     = "Incremental"
      debug_level         = "requestContent"
      template_content = <<TEMPLATE


{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",        
       

          "resources": [
            {
                     
            
                "apiVersion": "2017-05-15-preview",
                "type": "Microsoft.Automation/automationAccounts/softwareUpdateConfigurations",
                "name": "[concat(parameters('automationAccounts_automation_account_name'), '/test-schedule')]",
                "properties": {
                    "updateConfiguration": {
                        "operatingSystem": "Windows",
                        "duration": "PT2H",
                        "windows": {
                            "excludedKbNumbers": [],
                            "includedUpdateClassifications": "Critical, Security",
                            "rebootSetting": "IfRequired"
                        },
                        "targets": {
                          "azureQueries": [
                            {
                              "scope": ${jsonencode(each.value.subscriptions)},
                              "tagSettings": {
                                "tags": {
                                  "updates": [
                                    "test"
                                  ]
                                },
                                "filterOperator": "All"
                              }
                            }
                          ]
                        }
                    },
                    "scheduleInfo": {
                        "isEnabled": "true",
                        "frequency": "Month",
                        "interval": "1",
                        "startTime": "2022-01-18T01:01:00+11:00",
                        "timeZone":  "Australia/Sydney",
                        "advancedSchedule": {
                            "monthlyOccurrences": [
                              {
                                  "occurrence": "Saturday",
                                  "day": "2"
                              }
                            ]
                          }
                
                    }
                }
            } ]
    }


TEMPLATE
     
}

您在每个元素之间插入了双引号,但它们应该包含每个元素。

${join(", ", [ for sub in each.value.subscriptions: "\"${sub}\"" ])}