部署適用于測試環境的嵌套 Azure Resource Manager 範本Deploy nested Azure Resource Manager templates for testing environments

嵌套部署可讓您從主要 Resource Manager 範本內執行其他 Azure Resource Manager 範本。A nested deployment allows you to execute other Azure Resource Manager templates from within a main Resource Manager template. 它可讓您將部署分解成一組目標和特定用途的範本。It enables you to decompose your deployment into a set of targeted and purpose-specific templates. 它提供測試、重複使用及可讀性方面的優點。It provides benefits in terms of testing, reuse, and readability. 部署 Azure 資源時使用連結的範本 ,可讓您大致瞭解此解決方案的數個程式碼範例。The article Using linked templates when deploying Azure resources provides a good overview of this solution with several code samples. 本文提供 Azure DevTest Labs 特定的範例。This article provides an example that's specific to Azure DevTest Labs.

金鑰參數Key parameters

雖然您可以從頭開始建立自己的 Resource Manager 範本,但我們建議您在 Visual Studio 中使用 Azure 資源群組專案 ,這可讓您輕鬆地開發和偵錯工具範本。While you can create your own Resource Manager template from scratch, we recommend that you use the Azure Resource Group project in Visual Studio, which makes it easy to develop and debug templates. 當您將嵌套部署資源加入至 azuredeploy.js時,Visual Studio 會新增數個專案,讓範本更具彈性。When you add a nested deployment resource to azuredeploy.json, Visual Studio adds several items to make the template more flexible. 這些專案包括具有次要範本和參數檔案的子資料夾、主要範本檔案內的變數名稱,以及新檔案儲存位置的兩個參數。These items include the subfolder with the secondary template and parameters file, variable names within the main template file, and two parameters for the storage location for the new files. _ArtifactsLocation_ArtifactsLocationSasToken 是 DevTest Labs 使用的主要參數。The _artifactsLocation and _artifactsLocationSasToken are the key parameters that the DevTest Labs uses.

如果您不熟悉 DevTest Labs 與環境的運作方式,請參閱 使用 Azure Resource Manager 範本建立多個 VM 環境和 PaaS 資源If you aren't familiar with how the DevTest Labs works with environments, see Create multi-VM environments and PaaS resources with Azure Resource Manager templates. 您的範本會儲存在連結至 DevTest Labs 中的實驗室的儲存機制中。Your templates are stored in the repository linked to the lab in DevTest Labs. 當您使用這些範本建立新的環境時,這些檔案會移至實驗室中 Azure 儲存體的容器。When you create a new environment with those templates, the files are moved into an Azure Storage container in the lab. 為了能夠識別和複製嵌套的檔案,DevTest Labs 會識別 _artifactsLocation 並 _artifactsLocationSasToken 參數,並將子資料夾複製到儲存體容器。To be able to identify and copy the nested files, DevTest Labs identifies the _artifactsLocation and _artifactsLocationSasToken parameters and copies the subfolders up to the storage container. 然後,它會自動將位置和共用存取簽章 (SaS) 權杖插入參數中。Then, it automatically inserts the location and Shared Access Signature (SaS) token into parameters.

嵌套部署範例Nested deployment example

以下是一個簡單的嵌套部署範例:Here is a simple example of a nested deployment:


"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "_artifactsLocation": {
        "type": "string"
    },
    "_artifactsLocationSasToken": {
        "type": "securestring"
    }},
"variables": {
    "NestOneTemplateFolder": "nestedtemplates",
    "NestOneTemplateFileName": "NestOne.json",
    "NestOneTemplateParametersFileName": "NestOne.parameters.json"},
    "resources": [
    {
        "name": "NestOne",
        "type": "Microsoft.Resources/deployments",
        "apiVersion": "2016-09-01",
        "dependsOn": [ ],
        "properties": {
            "mode": "Incremental",
            "templateLink": {
                "uri": "[concat(parameters('_artifactsLocation'), '/', variables('NestOneTemplateFolder'), '/', variables('NestOneTemplateFileName'), parameters('_artifactsLocationSasToken'))]",
                "contentVersion": "1.0.0.0"
            },
            "parametersLink": {
                "uri": "[concat(parameters('_artifactsLocation'), '/', variables('NestOneTemplateFolder'), '/', variables('NestOneTemplateParametersFileName'), parameters('_artifactsLocationSasToken'))]",
                "contentVersion": "1.0.0.0"
            }
        }    
    }],
"outputs": {}

存放庫中包含此範本的資料夾會有一個子資料夾, nestedtemplates 其中包含 NestOne.js 的檔案,並 NestOne.parameters.jsThe folder in the repository containing this template has a subfolder nestedtemplates with the files NestOne.json and NestOne.parameters.json. 在的 azuredeploy.js 中,範本的 URI 是使用成品位置、嵌套的範本資料夾、嵌套的範本檔案名所建立。In the azuredeploy.json, URI for the template is built using the artifacts location, nested template folder, nested template file name. 同樣地,參數的 URI 是使用內建範本的成品位置、嵌套的範本資料夾和參數檔來建立。Similarly, URI for the parameters is built using the artifacts location, nested template folder, and parameter file for the nested template.

以下是 Visual Studio 中相同專案結構的影像:Here is the image of the same project structure in Visual Studio:

Visual Studio 中的專案結構

您可以在主資料夾中加入其他資料夾,但不能在單一層級上新增任何更多資料夾。You can add additional folders in the primary folder but not any deeper than a single level.

下一步Next steps

如需環境的詳細資訊,請參閱下列文章:See the following articles for details about environments: