共用變數檔案範本

減少 Bicep 檔案中共用值的重複。 相反地,從 Bicep 檔案內的共用 JSON 檔案載入這些值。 使用陣列時,請將共用值與 Bicep 程式代碼中的部署特定值串連。

內容和問題

當您撰寫 Bicep 程式代碼時,您可能會有一組 Bicep 檔案中重複使用的常見變數。 每次宣告資源時,您可以重複這些值,例如在您的 Bicep 檔案之間複製並貼上這些值。 不過,這種方法很容易發生錯誤,而且當您需要進行變更時,您必須更新每個資源定義,使其與其他資源保持同步。

此外,當您使用定義為陣列的變數時,可能會有一些通用值在多個 Bicep 檔案中共享,而且也需要為您要部署的資源添加特定值。 當您混合共用變數與資源特定的變數時,人們很難瞭解這兩個變數類別之間的差異。

解決方法

建立 JSON 檔案,其中包含您需要共用的變數。 使用函 loadJsonContent() 式 載入檔案並存取變數。 針對陣列變數,請使用 函數concat()來結合共用值與特定資源的任何自定義值。

範例 1:命名前置詞

假設您有多個用來定義資源的 Bicep 檔案。 您必須針對所有資源使用一致的命名前置詞。

定義 JSON 檔案,其中包含跨公司套用的常見命名前置詞:

{
  "storageAccountPrefix": "stg",
  "appServicePrefix": "app"
}

在您的 Bicep 檔案中,宣告匯入共用命名前置詞的變數:

var sharedNamePrefixes = loadJsonContent('./shared-prefixes.json')

當您定義資源名稱時,請使用字串插補將共享名稱的前綴與獨特的名稱後綴組合起來:

var appServiceAppName = '${sharedNamePrefixes.appServicePrefix}-myapp-${uniqueString(resourceGroup().id)}'
var storageAccountName = '${sharedNamePrefixes.storageAccountPrefix}myapp${uniqueString(resourceGroup().id)}'

範例 2:網路安全組規則

假設您有多個 Bicep 檔案,每個檔案定義自己的網路安全群組 (NSG)。 您有一組必須套用到每個 NSG 的共通安全規則,然後還有必須新增的應用程式專屬規則。

定義 JSON 檔案,其中包含跨公司套用的常見安全性規則:

{
  "securityRules": [
    {
      "name": "Allow_RDP_from_company_IP_address",
      "properties": {
        "description": "Allow inbound RDP from the company's IP address range.",
        "protocol": "Tcp",
        "sourceAddressPrefix": "203.0.113.0/24",
        "sourcePortRange": "*",
        "destinationAddressPrefix": "VirtualNetwork",
        "destinationPortRange": "3389",
        "access": "Allow",
        "priority": 100,
        "direction": "Inbound"
      }
    },
    {
      "name": "Allow_VirtualNetwork_to_Storage",
      "properties": {
        "description": "Allow outbound connections to the Azure Storage service tag.",
        "protocol": "Tcp",
        "sourceAddressPrefix": "VirtualNetwork",
        "sourcePortRange": "*",
        "destinationAddressPrefix": "Storage",
        "destinationPortRange": "*",
        "access": "Allow",
        "priority": 100,
        "direction": "Outbound"
      }
    }
    // other rules here
  ]
}

在您的 Bicep 檔案中,宣告匯入共用安全性規則的變數:

var sharedRules = loadJsonContent('./shared-rules.json', 'securityRules')

建立代表此特定 NSG 的自訂規則的變數陣列。

var customRules = [
  {
    name: 'Allow_Internet_HTTPS_Inbound'
    properties: {
      description: 'Allow inbound internet connectivity for HTTPS only.'
      protocol: 'Tcp'
      sourcePortRange: '*'
      destinationPortRange: '443'
      sourceAddressPrefix: 'Internet'
      destinationAddressPrefix: 'VirtualNetwork'
      access: 'Allow'
      priority: 400
      direction: 'Inbound'
    }
  }
]

定義 NSG 資源。 使用 函 concat() 式將兩個陣列結合在一起,並設定 securityRules 屬性:

resource nsg 'Microsoft.Network/networkSecurityGroups@2021-08-01' = {
  name: nsgName
  location: location
  properties: {
    securityRules: concat(sharedRules, customRules)
  }
}

考慮事項

  • 當您使用此方法時,JSON 檔案會包含在 Bicep 所產生的 ARM 範本內。 Bicep 所產生的 JSON ARM 範本有 4MB 的檔案限制,因此請務必避免使用大型共用變數檔案。
  • 請確定您的共享變數陣列不會與每個 Bicep 檔案中指定的陣列值衝突。 例如,使用組態集模式來定義網路安全組時,請確定您沒有定義相同優先順序和方向的多個規則。

後續步驟

瞭解組態集模式。