你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

教程:从 Azure 门户使用导出的模板

在本教程系列中,创建一个用于部署 Azure 存储帐户的模板。 在接下来的两篇教程中,你将添加一个应用服务计划和一个网站。 本教程介绍如何从 Azure 门户导出模板(无需从头开始创建模板),以及如何使用 Azure 快速入门模板中的示例模板。 你可以根据自己的用途自定义这些模板。 本教程重点介绍如何导出模板以及自定义模板的结果。 完成此说明需要 14 分钟。

先决条件

建议完成有关输出的教程,但这不是一项要求。

需要具有 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": "2021-09-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]"
    }
  }
}

此模板非常适合用于部署存储帐户,但可以在其中添加更多的资源。 可以从现有的资源导出模板,以快速获取该资源的 JSON。

创建应用服务计划

  1. 登录 Azure 门户

  2. 选择“创建资源”。

  3. 在“搜索服务及市场”中输入“应用服务计划”,然后选择“应用服务计划”。

  4. 选择“创建”。

  5. 在“创建应用服务计划”页输入以下内容:

    • 订阅:从下拉菜单选择 Azure 订阅。
    • 资源组:选择“新建”,然后指定名称。 提供的资源组名称应该与曾在本教程系列中使用的名称不同。
    • 名称:输入应用服务计划的名称。
    • 操作系统:选择“Linux”。
    • 区域:从下拉菜单中选择 Azure 位置,例如“美国中部”。
    • 定价层:若要节省成本,对于要求较低的工作负载,请在“开发/测试”下选择“更改大小”,将“SKU 和大小”更改为“第一个基本(B1)”。

    Azure 门户中的“创建应用服务计划”页的屏幕截图。

  6. 选择“查看并创建”。

  7. 选择“创建” 。 创建资源需要花费片刻时间。

导出模板

  1. 选择“转到资源”。

    Azure 门户中的“转到资源”按钮的屏幕截图。

  2. 在左侧菜单的“自动化”下,选择“导出模板”。

    Azure 门户中的“导出模板”选项的屏幕截图。

    导出模板功能将提取资源的当前状态,并生成用于部署该资源的模板。 导出模板可能有助于快速获取部署资源所需的 JSON。

  3. 查看导出的模板中的 Microsoft.Web/serverfarms 定义和参数定义。 不需要复制这些部分。 可以使用此导出的模板作为示例,了解如何将此资源添加到模板。

    Azure 门户中导出的模板 JSON 代码的屏幕截图。

重要

通常,导出的模板比创建模板时所需的信息更详细。 例如,导出的模板中的 SKU 对象具有五个属性。 此模板是可行的,但你只需使用 name 属性。 可以从导出的模板开始,然后根据要求对其进行修改。

修订现有模板

导出的模板提供所需的大部分 JSON,但你需要根据模板自定义这些 JSON。 请特别注意你的模板与导出的模板之间的参数和变量差异。 很明显,导出过程并不知道你已在模板中定义的参数和变量。

以下示例突出显示了在模板中添加的内容。 其中包含导出的代码以及一些更改。 第一,它会更改参数的名称以符合命名约定。 第二,它对应用服务计划的位置使用 ___location 参数。 第三,它删除默认值正常的所有属性。

复制整个文件,将模板替换为该文件的内容。

{
  "$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]"
    },
    "appServicePlanName": {
      "type": "string",
      "defaultValue": "exampleplan"
    }
  },
  "variables": {
    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-09-01",
      "name": "[variables('uniqueStorageName')]",
      "___location": "[parameters('___location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2021-03-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]"
    }
  }
}

部署模板

使用 Azure CLI 或 Azure PowerShell 来部署模板。

如果尚未创建资源组,请参阅创建资源组。 此示例假设已根据第一篇教程中所述,将 templateFile 变量设置为模板文件的路径。

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

注意

如果部署失败,请使用 verbose 开关获取有关正在创建的资源的信息。 使用 debug 开关获取调试的详细信息。

验证部署

可以通过在 Azure 门户中浏览资源组来验证部署。

  1. 登录 Azure 门户
  2. 在左侧菜单中选择“资源组”。
  3. 选择已部署到的资源组。
  4. 该资源组包含一个存储帐户和一个应用服务计划。

清理资源

若要继续学习下一篇教程,则不需删除该资源组。

如果就此停止学习,请删除该资源组。

  1. 在 Azure 门户上的左侧菜单中选择“资源组” 。
  2. 在“筛选任何字段…”文本字段中键入资源组名称。
  3. 选中“myResourceGroup”旁边的框,然后选择“myResourceGroup”或资源组名称。
  4. 在顶部菜单中选择“删除资源组”。

后续步骤

你已了解如何从 Azure 门户导出模板,以及如何使用导出的模板进行模板开发。 还可以使用 Azure 快速入门模板简化模板开发。