共用方式為


教學指南:將輸出新增至 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": {
    "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
      }
    }
  ]
}

它會部署儲存體帳戶,但不會傳回任何相關資訊。 您可能需要從新資源擷取屬性,以便稍後可供參考。

新增輸出

您可以使用輸出從範本傳回值。 例如,可能有助於取得新儲存體帳戶的端點。

以下範例顯示對範本的變更,以便新增輸出值。 將整個檔案複製,並以其內容替換您的範本:

{
  "$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
      }
    }
  ],
  "outputs": {
    "storageEndpoint": {
      "type": "object",
      "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]"
    }
  }
}

關於您添加的輸出值,有一些重要項目需要注意。

傳回值的類型會設定為 object,這表示它會傳回 JSON 物件。

它會使用 參考 函式來取得儲存體帳戶的執行階段狀態。 若要取得資源的執行階段狀態,請傳遞資源的名稱或識別碼。 在此情況下,您會使用用來建立儲存體帳戶名稱的相同變數。

最後,它會從儲存體帳戶傳回 primaryEndpoints 屬性。

部署範本

您已準備好部署範本並查看傳回值。

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

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

在部署命令的輸出中,只有在輸出為 JSON 格式時,您才會看到類似下列範例的物件:

{
    "dfs": "https://storeluktbfkpjjrkm.dfs.core.windows.net/",
    "web": "https://storeluktbfkpjjrkm.z19.web.core.windows.net/",
    "blob": "https://storeluktbfkpjjrkm.blob.core.windows.net/",
    "queue": "https://storeluktbfkpjjrkm.queue.core.windows.net/",
    "table": "https://storeluktbfkpjjrkm.table.core.windows.net/",
    "file": "https://storeluktbfkpjjrkm.file.core.windows.net/"
}

備註

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

檢查您的工作

在過去的六個教程中,您做了很多事。 讓我們花點時間回顧一下你所做的事情。 您已建立範本,其中包含易於提供的參數。 該模板可在不同的環境中重複使用,因為它允許自定義並動態創建所需的值。 它也會傳回您可以在腳本中使用的儲存體帳戶的相關資訊。

現在,讓我們看看資源群組和部署歷程記錄。

  1. 登入 Azure 入口網站

  2. 從左側功能表中,選取 [資源群組]。

  3. 選取您部署的資源群組。

  4. 視您執行的步驟而定,資源群組中至少應該有一個,也許有數個儲存體帳戶。

  5. 您也應該在歷程記錄中列出數個成功的部署。 選取該連結。

    顯示部署連結的 Azure 入口網站螢幕擷取畫面。

  6. 您可以在歷史紀錄中查看所有部署。 選取名為 addoutputs 的部署。

    顯示部署歷程記錄的 Azure 入口網站螢幕擷取畫面。

  7. 您可以檢閱輸入。

    顯示部署輸入的 Azure 入口網站螢幕擷取畫面。

  8. 您可以查看輸出。

    顯示部署輸出的 Azure 入口網站螢幕擷取畫面。

  9. 您可以檢閱範本。

    顯示部署範本的 Azure 入口網站螢幕擷取畫面。

清理資源

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

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

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

後續步驟

本教學中,您已將返回值新增至範本。 在下一個教學課程中,您將瞭解如何匯出範本,並在範本中使用該匯出範本的部分。