Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
此规则查找未参数化的 Azure 位置值的用法。
请在 Bicep 配置文件中使用以下值自定义规则设置:
no-hardcoded-location
模板用户对其可以创建资源的区域的访问权限可能有限。 硬编码的资源位置可能会阻止用户创建资源,从而阻止用户使用模板。 通过提供默认为资源组位置的位置参数,用户可以在方便但指定其他位置时使用默认值。
而不是使用硬编码的字符串或变量值,而是使用参数、字符串“global”或表达式(但不能resourceGroup().location
deployment().location
或,请参阅 no-loc-expr-outside-params)。 最佳做法建议,若要设置资源的位置,模板应具有一个名为 location
字符串参数。 此参数可能默认为资源组或部署位置(resourceGroup().location
或 deployment().location
)。
以下示例失败此测试,因为资源 location
的属性使用字符串文本:
resource stg 'Microsoft.Storage/storageAccounts@2023-04-01' = {
location: 'chinanorth'
}
可以通过创建新的 location
字符串参数来修复它(可以选择使用默认值 - resourceGroup()。位置经常用作默认值):
param location string = resourceGroup().location
resource stg 'Microsoft.Storage/storageAccounts@2023-04-01' = {
location: location
}
使用 快速修复 创建位置参数,并将字符串文本替换为参数名称。 请参阅以下屏幕截图:
以下示例失败此测试,因为资源 location
的属性使用具有字符串文本的变量。
var location = 'chinanorth'
resource stg 'Microsoft.Storage/storageAccounts@2023-04-01' = {
location: location
}
可以通过将变量转换为参数来修复此问题:
param location string = 'chinanorth'
resource stg 'Microsoft.Storage/storageAccounts@2023-04-01' = {
location: location
}
以下示例失败此测试,因为传入的字符串文本将传入模块参数,而模块参数又用于资源 location
的属性:
module m1 'module1.bicep' = {
name: 'module1'
params: {
location: 'chinanorth'
}
}
其中 module1.bicep 为:
param location string
resource storageaccount 'Microsoft.Storage/storageAccounts@2023-04-01' = {
name: 'storageaccount'
location: location
kind: 'StorageV2'
sku: {
name: 'Premium_LRS'
}
}
可以通过为值创建新参数来修复失败:
param location string // optionally with a default value
module m1 'module1.bicep' = {
name: 'module1'
params: {
location: location
}
}
有关 Linter 的详细信息,请参阅使用 Bicep Linter。