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

从现有资源导出 Azure 资源管理器模板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 isn't based on any template that you used for deployment. 它创建的模板是资源组的“快照”或“备份”。Instead, it creates a template that is a "snapshot" or "backup" of the resource group. 导出的模板会有许多硬编码的值,其参数可能没有定义的那么多。The exported template has many hard-coded values and probably not as many parameters as you would typically define. 使用此选项可将资源重新部署到同一资源组。Use this option to redeploy resources to the same resource group. 若要将此模板用于其他资源组,可能需要对其进行大幅修改。To use this template for another resource group, you may have to significantly modify it.

本文通过门户展示了这两种方法。This article 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 rest of this article assumes you've 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 Create a resource.

    选择“新建”

  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 for your new resource group. 可以看到,门户中显示了上次部署的结果。Notice that the portal shows the result of the last deployment. 选择此链接。Select this link.

    资源组

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

    上次部署

  3. 门户会显示部署摘要。The portal 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 CLI script file that you can use to deploy the template.
    4. PowerShell - 可用于部署该模板的 Azure PowerShell 脚本文件。PowerShell - An Azure PowerShell script file that you can use to deploy the template.
    5. .NET - 可用于部署该模板的 .NET 类。.NET - A .NET class that you can use to deploy the template.
    6. Ruby - 可用于部署模板的 Ruby 类。Ruby - A Ruby class that you can use to deploy the template.

      默认情况下,门户会显示模板。By default, the portal 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. 若要详细了解模板的结构,请参阅创作 Azure 资源管理器模板To learn more about the structure of a template, see Authoring Azure Resource Manager templates.

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

如果已手动更改了资源或将资源添加到多个部署中,则从部署历史记录中检索模板不能反映资源组的当前状态。If you've manually changed your resources or added resources in multiple deployments, retrieving a template from the deployment history doesn't 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. 它用作资源组的快照,可用来重新部署到同一资源组。It is intended as a snapshot of the resource group, which you can use to redeploy to the same resource group. 若要将导出的模板用于其他解决方案,必须大幅修改它。To use the exported template for other solutions, you must significantly modify it.

备注

无法导出包含超过 200 项资源的资源组模板。You can't 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're 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 aren't set up with a JSON editor, you might prefer editing the template through the portal. 本文剩余部分假设你已在门户中将模板保存到库。The rest of this article assumes you've 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, you 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 pass 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're 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. 仅当从资源组导出(而不是从部署历史记录导出)时,才会看到导出问题。You only see 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 aren't defined in a single template.

若要解决导出问题,请手动将缺少的资源添加回模板中。To resolve export issues, manually add the missing resources back into your template. 错误消息内指出了无法导出的资源类型。The error message includes the resource types that can't 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. 模板参考文档中提供了用于将资源添加到模板的 JSON。The template reference gives you the JSON to add the resource to your template.

获取资源的 JSON 格式后,需要获取资源值。After getting the JSON format for the resource, you need to get the resource values. 可以在 REST API 中对资源类型使用 GET 操作,从而查看资源值。You can see the values for the resource by using the GET operation in the REST API for the resource type. 例如,若要获取虚拟网络网关值,请参阅虚拟网络网关 - GetFor example, to get the values for your virtual network gateway, see Virtual Network Gateways - Get.

后续步骤Next steps