如何在 arm 模板中将国家/地区名称转换为 ISO 3166-1 alpha-2 值 [英] How to convert country names to ISO 3166-1 alpha-2 values in arm template

查看:23
本文介绍了如何在 arm 模板中将国家/地区名称转换为 ISO 3166-1 alpha-2 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ARM 模板,我想将国家/地区名称转换为美国";并且我想获得像US"这样的 ISO 3166-1 alpha 2 代码.这个转换后的值我将用于资源组的名称.我尝试使用条件if",但是如果参数CountryString"不可用,我可以使用此选项只包含两个国家.我无法找到参数CountryObject"的解决方案.其中包含两个以上的国家.有没有办法做到这一点?

I have an ARM-template and I would like convert country name like "United States" and I would like to get the ISO 3166-1 alpha 2 code like "US". This converted value I would use for name of resource group. I tryed use condicion "if", but this options i can use in case when Parametr "CountryString" contains only two country. I am not able to find solutions for parameter "CountryObject" which contains more than two country. Is there a way to do this?

{
"$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]"
    }
}}

部署到azure

模板在这里

推荐答案

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

Instead of using the if statement, treat the CountryObject like a hashtable.

      "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]"
    }
  }
}

这篇关于如何在 arm 模板中将国家/地区名称转换为 ISO 3166-1 alpha-2 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