如何在 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]"
}
}}
不使用 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]"
}
}}
我有一个 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]"
}
}}
不使用 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]"
}
}}