教程:从 Azure 门户使用导出的模板Tutorial: Use exported template from the Azure portal

在本教程系列中,你已创建一个用于部署 Azure 存储帐户的模板。In this tutorial series, you've created a template to deploy an Azure storage account. 在接下来的两篇教程中,你将添加一个应用服务计划和一个网站。 In the next two tutorials, you add an App Service plan and a website. 本教程介绍如何从 Azure 门户导出模板(无需从头开始创建模板),以及如何使用 Azure 快速入门模板中的示例模板。Instead of creating templates from scratch, you learn how to export templates from the Azure portal and how to use sample templates from the Azure Quickstart templates. 你可以根据自己的用途自定义这些模板。You customize those templates for your use. 本教程重点介绍如何导出模板,以及自定义模板的结果。This tutorial focuses on exporting templates, and customizing the result for your template. 完成本教程需要大约 14 分钟It takes about 14 minutes to complete.

先决条件Prerequisites

建议完成有关输出的教程,但这不是一项要求。We recommend that you complete the tutorial about outputs, but it's not required.

必须已安装带有资源管理器工具扩展的 Visual Studio Code,以及 Azure PowerShell 或 Azure CLI。You must have Visual Studio Code with the Resource Manager Tools extension, and either Azure PowerShell or Azure CLI. 有关详细信息,请参阅模板工具For more information, see template tools.

审阅模板Review template

在上一篇教程的结束时,模板包含以下 JSON:At the end of the previous tutorial, your template had the following 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",
        "Premium_LRS",
        "Standard_RAGZRS"
      ]
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "variables": {
    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-04-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]"
    }
  }
}

此模板非常适合用于部署存储帐户,但可以在其中添加更多的资源。This template works well for deploying storage accounts, but you might want to add more resources to it. 可以从现有的资源导出模板,以快速获取该资源的 JSON。You can export a template from an existing resource to quickly get the JSON for that resource.

创建应用服务计划Create App Service plan

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 选择“创建资源”。 Select Create a resource.

  3. 在“搜索市场”中输入“应用服务计划”,然后选择“应用服务计划”。 In Search the Marketplace, enter App Service plan, and then select App Service plan. 请不要选择“应用服务计划(经典)” Don't select App Service plan (classic)

  4. 选择“创建” 。Select Create.

  5. 输入:Enter:

    • 订阅:选择 Azure 订阅。Subscription: select your Azure subscription.
    • 资源组:选择“新建”,然后指定名称。 Resource Group: Select Create new and then specify a name. 提供的资源组名称应该与过去在本教程系列中使用的名称不同。Provide a different resource group name than the one you have been using in this tutorial series.
    • 名称:输入应用服务计划的名称。Name: enter a name for the App service plan.
    • 操作系统:选择“Linux”。 Operating System: select Linux.
    • 区域:选择一个 Azure 位置。Region: select an Azure location. 例如,中国北部For example, China North.
    • 定价层:为了节省成本,请将 SKU 更改为“基本 B1”(在“开发/测试”下)。 Pricing tier: to save costs, change the SKU to Basic B1 (under Dev/Test).

    资源管理器模板 - 在门户中导出模板

  6. 选择“查看并创建”。 Select Review and create.

  7. 选择“创建” 。Select Create. 创建资源需要花费片刻时间。It takes a few moments to create the resource.

导出模板Export template

  1. 选择“转到资源”。 Select Go to resource.

    转到资源

  2. 选择“导出模板” 。Select Export template.

    资源管理器模板 - 导出模板

    导出模板功能将提取资源的当前状态,并生成用于部署该资源的模板。The export template feature takes the current state of a resource and generates a template to deploy it. 导出模板可能有助于快速获取部署资源所需的 JSON。Exporting a template can be a helpful way of quickly getting the JSON you need to deploy a resource.

  3. Microsoft.Web/serverfarms 定义和参数定义复制到模板。Copy the Microsoft.Web/serverfarms definition and the parameter definition to your template.

    资源管理器模板 - 导出模板 - 导出的模板

重要

