Azure Bicep(三)變量控制

一,引言

  當我們在使用 Azure Bicep 的時候會出現以下幾個問題:

1)文件中有很多地方會重用很多相同的值

2)輸入參數可以在統一的地方進行修改

帶着這些問題,我們開始今天的內容,學習如何在 Bicep 中使用變量。

--------------------我是分割線--------------------

1,Azure Bicep 開發利器

2,Azure Bicep(二)語法簡介

3,Azure Bicep(三)變量控制

二,正文

1,語法

在 Bicep 中聲明變量並給其初值時非常簡單的。如下圖所示,爲標準的參數賦值的語句

param runtime string='.NET 6'

Bicep 語句中的變量的定義類似於其他語言,但是也有它的奇特之處,我稱它爲 “Azure Bicep 語法糖”

var myString = 'some string value'
var myNull = null
var location = resourceGroup().location

以上的語句中,我們不難看出,

第一個變量沒有類型

第二個變量沒有類型,而且變量默認值爲 null

第三個變量的默認值爲表達式

2,使用參數,變量

常見的情況時在變量聲明中使用參數

//targetScope='resourceGroup' Default Deplay Target
param webAppName string = toLower('cnbateblogweb')
param webSiteName string=toLower('${webAppName}-appservice')

變量也是一樣的,但是需要注意的時,我們不能在變量使用的時候引入循環,這個是不允許的

var name = 'Allen'
var str= 'Hello, ${name}!'

3,資源屬性

除了使用參數和變量之外,還可以使用資源屬性來創建變量 planid

//targetScope='resourceGroup' Default Deplay Target
param webAppName string = toLower('cnbateblogweb')
param webSiteName string = toLower('${webAppName}-appservice')
param runtime string = '.NET 6'
param webAppPlanName string = toLower('${webAppName}-appserviceplan')
param location string = resourceGroup().location // Location for all resources

resource appServicePlan 'Microsoft.Web/serverfarms@2020-12-01' = {
  name: webAppPlanName
  location: location
  sku: {
    name: 'F1'
    capacity: 1
  }
}

var planid = '${appServicePlan.id}'

resource webApplication 'Microsoft.Web/sites@2018-11-01' = {
  name: webSiteName
  location: location
  properties: {
    serverFarmId: planid
    siteConfig: {
      linuxFxVersion: runtime
    }
  }
}

4,模塊

如果模塊在 outpus 部分中返回一些值,它們也可以用於變量聲明。模塊在輸出中返回完整的存儲帳戶對象,然後我們在主模板中使用這些對象

main.bicep

module stg 'storageaccount/storage.bicep' = {
  name: 'storageDeployment'
  params: {
    storageAccountName: 'cnbateblogsa'
  }
}

var myTag = '${stg.outputs.storageAccount.kind}-${stg.outputs.storageAccount.sku.name}'

storage.bicep

param storageAccountName string

resource stg 'Microsoft.Storage/storageAccounts@2021-02-01' = {
  name: storageAccountName
  location: resourceGroup().location
  kind: 'StorageV2'
  sku: {
    name: 'Standard_LRS'
  }
}

output storageAccount object = stg

 

5,循環

使用循環來初始化變量,並且可以使用循環定義多個資源模塊

// Creating a variable using a for-loop
var secretsValues = [for i in range(0, 3): {
  name: 'secret${i}'
  value: 'supersecretvalue${i}'
}]

// Assuming that a key vault already exists
resource kv 'Microsoft.KeyVault/vaults@2019-09-01' existing = {
  name: 'kv-contoso'
}

// Using variable to create multiple resources
resource secrets 'Microsoft.KeyVault/vaults/secrets@2019-09-01' = [for secret in secretsValues: {
  name: secret.name
  parent: kv
  properties: {
    value: secret.value
  }
}]

三,結尾

  今天我們介紹了Azure Bicep的變量的相關用法。變量用於簡化 Bicep 文件開發。 請定義包含複雜表達式的變量,而不必在整個 Bicep 文件中重複使用複雜的表達式。 然後,在整個 Bicep 文件中根據需要使用該變量。資源管理器會在啓動部署操作之前解析變量。 在 Bicep 文件中所有使用該變量的位置,資源管理器都會將其替換爲解析的值。本文所分享的內容也存在着很多我自己的一些理解,有理解不到位的,還包含,並且指出不足之處!!!!!

作者:Allen 

版權:轉載請在文章明顯位置註明作者及出處。如發現錯誤,歡迎批評指正。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章