你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
此规则在 resourceGroup().___location
参数默认值之外查找或使用 deployment().___location
。
注释
此规则默认处于关闭状态。 更改 bicepconfig.json 中的级别以启用它。
Linter 规则代码
请在 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。