我们有一个二头肌文件,其中包含十几个相关资源和一个资源名称前缀作为参数(后缀是硬编码的)。
是否有一种bicep/ARM方法可以在创建任何资源之前首先检查是否使用了任何结果(前缀+后缀)资源名称?
我知道
uniqueString
功能。我们还可以为每个资源名称引入一个默认参数,并使用违规资源的不同资源名称再次运行二头肌文件。
非二头肌检查方法是使用 REST API/CLI 来检查资源名称是否已被采用。
您是否尝试过在部署脚本中使用
WhatIf
并在 Bicep 中使用 existing
?
WhatIf
帮助检查资源是否部署。existing
有助于减少二头肌变形。sample.ps1
$resoucreGroupName = "test-vm-rg"
$param = @{
vnetName = "wbtestvnet1"
location = "eastus"
newOrExisting = "new"
}
$deploymentName = "testvnetdeployment"
$templateFile = ".\sample.bicep"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resoucreGroupName -TemplateFile $templateFile `
-TemplateParameterObject $param -WhatIf
sample.bicep
param vnetName string
param vnetPrefix string = '10.0.0.0/16'
param vmSubnetPrefix string = '10.0.0.0/24'
@description('location for all resources.')
param location string
@allowed([
'new'
'existing'
])
param newOrExisting string
resource vnet 'Microsoft.Network/virtualNetworks@2022-11-01' = if (newOrExisting == 'new') {
name: vnetName
location: location
properties: {
addressSpace: {
addressPrefixes: [
vnetPrefix
]
}
subnets: [
{
name: 'vmSubnet1'
properties: {
addressPrefix: vmSubnetPrefix
}
}
]
}
}
resource vnetExisting 'Microsoft.Network/virtualNetworks@2022-11-01' existing = if (newOrExisting == 'existing') {
name: vnetName
}
output vnetId string = ((newOrExisting == 'new') ? vnet.id : vnetExisting.id)