此规则在 resourceGroup().location
参数默认值之外查找或使用 deployment().location
。
请在 Bicep 配置文件中使用以下值自定义规则设置:
no-loc-expr-outside-params
resourceGroup().location
并且 deployment().location
只应用作参数的默认值。
模板用户对其可以创建资源的区域的访问权限可能有限。 如果在用户无法访问的区域中创建资源组或部署,则表达式 resourceGroup().location
或 deployment().location
可能会阻止用户,从而阻止用户使用模板。
最佳做法建议,若要设置资源的位置,模板应具有一个名为 location
字符串参数。 如果将参数默认 location
为 resourceGroup().location
模板中的其他位置或使用 deployment().location
这些函数,则模板的用户可以在方便时使用默认值,但也在需要时指定其他位置。
resource storageaccount 'Microsoft.Storage/storageAccounts@2023-04-01' = {
location: resourceGroup().location
}
可以通过创建 location
默认 resourceGroup().location
属性并改用此新参数来修复失败:
param location string = resourceGroup().location
resource storageaccount 'Microsoft.Storage/storageAccounts@2023-04-01' = {
location: location
}
以下示例失败此测试,因为 location
使用的是 resourceGroup().location
但不是参数:
var location = resourceGroup().location
可以通过将变量转换为参数来修复失败:
param location string = resourceGroup().location
如果使用 Azure PowerShell 部署到订阅、管理组或租户,则应使用除其他参数名称以外的 location
参数名称。 rgLocation
名称来避免此冲突。
部署到资源组时, location
可以使用参数名称,因为 New-AzResourceGroupDeployment 没有命名 location
的参数。
有关 Linter 的详细信息,请参阅使用 Bicep Linter。