azure - 如何在 arm 模板中将国家名称转换为 ISO 3166-1 alpha-2 值
问题描述
我有一个 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]"
}
}
}
推荐阅读
- php - Laravel 基于角色的身份验证和视图。路由中的中间件不起作用
- swiftui - SwiftUI:添加 .onDrag 块单击/选择 macOS 侧边栏中的项目
- python - 跟踪动态规划中的项目(类似于背包问题)
- python - windows下python socket不可靠
- java - 如何将 Saxon 调试支持从 9.2 升级到 9.5
- javascript - 如何在 react-split-pane 中分别支持单个窗格的滚动?
- google-bigquery - 通过上传 csv 在大查询中创建表
- php - docker php-fpm 无法删除 Nginx 进程的 Fastcgi Cache 文件
- python - 使用字符串数字创建字典字典
- django - 我应该在哪里放置我的上下文以使 forms_as.p 正常工作