gpt4 book ai didi

azure - 二头肌 : how to deploy resource conditionally only if it does not exists

转载 作者:行者123 更新时间:2023-12-02 23:08:56 36 4
gpt4 key购买 nike

我遇到以下问题:我想部署存储帐户,但前提是该帐户不存在。

我使用 az cli 部署脚本检查是否存在:

resource checkStorageAccountExistence 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
name: 'checkStorageAccountExistenceScript'
location: location
kind: 'AzurePowerShell'
identity: identity
properties: {
arguments: '-storageAccountName \'${string(name)}\' -resourceGroupName \'${string(resourceGroupName)}\''
azPowerShellVersion: '3.0'
scriptContent: '''
param([string]$storageAccountName, [string]$resourceGroupName)
$provisioningState = az storage account show --name $storageAccountName --resource-group $resourceGroupName --output tsv --query 'provisioningState'
if ($provisioningState -eq "Succeeded") {$output = $true} else {$output = $false}
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['storageAccountExists'] = $output
'''
forceUpdateTag: utcValue
retentionInterval: 'P1D'
}
}

并且我想部署有条件存储帐户:

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = if (checkStorageAccountExistence.properties.outputs.storageAccountExists == true){
}

但是我收到错误:

This expression is being used in the if-condition expression, whichrequires a value that can be calculated at the start of thedeployment. Properties of checkStorageAccountExistence which can becalculated at the start include "apiVersion", "id", "name","type".bicep(BCP177)

最佳答案

您可以在模块中创建存储帐户:

// storage-account.bicep

param exists bool
param name string
param location string

...

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = if (!exists) {
name: name
location: location
...
}

然后像这样调用它:

// main.bicep

resource checkStorageAccountExistence 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
name: 'checkStorageAccountExistenceScript'
location: location
kind: 'AzurePowerShell'
identity: identity
properties: {
arguments: '-storageAccountName \'${string(name)}\' -resourceGroupName \'${string(resourceGroupName)}\''
azPowerShellVersion: '3.0'
scriptContent: '''
param([string]$storageAccountName, [string]$resourceGroupName)
$provisioningState = az storage account show --name $storageAccountName --resource-group $resourceGroupName --output tsv --query 'provisioningState'
if ($provisioningState -eq "Succeeded") {$output = $true} else {$output = $false}
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['storageAccountExists'] = $output
'''
forceUpdateTag: utcValue
retentionInterval: 'P1D'
}
}

module storageAccount 'storage-account.bicep' = {
name: 'storage-account'
params: {
exists: checkStorageAccountExistence.properties.outputs.storageAccountExists
location: location
name: name
...
}
}

关于azure - 二头肌 : how to deploy resource conditionally only if it does not exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75868317/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com