0

我有一个 ARM 模板,我想转换国家名称,如“美国”,我想获得 ISO 3166-1 alpha 2 代码,如“美国”。我将使用此转换后的值作为资源组的名称。我尝试使用条件“if”,但如果参数“CountryString”仅包含两个国家/地区,我可以使用此选项。我无法找到包含两个以上国家/地区的参数“CountryObject”的解决方案。有没有办法做到这一点?

{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.1",
"parameters": {
        "CountryString": {
        "type": "string",
        "metadata": { "Description": "Select a country from the list." },
        "defaultValue": "United States",
        "allowedValues": [ "United States", "Germany"]
    },
    "CountryObject": {
        "type": "object",
        "defaultValue": {
            "United States": "US",
            "Germany": "DE",
            "United Kingdom": "GB"
        }
    }
},
"variables": {
     "OutputString": {
        "type": "string",
        "value": "[if(equals('United States', parameters('CountryString')), 'US','DE')]"
    },
      "Outputobject": {
        "type": "string",
        "value": "[if(equals('United States', parameters('CountryObject')), 'US','DE')]"
    },
     "rgName": "[concat('rg-',variables('Outputobject').value, '-rgname')]"
},

   "resources": [
    {
        "type": "Microsoft.Resources/resourceGroups",
        "apiVersion": "2019-08-01",
        "location": "East Asia",
        "name": "[variables('rgName')]",
        "properties": {}
    }],
"outputs": {
    "OutputString": {
        "type": "string",
        "value": "[variables('OutputString').value]"
    },
        "Outputobject": {
        "type": "string",
        "value": "[variables('Outputobject').value]"
    }
}}

部署到天蓝色

模板在这里

4

2 回答 2

0

不要使用该if语句,而是将其视为CountryObject哈希表。

      "value": "[parameters('CountryObject')[parameters('CountryString')]]"

整个东西。

{
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
  "contentVersion": "1.0.0.1",
  "parameters": {
    "CountryString": {
      "type": "string",
      "metadata": { "Description": "Select a country from the list." },
      "defaultValue": "United States",
      "allowedValues": [ "United States", "Germany" ]
    },
    "CountryObject": {
      "type": "object",
      "defaultValue": {
        "United States": "US",
        "Germany": "DE",
        "United Kingdom": "GB"
      }
    }
  },
  "variables": {
    "OutputString": {
      "type": "string",
      "value": "[parameters('CountryObject')[parameters('CountryString')]]"
    }  },

  "resources": [],
  "outputs": {
    "OutputString": {
      "type": "string",
      "value": "[variables('OutputString').value]"
    }
  }
}
于 2020-10-14T17:19:42.490 回答
0

我使用的最终解决方案:将 参数“CountryObject”替换为变量“CountryObject”

{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.1",
"parameters": {
    "CountryString": {
        "type": "string",
        "metadata": { "Description": "Select a country from the list." },
        "allowedValues": [
            "United States",
            "Germany",
            "United Kingdom"
        ]
    }
},
"variables": {
    "CountryObject": {
        "type": "object",
        "value": {
            "United States": "US",
            "Germany": "DE",
            "United Kingdom": "GB"

        }
    },
    "OutputString": {
        "type": "object",
        "value": "[variables('CountryObject').value[parameters('CountryString')]]"
    }
},

"resources": [],
"outputs": {
    "OutputString": {
        "type": "string",
        "value": "[variables('OutputString').value]"
    }
}}

带有完整国家/地区列表的模板。

于 2020-10-14T22:36:57.277 回答