如何在手臂模板中将国家/地区名称转换为ISO 3166-1 alpha-2值

问题描述

我有一个ARM模板,我想要转换国家名称(如“美国”),并且想要获得ISO 3166-1 alpha 2代码(如“ US”)。我将使用此转换后的值作为资源组的名称。我尝试使用条件转换“ if”,但是当Parametr“ 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",parameters('CountryObject')),"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]"
    },"value": "[variables('Outputobject').value]"
    }
}}

Deploy to azure

Template here

解决方法

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#","parameters": {
    "CountryString": {
        "type": "string","allowedValues": [
            "United States","Germany","United Kingdom"
        ]
    }
},"variables": {
    "CountryObject": {
        "type": "object","value": {
            "United States": "US","United Kingdom": "GB"

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

Template with a complete list of countries.

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...