您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

从现有资源导出 Azure Resource Manager 模板Export an Azure Resource Manager template from existing resources

本文介绍如何从订阅中的现有资源导出 Resource Manager 模板。In this article, you learn how to export a Resource Manager template from existing resources in your subscription. 可以使用该生成的模板更好地了解模板语法。You can use that generated template to gain a better understanding of template syntax.

可以通过两种方式来导出模板:There are two ways to export a template:

  • 可以导出用于部署的实际模板。You can export the actual template used for deployment. 导出的模板中包括的所有参数和变量与原始模板中显示的完全一样。The exported template includes all the parameters and variables exactly as they appeared in the original template. 在已通过门户部署资源的情况下,若需了解如何通过模板来创建这些资源,则可使用此方法。This approach is helpful when you deployed resources through the portal, and want to see the template to create those resources. 此模板可随时使用。This template is readily usable.
  • 可以导出已生成的表示资源组当前状态的模板。You can export a generated template that represents the current state of the resource group. 导出的模板不基于任何已用于部署的模板。The exported template is not based on any template that you used for deployment. 与之相反,它所创建的模板为资源组的快照。Instead, it creates a template that is a snapshot of the resource group. 导出的模板会有许多硬编码的值,其参数可能没有定义的那么多。The exported template has many hard-coded values and probably not as many parameters as you would typically define. 如果已在部署后修改资源组,则可使用此方法。This approach is useful when you have modified the resource group after deployment. 此模板通常需要修改才能使用。This template usually requires modifications before it is usable.

本主题通过门户演示这两种方法。This topic shows both approaches through the portal.

部署资源Deploy resources

让我们开始将资源部署到 Azure,以便将其用作模板导出。Let's start by deploying resources to Azure that you can use for exporting as a template. 如果订阅中已有要导出到模板的资源组,则可跳过此部分。If you already have a resource group in your subscription that you want to export to a template, you can skip this section. 本文其余部分假定你已部署此部分介绍的 Web 应用和 SQL 数据库解决方案。The remainder of this article assumes you have deployed the web app and SQL database solution shown in this section. 如果使用其他解决方案,则体验可能会稍有不同,但导出模板的步骤是相同的。If you use a different solution, your experience might be a little different, but the steps to export a template are the same.

  1. Azure 门户中,选择“新建”。In the Azure portal, select New.

    选择“新建”

  2. 搜索“Web 应用 + SQL”,然后在可用选项中选择它。Search for web app + SQL and select it from the available options.

    搜索 Web 应用和 SQL

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

    选择“创建”

  4. 为 Web 应用和 SQL 数据库提供所需值。Provide the required values for the web app and SQL database. 选择“创建” 。Select Create.

    提供 Web 和 SQL 值

部署可能需要几分钟时间。The deployment may take a minute. 部署完成后,订阅将包含解决方案。After the deployment finishes, your subscription contains the solution.

在部署历史记录中查看模板View template from deployment history

  1. 转到新资源组的资源组边栏选项卡。Go to the resource group blade for your new resource group. 可以看到,该边栏选项卡列出了上次部署的结果。Notice that the blade shows the result of the last deployment. 选择此链接。Select this link.

    资源组边栏选项卡

  2. 可以看到该组的部署历史记录。You see a history of deployments for the group. 在本例中,边栏选项卡可能只列出了一个部署。In your case, the blade probably lists only one deployment. 选择此部署。Select this deployment.

    上次部署

  3. 边栏选项卡会显示部署摘要。The blade displays a summary of the deployment. 摘要包括此部署及其操作的状态,以及为参数提供的值。The summary includes the status of the deployment and its operations and the values that you provided for parameters. 若要查看用于部署的模板,请选择“查看模板”。To see the template that you used for the deployment, select View template.

    查看部署摘要

  4. Resource Manager 检索以下七个文件:Resource Manager retrieves the following seven files for you:

    1. 模板 - 定义解决方案基础结构的模板。Template - The template that defines the infrastructure for your solution. 通过门户创建存储帐户时,Resource Manager 使用模板来部署该存储帐户,并保存该模板供将来参考。When you created the storage account through the portal, Resource Manager used a template to deploy it and saved that template for future reference.
    2. 参数 - 可用于在部署过程中传入值的参数文件。Parameters - A parameter file that you can use to pass in values during deployment. 它包含你在首次部署时提供的值。It contains the values that you provided during the first deployment. 重新部署模板时,可以更改这其中的任何值。You can change any of these values when you redeploy the template.
    3. CLI - 可用于部署该模板的 Azure 命令行界面 (CLI) 脚本文件。CLI - An Azure command-line-interface (CLI) script file that you can use to deploy the template.
    4. CLI 2.0 - 可用于部署该模板的 Azure 命令行界面 (CLI) 脚本文件。CLI 2.0 - An Azure command-line-interface (CLI) script file that you can use to deploy the template.
    5. PowerShell - 可用于部署该模板的 Azure PowerShell 脚本文件。PowerShell - An Azure PowerShell script file that you can use to deploy the template.
    6. .NET - 可用于部署该模板的 .NET 类。.NET - A .NET class that you can use to deploy the template.
    7. Ruby - 可用于部署模板的 Ruby 类。Ruby - A Ruby class that you can use to deploy the template.

      可通过边栏选项卡上的链接获取这些文件。The files are available through links across the blade. 默认情况下,边栏选项卡会显示模板。By default, the blade displays the template.

      查看模板

