Share via


教學課程:將範本規格部署為連結的範本

了解如何使用連結的部署來部署現有的範本規格。 您可以使用範本規格,與組織中其他的使用者分享 ARM 範本。 建立範本規格之後,您可以使用 Azure PowerShell 或 Azure CLI 來部署範本規格。 您也可以使用連結的範本,將範本規格部署為解決方案的一部分。

必要條件

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

注意

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

建立範本規格

遵循快速入門:建立及部署範本規格,以建立用於部署儲存體帳戶的範本規格。 在下一節中,您需要範本規格的資源組織名稱、範本規格名稱和範本規格版本。

建立主要範本

若要在 ARM 範本中部署範本規格,請將部署資源新增至您的主要範本。 在 templateLink 屬性中,請指定範本規格的資源識別碼。使用名為 azuredeploy.json的下列 JSON 建立範本。 本教學課程假設您儲存至路徑 c:\Templates\deployTS\azuredeploy.json,但是您可以使用任何路徑。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    },
    "tsResourceGroup":{
      "type": "string",
      "metadata": {
        "Description": "Specifies the resource group name of the template spec."
      }
    },
    "tsName": {
      "type": "string",
      "metadata": {
        "Description": "Specifies the name of the template spec."
      }
    },
    "tsVersion": {
      "type": "string",
      "defaultValue": "1.0.0.0",
      "metadata": {
        "Description": "Specifies the version the template spec."
      }
    },
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "metadata": {
        "Description": "Specifies the storage account type required by the template spec."
      }
    }
  },
  "variables": {
    "appServicePlanName": "[concat('plan', uniquestring(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2016-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": "2020-10-01",
      "name": "createStorage",
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "id": "[resourceId(parameters('tsResourceGroup'), 'Microsoft.Resources/templateSpecs/versions', parameters('tsName'), parameters('tsVersion'))]"
        },
        "parameters": {
          "storageAccountType": {
            "value": "[parameters('storageAccountType')]"
          }
        }
      }
    }
  ],
  "outputs": {
    "templateSpecId": {
      "type": "string",
      "value": "[resourceId(parameters('tsResourceGroup'), 'Microsoft.Resources/templateSpecs/versions', parameters('tsName'), parameters('tsVersion'))]"
    }
  }
}

範本規格識別碼是使用 resourceID() 函式所產生。 如果 templateSpec 位於與目前部署的相同資源群組中,則 resourceID () 函式中的資源群組引數是選擇性的。 您也可以直接傳入資源識別碼做為參數。 若要取得識別碼,請使用:

$id = (Get-AzTemplateSpec -ResourceGroupName $resourceGroupName -Name $templateSpecName -Version $templateSpecVersion).Versions.Id

將參數傳遞給範本規格的語法如下:

"parameters": {
  "storageAccountType": {
    "value": "[parameters('storageAccountType')]"
  }
}

注意

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

部署範本

當您部署連結的範本時,會同時部署 Web 應用程式和儲存體帳戶。 部署過程與部署其他 ARM 範本相同。

New-AzResourceGroup `
  -Name webRG `
  -Location westus2

New-AzResourceGroupDeployment `
  -ResourceGroupName webRG `
  -TemplateFile "c:\Templates\deployTS\azuredeploy.json" `
  -tsResourceGroup templateSpecRg `
  -tsName storageSpec `
  -tsVersion 1.0

下一步

若要了解如何建立包含連結範本的範本規格,請參閱建立連結範本的範本規格