教學課程:使用連結的範本建立範本規格

了解如何使用主要範本和連結的範本,建立範本規格。 您可以使用範本規格,與組織中的其他使用者分享 ARM 範本。 本文說明如何使用部署資源relativePath 屬性,建立範本規格以封裝主要範本及其連結的範本。

必要條件

具有有效訂用帳戶的 Azure 帳戶。 免費建立帳戶

注意

若使用 Azure PowerShell 時要搭配範本規格,您必須安裝 5.0.0 版或更新版本。 若要與 Azure CLI 搭配使用,請使用 2.14.2 版或更新版本

建立連結的範本

建立主要範本和連結的範本。

若要連結到範本,請將部署資源新增至主要範本。 請在 templateLink 屬性中,根據父範本的路徑,指定連結範本的相對路徑。

連結的範本稱為 linkedTemplate.json,會儲存在存放主要範本且名稱為成品的子資料夾中。 您可以在 relativePath 中使用下列其中一個值:

  • ./artifacts/linkedTemplate.json
  • /artifacts/linkedTemplate.json
  • artifacts/linkedTemplate.json

relativePath 屬性一律相對於宣告 relativePath 的範本檔案,因此,如果有另一個從 linkedTemplate.json 呼叫的 linkedTemplate2.json,且 linkedTemplate2.json 儲存在同一個成品子資料夾中,則 linkedtemplate.json 中指定的 relativePath 就只會是 linkedTemplate2.json

  1. 使用下列 JSON 建立主要範本。 將主要範本以 azuredeploy.json 儲存到本機電腦。 本教學課程假設您儲存到了路徑 c:\Templates\linkedTS\azuredeploy.json,但是您可以使用任何路徑。

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "location": {
          "type": "string",
          "defaultValue": "westus2",
          "metadata":{
            "description": "Specify the location for the resources."
          }
        },
        "storageAccountType": {
          "type": "string",
          "defaultValue": "Standard_LRS",
          "metadata":{
            "description": "Specify the storage account type."
          }
        }
      },
      "variables": {
        "appServicePlanName": "[format('plan{0}', uniquestring(resourceGroup().id))]"
      },
      "resources": [
        {
          "type": "Microsoft.Web/serverfarms",
          "apiVersion": "2022-09-01",
          "name": "[variables('appServicePlanName')]",
          "location": "[parameters('location')]",
          "sku": {
            "name": "B1",
            "tier": "Basic",
            "size": "B1",
            "family": "B",
            "capacity": 1
          },
          "kind": "linux",
          "properties": {
            "perSiteScaling": false,
            "reserved": true,
            "targetWorkerCount": 0,
            "targetWorkerSizeId": 0
          }
        },
        {
          "type": "Microsoft.Resources/deployments",
          "apiVersion": "2022-09-01",
          "name": "createStorage",
          "properties": {
            "mode": "Incremental",
            "templateLink": {
              "relativePath": "artifacts/linkedTemplate.json"
            },
            "parameters": {
              "storageAccountType": {
                "value": "[parameters('storageAccountType')]"
              }
            }
          }
        }
      ]
    }
    

    注意

    Microsoft.Resources/deployments 的 apiVersion 必須是 2020-06-01 或更新的版本。

  2. 在儲存主要範本的資料夾中,建立稱為成品的目錄。

  3. 使用下列 JSON 建立連結的範本:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "storageAccountType": {
          "type": "string",
          "defaultValue": "Standard_LRS",
          "allowedValues": [
            "Standard_LRS",
            "Standard_GRS",
            "Standard_ZRS",
            "Premium_LRS"
          ],
          "metadata": {
            "description": "Storage Account type"
          }
        },
        "location": {
          "type": "string",
          "defaultValue": "[resourceGroup().location]",
          "metadata": {
            "description": "Location for all resources."
          }
        }
      },
      "variables": {
        "storageAccountName": "[format('store{0}', uniquestring(resourceGroup().id))]"
      },
      "resources": [
        {
          "type": "Microsoft.Storage/storageAccounts",
          "apiVersion": "2022-09-01",
          "name": "[variables('storageAccountName')]",
          "location": "[parameters('location')]",
          "sku": {
            "name": "[parameters('storageAccountType')]"
          },
          "kind": "StorageV2",
          "properties": {}
        }
      ],
      "outputs": {
        "storageAccountName": {
          "type": "string",
          "value": "[variables('storageAccountName')]"
        }
      }
    }
    
  4. 成品資料夾中,將範本儲存為 linkedTemplate.json

建立範本規格

範本規格會儲存在資源群組中。 請建立資源群組,然後使用下列指令碼建立範本規格。 範本規格名稱為 webSpec

New-AzResourceGroup `
  -Name templateSpecRG `
  -Location westus2

New-AzTemplateSpec `
  -Name webSpec `
  -Version "1.0.0.0" `
  -ResourceGroupName templateSpecRG `
  -Location westus2 `
  -TemplateFile "c:\Templates\linkedTS\azuredeploy.json"

完成後即可從 Azure 入口網站或使用下列 Cmdlet,檢視範本規格:

Get-AzTemplateSpec -ResourceGroupName templatespecRG -Name webSpec

部署範本規格

您現在可以部署範本規格。部署範本規格就如同部署其包含的範本,不同的是您會傳入範本規格的資源識別碼。您可以使用相同的部署命令,並視需要傳入範本規格的參數值。

New-AzResourceGroup `
  -Name webRG `
  -Location westus2

$id = (Get-AzTemplateSpec -ResourceGroupName templateSpecRG -Name webSpec -Version "1.0.0.0").Versions.Id

New-AzResourceGroupDeployment `
  -TemplateSpecId $id `
  -ResourceGroupName webRG

下一步

若要了解如何將範本規格部署為連結的範本,請參閱 教學課程:將範本規格部署為連結的範本