如何在Bicep脚本中复用az deployment命令行的location参数?
解决订阅级Bicep部署重复输入Location的问题
方法1:直接调用部署元数据位置(推荐)
订阅级部署时,az deployment sub create命令里的--location是用来存储部署记录的位置,Bicep内置了deployment().location变量可以直接获取这个值,完全不用单独定义location参数。
修改后的Bicep脚本:
targetScope = 'subscription' @minLength(1) param resourceGroupName string resource myRG 'Microsoft.Resources/resourceGroups@2022-09-01' = { name: resourceGroupName location: deployment().location }
执行部署命令时,只需要指定--location,不会再弹出输入location参数的提示:
az deployment sub create --location eastus --template-file main.bicep
方法2:给Location参数设默认值(保留灵活性)
如果需要保留location参数,方便特殊场景下给资源组指定不同位置,可以给参数设置默认值为deployment().location。默认情况不用手动输入location参数,需要自定义时再传参覆盖。
修改后的脚本:
targetScope = 'subscription' @minLength(1) param location string = deployment().location @minLength(1) param resourceGroupName string resource myRG 'Microsoft.Resources/resourceGroups@2022-09-01' = { name: resourceGroupName location: location }
默认执行命令(无需输入location参数):
az deployment sub create --location eastus --template-file main.bicep
需要指定资源组不同位置时,手动传参覆盖:
az deployment sub create --location eastus --template-file main.bicep --parameters location='westus'
内容的提问来源于stack exchange,提问作者Michael Stum
相关产品推荐
相关产品推荐

