分享方式:


共用變數檔案模式

減少重複使用 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)
  }
}

考量

  • 使用此方法時,Bicep 所產生的 ARM 範本將包含該 JSON 檔案。 Bicep 所產生的 JSON ARM 範本的檔案限制為 4MB,因此請務必避免使用大型的共用變數檔案。
  • 確定您的共用變數陣列未與各 Bicep 檔案中的指定陣列值有所衝突。 例如,使用設定集模式來定義網路安全性群組時,請確保沒有多個定義相同優先順序及方向的規則。

下一步

深入了解設定集模式。