事实上,创建 Web 应用和 SQL 数据库时使用的就是此模板。This template is the actual template used to create your web app and SQL database. 请注意,可以使用该模板包含的参数在部署时提供不同的值。Notice it contains parameters that enable you to provide different values during deployment. 若要详细了解模板的结构,请参阅 Authoring Azure Resource Manager templates(创作 Azure Resource Manager 模板)。To learn more about the structure of a template, see Authoring Azure Resource Manager templates.

从资源组导出模板Export the template from resource group

如果已手动更改资源或将资源添加到多个部署中,则从部署历史记录中检索模板无法反映资源组的当前状态。If you have manually changed your resources or added resources in multiple deployments, retrieving a template from the deployment history does not reflect the current state of the resource group. 本部分介绍如何导出反映资源组当前状态的模板。This section shows you how to export a template that reflects the current state of the resource group.

备注

无法导出包含超过 200 项资源的资源组模板。You cannot export a template for a resource group that has more than 200 resources.

  1. 若要查看资源组的模板,请选择“自动化脚本”。To view the template for a resource group, select Automation script.

    导出资源组

    Resource Manager 会评估资源组中的资源,并为这些资源生成一个模板。Resource Manager evaluates the resources in the resource group, and generates a template for those resources. 并非所有资源类型都支持导出模板功能。Not all resource types support the export template function. 可能会出现一个错误,指出导出存在问题。You may see an error stating that there is a problem with the export. 修复导出问题 部分介绍了如何处理这些问题。You learn how to handle those issues in the Fix export issues section.

  2. 将会再次出现六个可用于重新部署解决方案的文件。You again see the six files that you can use to redeploy the solution. 但是,这一次模板稍有不同。However, this time the template is a little different. 请注意,生成的模板包含的参数少于前一部分的模板所包含的。Notice that the generated template contains fewer parameters than the template in previous section. 另外,在此模板中,许多值(例如位置和 SKU 值)是硬编码的,并不接受参数值。Also, many of the values (like location and SKU values) are hard-coded in this template rather than accepting a parameter value. 在重用该模板之前,可能需要对其进行编辑,以便更好地使用参数。Before reusing this template, you might want to edit the template to make better use of parameters.

  3. 可通过几个选项继续使用此模板。You have a couple of options for continuing to work with this template. 可以下载模板,并使用 JSON 编辑器本地使用它。You can either download the template and work on it locally with a JSON editor. 也可将模板保存到库,并通过门户使用它。Or, you can save the template to your library and work on it through the portal.

    如果喜欢使用 VS CodeVisual Studio 等 JSON 编辑器,可本地下载模板并使用该编辑器。If you are comfortable using a JSON editor like VS Code or Visual Studio, you might prefer downloading the template locally and using that editor. 若要在本地使用,请选择“下载”。To work locally, select Download.

    下载模板

    如果不使用 JSON 编辑器进行设置,可通过门户编辑模板。If you are not set up with a JSON editor, you might prefer editing the template through the portal. 本主题其余部分假设已在门户中将模板保存到库。The remainder of this topic assumes you have saved the template to your library in the portal. 但无论使用本地 JSON 编辑器还是通过门户,都要对模板进行相同的语法更改。However, you make the same syntax changes to the template whether working locally with a JSON editor or through the portal. 若要通过门户使用,请选择“添加到库”。To work through the portal, select Add to library.

    添加到库

    将模板添加到库时,请为模板提供名称和说明。When adding a template to the library, give the template a name and description. 然后选择“保存”。Then, select Save.

    设置模板值

  4. 要查看库中保存的模板,请选择“更多服务”,并键入“模板”以筛选结果,并选择“模板”。To view a template saved in your library, select More services, type Templates to filter results, select Templates.

    查找模板

  5. 选择具有已保存名称的模板。Select the template with the name you saved.

    选择模板

