You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure ARM模板中获取位置缩写代码

在Azure ARM模板中获取位置缩写的简便方法
  • 使用变量映射表替代嵌套if语句
    直接在ARM模板的variables块里定义键值对形式的区域映射表,将完整位置名称与缩写一一对应,相比嵌套if语句更简洁易维护:

    "variables": {
      "locationAbbreviations": {
        "westus2": "wus2",
        "westcentralus": "wcus",
        "eastus": "eus",
        "southeastasia": "sea"
        // 根据需求补充其他区域的映射关系
      }
    },
    

    在需要调用位置缩写的地方,直接通过索引取值即可:

    "[variables('locationAbbreviations')[parameters('location')]]"
    

    注意ARM模板中resourceGroup().location返回的是小写字符串,若你的location参数可能包含大写格式,可先转小写再取值:

    "[variables('locationAbbreviations')[toLower(parameters('location'))]]"
    
  • 添加未定义区域的兜底处理
    若担心存在未添加到映射表的区域,可结合contains和if函数实现兜底逻辑,避免模板执行报错:

    "[if(contains(variables('locationAbbreviations'), toLower(parameters('location'))), variables('locationAbbreviations')[toLower(parameters('location'))], parameters('location'))]"
    

    当找不到对应缩写时,会直接返回原位置值。

内容的提问来源于stack exchange,提问作者samsu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 04:56:03