首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在arm模板中将国家名称转换为ISO 3166-1 alpha-2值

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

Stack Overflow用户
提问于 2020-10-14 08:24:56
回答 2查看 124关注 0票数 0

我有一个手臂模板,我想转换像“美国”这样的国家名称,我想得到ISO 3166-1 alpha 2代码像"US“。此转换后的值用于资源组的名称。我试着使用condicion "if",但是当Parametr "CountryString“只包含两个国家时,我可以使用这个选项。我无法为包含两个以上国家的参数"CountryObject“找到解决方案。有办法这样做吗?

代码语言:javascript
复制
{
"$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]"
    }
}}

部署到蔚蓝

模板在这里

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-14 17:19:42

与其使用if语句,不如将CountryObject视为一个哈希表。

代码语言:javascript
复制
      "value": "[parameters('CountryObject')[parameters('CountryString')]]"

整件事。

代码语言:javascript
复制
{
  "$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]"
    }
  }
}
票数 0
EN

Stack Overflow用户

发布于 2020-10-14 22:36:57

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

代码语言:javascript
复制
{
"$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]"
    }
}}

有完整国家清单的模板。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64349356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档