共用方式為


比較適用於範本的 JSON 和 Bicep

此文章會比較適用於 Azure Resource Manager 範本 (ARM 範本) 的 Bicep 語法與 JSON 語法。 在大部分的情況下,相較於 JSON 的對等語法,Bicep 提供的語法較為簡潔。

如果您熟悉如何使用 JSON 開發 ARM 範本,請使用下列範例來了解 Bicep 的對等語法。

比較完整的檔案

Bicep 遊樂場 (英文) 可讓您並排檢視 Bicep 和對等的 JSON。 您可以比較相同基礎結構的實作。

例如,您可以檢視檔案來部署 SQL 伺服器和資料庫 (英文)。 Bicep 大約是 ARM 範本大小的一半。

Screenshot of side by side templates

運算式

編寫運算式:

func()
"[func()]"

參數

宣告具有預設值的參數:

param orgName string = 'Contoso'
"parameters": {
  "orgName": {
    "type": "string",
    "defaultValue": "Contoso"
  }
}

若要取得參數值,請使用您定義的名稱:

name: orgName
"name": "[parameters('orgName'))]"

變數

宣告變數:

var description = 'example value'
"variables": {
  "description": "example value"
},

若要取得變數值,請使用您定義的名稱:

workloadSetting: description
"workloadSetting": "[variables('description'))]"

字串

串連字串:

name: '${namePrefix}-vm'
"name": "[concat(parameters('namePrefix'), '-vm')]"

邏輯運算子

傳回邏輯 AND

isMonday && isNovember
[and(parameter('isMonday'), parameter('isNovember'))]

有條件地設定值:

isMonday ? 'valueIfTrue' : 'valueIfFalse'
[if(parameters('isMonday'), 'valueIfTrue', 'valueIfFalse')]

部署範圍

設定部署的目標範圍:

targetScope = 'subscription'
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#"

資源

宣告資源:

resource virtualMachine 'Microsoft.Compute/virtualMachines@2023-03-01' = {
  ...
}
"resources": [
  {
    "type": "Microsoft.Compute/virtualMachines",
    "apiVersion": "2020-06-01",
    ...
  }
]

有條件地部署資源:

resource virtualMachine 'Microsoft.Compute/virtualMachines@2023-03-01' = if(deployVM) {
  ...
}
"resources": [
  {
    "condition": "[parameters('deployVM')]",
    "type": "Microsoft.Compute/virtualMachines",
    "apiVersion": "2023-03-01",
    ...
  }
]

設定資源屬性:

sku: '2016-Datacenter'
"sku": "2016-Datacenter",

取得範本中資源的資源識別碼:

nic1.id
[resourceId('Microsoft.Network/networkInterfaces', variables('nic1Name'))]

迴圈

逐一查看陣列中的項目或計數:

[for storageName in storageAccountNames: {
  ...
}]
"copy": {
  "name": "storagecopy",
  "count": "[length(parameters('storageAccountNames'))]"
},
...

資源相依性

針對 Bicep,您可以設定明確相依性,但不建議使用此方法。 相反地,請依賴隱含相依性。 當某一個資源宣告會參考另一個資源的識別碼時,就會建立隱含相依性。

以下顯示對網路安全性群組具有隱含相依性的網路介面。 其會透過 netSecurityGroup.id 來參考網路安全性群組。

resource netSecurityGroup 'Microsoft.Network/networkSecurityGroups@2022-11-01' = {
  ...
}

resource nic1 'Microsoft.Network/networkInterfaces@2022-11-01' = {
  name: nic1Name
  location: location
  properties: {
    ...
    networkSecurityGroup: {
      id: netSecurityGroup.id
    }
  }
}

如果您必須設定明確相依性,請使用:

dependsOn: [ storageAccount ]
"dependsOn": ["[resourceId('Microsoft.Storage/storageAccounts', 'parameters('storageAccountName'))]"]

參考資源

從範本中的資源取得屬性:

storageAccount.properties.primaryEndpoints.blob
[reference(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))).primaryEndpoints.blob]

從未部署於範本中的現有資源取得屬性:

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

// use later in template as often as needed
storageAccount.properties.primaryEndpoints.blob
// required every time the property is needed
"[reference(resourceId('Microsoft.Storage/storageAccounts/', parameters('storageAccountName')), '2019-06-01').primaryEndpoints.blob]"

在 Bicep 中,使用巢狀存取子 (::) 來取得父資源內巢狀資源的屬性:

VNet1::Subnet1.properties.addressPrefix

針對 JSON,請使用 reference 函式:

[reference(resourceId('Microsoft.Network/virtualNetworks/subnets', variables('subnetName'))).properties.addressPrefix]

輸出

從範本中的資源輸出屬性:

output hostname string = publicIP.properties.dnsSettings.fqdn
"outputs": {
  "hostname": {
    "type": "string",
    "value": "[reference(resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))).dnsSettings.fqdn]"
  },
}

有條件地輸出值:

output hostname string = condition ? publicIP.properties.dnsSettings.fqdn : ''
"outputs": {
  "hostname": {
    "condition": "[variables('condition')]",
    "type": "string",
    "value": "[reference(resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))).dnsSettings.fqdn]"
  }
}

Bicep 三元運算子相當於 ARM 範本 JSON 中的 if 函式,而非 condition 屬性。 三元語法必須評估為某一個值或另一個值。 如果上述範例中的條件為 false,Bicep 會輸出含有空字串的主機名稱,但 JSON 不會輸出任何值。

程式碼重複使用

將解決方案分成多個檔案:

下一步