簡體   English   中英

如何在 arm 模板中將國家/地區名稱轉換為 ISO 3166-1 alpha-2 值

[英]How to convert country names to ISO 3166-1 alpha-2 values in arm template

我有一個 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]"
    }
}}

部署到 azure

模板在這里

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

帶有完整國家/地區列表的模板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM