教學課程:將變數新增至 Azure Resource Manager 範本

在本教學課程中,您將瞭解如何將變數新增至 Azure Resource Manager 範本 (ARM 範本)。 變數可簡化您的範本。 它們可讓您編寫一次表達式,並在整個範本中重複使用它。 本教學課程需要 7 分鐘 才能完成。

先決條件

建議您完成 函式的教學課程,但這不是必要條件。

您必須有 Visual Studio Code,以及 Azure PowerShell 或 Azure CLI。 如需詳細資訊,請參閱 範本工具。

審查模板

在上一個教學課程的結尾,您的範本具有下列 JSON 檔案:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageName": {
      "type": "string",
      "minLength": 3,
      "maxLength": 24
    },
    "storageSKU": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Standard_ZRS",
        "Premium_LRS",
        "Premium_ZRS",
        "Standard_GZRS",
        "Standard_RAGZRS"
      ]
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-06-01",
      "name": "[parameters('storageName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

您的 Azure 儲存體帳戶 名稱必須是唯一的,才能輕鬆地繼續建置 ARM 範本。 如果您已經完成本系列的先前教學課程,您可能已經感到厭倦想出一個獨特的名稱。 您可以新增變數來解決此問題,以建立儲存體帳戶的唯一名稱。

使用變數

下列範例顯示將變數新增至範本的變更,以建立唯一的儲存體帳戶名稱。 將整個檔案複製,並以其內容替換您的範本:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "type": "string",
      "minLength": 3,
      "maxLength": 11
    },
    "storageSKU": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Standard_ZRS",
        "Premium_LRS",
        "Premium_ZRS",
        "Standard_GZRS",
        "Standard_RAGZRS"
      ]
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "variables": {
    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-06-01",
      "name": "[variables('uniqueStorageName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

請注意,它包含一個名為 uniqueStorageName的變數。 此變數使用四個函數來建立字串值。

您已經熟悉 參數 函數,因此我們不會檢查它。

您也熟悉 resourceGroup 函式。 在此情況下,您會取得 id 屬性而不是 location 屬性,如上一個教學課程所示。 屬性 id 會傳回資源群組的完整識別碼,包括訂用帳戶識別碼和資源群組名稱。

uniqueString 函數會建立 13 個字元的雜湊值。 您傳遞的參數會決定傳回值。 在本教學課程中,您會使用資源群組識別碼作為雜湊值的輸入。 這表示您可以將此範本部署至不同的資源群組,並取得不同的唯一字串值。 不過,如果您部署至相同的資源群組,您會取得相同的值。

concat 函數會取得值並將它們組合起來。 對於此變數,它會從參數中取得字串,並從函數中 uniqueString 取得字串,並將它們組合成一個字串。

此 storagePrefix 參數可讓您傳入前置詞,以協助您識別儲存體帳戶。 您可以建立自己的命名慣例,以便在部署之後,從廣泛的資源清單中更輕鬆地識別儲存體帳戶。

最後,請注意,儲存體帳戶名稱現在會設定為變數,而不是參數。

部署範本

讓我們部署範本。 部署此範本比先前的範本更容易,因為您只提供儲存體帳戶名稱的前置詞。

如果您尚未建立資源群組,請參閱 建立資源群組。 此範例假設您已將變數設定 templateFile 為範本檔案的路徑,如 第一個教學課程所示。

New-AzResourceGroupDeployment `
  -Name addnamevariable `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $templateFile `
  -storagePrefix "store" `
  -storageSKU Standard_LRS

備註

如果部署失敗,請使用 verbose 交換器來取得所建立資源的相關資訊。 使用 debug 參數取得更多資訊,以進行偵錯。

確認部署

您可以從 Azure 入口網站探索資源群組來驗證部署。

  1. 登入 Azure 入口網站。
  2. 從左側功能表中,選取 [資源群組]。
  3. 選取您的資源群組。
  4. 請注意,您部署的儲存體帳戶名稱是 store,加上一串隨機字元。

清理資源

如果您要繼續進行下一個教學課程,則不需要刪除資源群組。

如果您現在要停止,您可能想要刪除資源群組。

  1. 從 Azure 入口網站,從左側功能表中選取 [資源群組]。
  2. 在 [篩選任何欄位... ] 文字欄位中輸入資源群組名稱。
  3. 核取 myResourceGroup 旁邊的方塊,然後選取 myResourceGroup 或您的資源群組名稱。
  4. 從頂端功能表選取 [刪除資源群組 ]。

後續步驟

在本教學課程中,您會新增變數來建立唯一的儲存體帳戶名稱。 在下一個教學課程中,您會從已部署的儲存體帳戶傳回值。