我有一个手臂模板,我想转换像“美国”这样的国家名称,我想得到ISO 3166-1 alpha 2代码像"US“。此转换后的值用于资源组的名称。我试着使用condicion "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",
"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]"
}
}}发布于 2020-10-14 17:19:42
与其使用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]"
}
}
}发布于 2020-10-14 22:36:57
我使用的最终解决方案:将参数 "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]"
}
}}https://stackoverflow.com/questions/64349356
复制相似问题