通常,导出的模板比创建模板时所需的信息更详细。Typically, the exported template is more verbose than you might want when creating a template. 例如,导出的模板中的 SKU 对象包含五个属性。For example, the SKU object in the exported template has five properties. 此模板是可行的,但你只需使用 name 属性。This template works, but you could just use the name property. 可以从导出的模板着手,然后根据要求对其进行修改。You can start with the exported template, and then modify it as you like to fit your requirements.

修订现有模板Revise existing template

导出的模板提供所需的大部分 JSON,但你需要根据模板自定义这些 JSON。The exported template gives you most of the JSON you need, but you need to customize it for your template. 请特别注意你的模板与导出的模板之间的参数和变量差异。Pay particular attention to differences in parameters and variables between your template and the exported template. 很明显,导出过程并不知道你已在模板中定义的参数和变量。Obviously, the export process doesn't know the parameters and variables that you've already defined in your template.

以下示例突出显示了在模板中添加的内容。The following example highlights the additions to your template. 其中包含导出的代码以及一些更改。It contains the exported code plus some changes. 第一,它会更改参数的名称以符合命名约定。First, it changes the name of the parameter to match your naming convention. 第二,它对应用服务计划的位置使用 location 参数。Second, it uses your location parameter for the location of the app service plan. 第三,它会删除 properties 对象中的 name,因为在资源级别,此值对于 name 属性是多余的。Third, it removes the name inside the properties object because this value is redundant with the name property at the resource level.

复制整个文件,将模板替换为该文件的内容。Copy the whole file and replace your template with its contents.

{
  "$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",
        "Premium_LRS",
        "Standard_RAGZRS"
      ]
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    },
    "appServicePlanName": {
      "type": "string",
      "defaultValue": "exampleplan"
    }
  },
  "variables": {
    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-04-01",
      "name": "[variables('uniqueStorageName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2016-09-01",
      "name": "[parameters('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
      }
    }
  ],
  "outputs": {
    "storageEndpoint": {
      "type": "object",
      "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]"
    }
  }
}

部署模板Deploy template

使用 Azure CLI 或 Azure PowerShell 来部署模板。Use either Azure CLI or Azure PowerShell to deploy a template.

如果尚未创建资源组,请参阅创建资源组If you haven't created the resource group, see Create resource group. 此示例假设已根据第一篇教程中所述,将 templateFile 变量设置为模板文件的路径。The example assumes you've set the templateFile variable to the path to the template file, as shown in the first tutorial.

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

备注

如果部署失败,请将 debug 开关和部署命令配合使用来显示调试日志。If the deployment failed, use the debug switch with the deployment command to show the debug logs. 还可以使用 verbose 开关来显示完整的调试日志。You can also use the verbose switch to show the full debug logs.

验证部署Verify deployment

可以通过在 Azure 门户中浏览资源组来验证部署。You can verify the deployment by exploring the resource group from the Azure portal.

  1. 登录到 Azure 门户Sign in to the Azure portal.
  2. 在左侧菜单中选择“资源组”。 From the left menu, select Resource groups.
  3. 选择已部署到的资源组。Select the resource group you deployed to.
  4. 该资源组包含一个存储帐户和一个应用服务计划。The resource group contains a storage account and an App Service plan.

清理资源Clean up resources

若要继续学习下一篇教程,则不需删除该资源组。If you're moving on to the next tutorial, you don't need to delete the resource group.

如果你不打算继续学习,请删除该资源组以清理部署的资源。If you're stopping now, you might want to clean up the resources you deployed by deleting the resource group.

  1. 在 Azure 门户上的左侧菜单中选择“资源组” 。From the Azure portal, select Resource group from the left menu.
  2. 在“按名称筛选”字段中输入资源组名称。 Enter the resource group name in the Filter by name field.
  3. 选择资源组名称。Select the resource group name.
  4. 在顶部菜单中选择“删除资源组”。 Select Delete resource group from the top menu.

后续步骤Next steps

你已了解如何从 Azure 门户导出模板,以及如何使用导出的模板进行模板开发。You learned how to export a template from the Azure portal, and how to use the exported template for your template development. 你还可以使用 Azure 快速入门模板来简化模板开发。You can also use the Azure Quickstart templates to simplify template development.