自定义模板Customize the template

若要为每个部署创建相同的 Web 应用和 SQL 数据库,则导出的模板可满足要求。The exported template works fine if you want to create the same web app and SQL database for every deployment. 但是,Resource Manager 提供相关选项,因此使用它可以更灵活地部署模板。However, Resource Manager provides options so that you can deploy templates with a lot more flexibility. 本文介绍如何针对数据库管理员名称和密码来添加参数。This article shows you how to add parameters for the database administrator name and password. 可以通过这个相同的方法,提高模板中其他值的灵活性。You can use this same approach to add more flexibility for other values in the template.

  1. 若要自定义模板,请选择“编辑”。To customize the template, select Edit.

    显示模板

  2. 选择模板。Select the template.

    编辑模板

  3. 请将下面的两个参数添加到模板中的 parameters 节,以便传递可能需要在部署过程中指定的值:To be able to pass the values that you might want to specify during deployment, add the following two parameters to the parameters section in the template:

    "administratorLogin": {
        "type": "String"
    },
    "administratorLoginPassword": {
        "type": "SecureString"
    },
    
  4. 若要使用新参数,请替换 resources 节中的 SQL Server 定义。To use the new parameters, replace the SQL server definition in the resources section. 请注意,administratorLogin 和 administratorLoginPassword 现在使用参数值。Notice that administratorLogin and administratorLoginPassword now use parameter values.

    {
        "comments": "Generalized from resource: '/subscriptions/{subscription-id}/resourceGroups/exportsite/providers/Microsoft.Sql/servers/tfserverexport'.",
        "type": "Microsoft.Sql/servers",
        "kind": "v12.0",
        "name": "[parameters('servers_tfserverexport_name')]",
        "apiVersion": "2014-04-01-preview",
        "location": "South Central US",
        "scale": null,
        "properties": {
            "administratorLogin": "[parameters('administratorLogin')]",
            "administratorLoginPassword": "[parameters('administratorLoginPassword')]",
            "version": "12.0"
        },
        "dependsOn": []
    },
    
  5. 编辑完模板后,选择“确定”。Select OK when you are done editing the template.

  6. 选择“保存”以保存对模板所做的更改。Select Save to save the changes to the template.

    保存模板

  7. 若要重新部署已更新的模板,请选择“部署”。To redeploy the updated template, select Deploy.

    部署模板

  8. 提供参数值,并选择要将资源部署到其中的资源组。Provide parameter values, and select a resource group to deploy the resources to.

修复导出问题Fix export issues

并非所有资源类型都支持导出模板功能。Not all resource types support the export template function. 要解决此问题,可以手动将缺失的资源添加回模板中。To resolve this issue, manually add the missing resources back into your template. 错误消息包含无法导出的资源类型。The error message includes the resource types that cannot be exported. 请在模板引用中查找该资源类型。Find that resource type in Template reference. 例如,若要手动添加虚拟网关,请参阅 Microsoft.Network/virtualNetworkGateways 模板引用For example, to manually add a virtual network gateway, see Microsoft.Network/virtualNetworkGateways template reference.

备注

仅当从资源组而不是从部署历史记录中导出时,才会遇到导出问题。You only encounter export issues when exporting from a resource group rather than from your deployment history. 如果上一个部署能够准确地代表资源组的当前状态,则应从部署历史记录而非资源组中导出模板。If your last deployment accurately represents the current state of the resource group, you should export the template from the deployment history rather than from the resource group. 只有在已对资源组进行更改且该更改未在单个模板中定义的情况下,才应从资源组导出。Only export from a resource group when you have made changes to the resource group that are not defined in a single template.

后续步骤Next steps

已学习如何从门户中创建的资源导出模板。You have learned how to export a template from resources that you created in the portal.