Azure 如何在arm模板中将国家名称转换为ISO 3166-1 alpha-2值

Azure 如何在arm模板中将国家名称转换为ISO 3166-1 alpha-2值,azure,azure-resource-manager,arm-template,country-codes,iso-3166,Azure,Azure Resource Manager,Arm Template,Country Codes,Iso 3166,我有一个ARM模板,我想转换国家名称,如“美国”,我想得到ISO 3166-1阿尔法2代码,如“美国”。我将使用此转换值作为资源组的名称。我尝试使用条件“if”,但当参数“CountryString”仅包含两个国家时,我可以使用此选项。我无法找到包含两个以上国家的参数“CountryObject”的解决方案。有办法做到这一点吗 { "$schema": "https://schema.management.azure.com/schemas/2018-05-01/s

我有一个ARM模板,我想转换国家名称,如“美国”,我想得到ISO 3166-1阿尔法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]"
    }
}}


不要使用
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]"
    }
  }
}

我使用的最终解决方案是: 参数“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]"
    }
}}