azure 使用BICEP可以通过名称的子字符串在数组中查找特定元素吗?

ghhaqwfi  于 2022-11-17  发布在  其他
关注(0)|答案(2)|浏览(112)

我有一个bicep模块storageAccounts.bicep,它创建多个存储帐户并输出一个名称数组:

@description('Name of Environment')
param environment string

param storageAccounts array = [
  {
    name : 'api${environment}${uniqueString(resourceGroup().id)}'
    skuName : 'Standard_LRS'
    accessTier : 'Hot'
  }
  {
    name : 'web${environment}${uniqueString(resourceGroup().id)}'
    skuName : 'Standard_LRS'
    accessTier : 'Hot'
  }
]

resource storage_resource 'Microsoft.Storage/storageAccounts@2021-06-01' = [for storage in storageAccounts: {
  name : storage.name
  location : resourceGroup().location
  sku:{
    name : storage.skuName
  }
  kind : 'StorageV2'
  properties:{
    accessTier: storage.accessTier
    allowBlobPublicAccess: true
    minimumTlsVersion: 'TLS1_2'
    supportsHttpsTrafficOnly: true
    encryption:{
      keySource: 'Microsoft.Storage'
      services:{
        blob:{
          keyType: 'Account'
          enabled: true
        }
        file:{
          keyType: 'Account'
          enabled: true
        }
      }
    }    
  }
}]

output storageAccounts array = [for (name, i) in storageAccounts:{
    storageAccountName : name
}]

我把这个数组传递给另一个模块来创建一个函数应用程序,我试图在数组中找到一个特定的项,如下所示:

@description('array of storage account names')
param storageAccounts array

var storageAccountName = storagesAccounts.where(function(storageAccount) {
  return storageAccount.name.startsWith('api')
})

resource storageAccount 'Microsoft.Storage/storageAccounts@2019-04-01' existing = {
  name: storageAccountName
}

这是我可以做的吗?或者我会更好地单独创建存储帐户并直接传递名称吗?

9gm1akwq

9gm1akwq1#

目前***不支持在二头肌中查找数组元素***。在相同的***Issue 1Issue 2上存在***Github问题
作为一种***解决方法***,您可以***将所需的存储帐户***传递给模块,然后在模块中使用,即***nested template***。
我测试了以下内容,以***创建两个存储帐户***和***,然后在第二个模板中引用api存储帐户***和***将存储帐户的ID存储在keyvault机密中***,如下所示:

一个月一个月

@description('Name of Environment')
param environment string = 'test'

param storageAccounts array = [
  {
    name : 'api${environment}${uniqueString(resourceGroup().id)}'
    skuName : 'Standard_LRS'
    accessTier : 'Hot'
  }
  {
    name : 'web${environment}${uniqueString(resourceGroup().id)}'
    skuName : 'Standard_LRS'
    accessTier : 'Hot'
  }
]

resource storage_resource 'Microsoft.Storage/storageAccounts@2021-06-01' = [for storage in storageAccounts: {
  name : storage.name
  location : resourceGroup().location
  sku:{
    name : 'Standard_LRS'
  }
  kind : 'StorageV2'
  properties:{
    accessTier: 'Hot'
    allowBlobPublicAccess: true
    minimumTlsVersion: 'TLS1_2'
    supportsHttpsTrafficOnly: true
    encryption:{
      keySource: 'Microsoft.Storage'
      services:{
        blob:{
          keyType: 'Account'
          enabled: true
        }
        file:{
          keyType: 'Account'
          enabled: true
        }
      }
    }    
  }
}]

output storageAccounts array = [for (name, i) in storageAccounts:{
    storageAccountName : name
}]

module connectionString './functionapp.bicep'=[for (name,i) in storageAccounts :if(startsWith(storage_resource[i].name,'api')){
  name: 'functionappNested${i}'
  params: {
    storageAccounts:storage_resource[i].name
}
}]

一个月五个月一个月

param storageAccounts string

resource storageAccount 'Microsoft.Storage/storageAccounts@2019-04-01' existing = {
  name: storageAccounts
}

resource keyVaultShared 'Microsoft.KeyVault/vaults@2021-06-01-preview' existing = {
  name: 'keyvaulttest1234ans'
}
resource storageAccountConnectionString 'Microsoft.KeyVault/vaults/secrets@2021-06-01-preview' = {
  parent:keyVaultShared
  name: '${keyVaultShared.name}-test'
  properties:{
    contentType: 'Storage Account Connection String'
    value: storageAccount.id
}
}

输出:

注意:同样,您可以将以web***开头得存储帐户名作为模块传递给另一个嵌套模板作为将其用于另一个资源*.

wydwbb8l

wydwbb8l2#

Bicep支持 filter lambda函数。

var storageAccountName = first(filter(storagesAccounts, x => x.name.startsWith('api'))).name

更多的细节在这个博客中,当我有一个类似的问题要解决https://arinco.com.au/blog/azure-bicep-find-an-item-in-an-array-of-objects/时,我写了这个博客

相关问题