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

Azure 资源管理器模板概述Azure Resource Manager templates overview

在迁移到云的过程中,许多团队都采用了敏捷开发方法。With the move to the cloud, many teams have adopted agile development methods. 这些团队的工作快速迭代。These teams iterate quickly. 他们需要反复将其解决方案部署到云,并需要知道其基础结构处于一种可靠的状态。They need to repeatedly deploy their solutions to the cloud, and know their infrastructure is in a reliable state. 随着基础结构成为迭代过程的一部分,运营与开发之间的划分已经消失。As infrastructure has become part of the iterative process, the division between operations and development has disappeared. 团队需要通过统一的过程来管理基础结构和应用程序代码。Teams need to manage infrastructure and application code through a unified process.

为了解决这些难题,可将部署自动化,并运用基础结构即代码。To meet these challenges, you can automate deployments and use the practice of infrastructure as code. 在代码中定义需要部署的基础结构。In code, you define the infrastructure that needs to be deployed. 基础结构代码将成为项目的一部分。The infrastructure code becomes part of your project. 与应用程序代码一样,可将基础结构代码存储在源存储库中,并控制其版本。Just like application code, you store the infrastructure code in a source repository and version it. 团队中的任何人都可以运行该代码并部署类似的环境。Any one on your team can run the code and deploy similar environments.

若要对 Azure 解决方案实现基础结构即代码,请使用 Azure 资源管理器模板。To implement infrastructure as code for your Azure solutions, use Azure Resource Manager templates. 该模板是一个定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。The template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. 该模板使用声明性语法,使你可以声明要部署的内容,而不需要编写一系列编程命令来进行创建。The template uses declarative syntax, which lets you state what you intend to deploy without having to write the sequence of programming commands to create it. 在该模板中,指定要部署的资源以及这些资源的属性。In the template, you specify the resources to deploy and the properties for those resources.

为何选择资源管理器模板?Why choose Resource Manager templates?

在资源管理器模板与其他某个基础结构即代码服务之间做出选择时,请考虑模板的以下优势:If you're trying to decide between using Resource Manager templates and one of the other infrastructure as code services, consider the following advantages of using templates:

  • 声明性语法:资源管理器模板允许以声明方式创建和部署整个 Azure 基础结构。Declarative syntax: Resource Manager templates allow you to create and deploy an entire Azure infrastructure declaratively. 例如,不仅可以部署虚拟机,还可以部署网络基础结构、存储系统和可能需要的任何其他资源。For example, you can deploy not only virtual machines, but also the network infrastructure, storage systems and any other resources you may need.

  • 重复的结果:在整个开发生命周期内重复部署你的基础结构,并确保以一致的方式部署你的资源。Repeatable results: Repeatedly deploy your infrastructure throughout the development lifecycle and have confidence your resources are deployed in a consistent manner. 模板是幂等的,这意味着,可以多次部署同一模板,并获得处于相同状态的相同资源类型。Templates are idempotent, which means you can deploy the same template many times and get the same resource types in the same state. 可以开发一个模板来表示所需的状态,而无需开发大量的独立模板来表示更新。You can develop one template that represents the desired state, rather than developing lots of separate templates to represent updates.

  • 业务流程:不必担心排序操作的复杂性。Orchestration: You don't have to worry about the complexities of ordering operations. 资源管理器会协调相互依赖的资源的部署,以按正确的顺序创建这些资源。Resource Manager orchestrates the deployment of interdependent resources so they're created in the correct order. 在可能的情况下,资源管理器将会并行部署资源,因此,其完成速度比串行部署更快。When possible, Resource Manager deploys resources in parallel so your deployments finish faster than serial deployments. 通过一个命令部署模板,而无需使用多个强制性命令。You deploy the template through one command, rather than through multiple imperative commands.

    模板部署的比较

  • 内置验证:只有通过验证后才会部署模板。Built-in validation: Your template is deployed only after passing validation. 资源管理器在开始部署之前会检查模板,以确保部署成功。Resource Manager checks the template before starting the deployment to make sure the deployment will succeed. 部署不太可能会在半完成状态时停止。Your deployment is less likely to stop in a half-finished state.

  • 模块化文件:可以将模板分解为更小的可重复使用的组件,并在部署时将它们链接在一起。Modular files: You can break your templates into smaller, reusable components and link them together at deployment time. 还可以在一个模板中嵌套另一个模板。You can also nest one template inside another templates.

  • 创建任何 azure 资源:可以立即在模板中使用新的 azure 服务和功能。Create any Azure resource: You can immediately use new Azure services and features in templates. 一旦资源提供程序引入了新资源,你就可以通过模板立即部署这些资源。As soon as a resource provider introduces new resources, you can deploy those resources through templates. 在使用新服务之前,无需等待工具或模块完成更新。You don't have to wait for tools or modules to be updated before using the new services.

  • 跟踪的部署:在 Azure 门户中,你可以查看部署历史记录并获取有关模板部署的信息。Tracked deployments: In the Azure portal, you can review the deployment history and get information about the template deployment. 可以查看已部署的模板、已传入的参数值,以及任何输出值。You can see the template that was deployed, the parameter values passed in, and any output values. 其他基础结构即代码服务不是通过门户跟踪的。Other infrastructure as code services aren't tracked through the portal.

    部署历史记录

  • 策略即代码Azure 策略是一种作为代码框架的策略,用于自动化管理。Policy as code: Azure Policy is a policy as code framework to automate governance. 如果使用 Azure 策略,在通过模板进行部署时,将会针对不合规的资源执行策略修正。If you're using Azure policies, policy remediation is done on non-compliant resources when deployed through templates.

  • 部署蓝图:你可以利用 Microsoft 提供的蓝图来满足法规和合规性标准。Deployment Blueprints: You can take advantage of Blueprints provided by Microsoft to meet regulatory and compliance standards. 这些蓝图包括用于各种体系结构的预建模板。These blueprints include pre-built templates for various architectures.

  • CI/CD 集成:可以将模板集成到持续集成和持续部署(CI/CD)工具中,这可以自动执行发布管道,实现快速可靠的应用程序和基础结构更新。CI/CD integration: You can integrate templates into your continuous integration and continuous deployment (CI/CD) tools, which can automate your release pipelines for fast and reliable application and infrastructure updates. 通过使用 Azure DevOps 和资源管理器模板任务,你可以使用 Azure Pipelines 持续生成和部署 Azure 资源管理器模板项目。By using Azure DevOps and Resource Manager template task, you can use Azure Pipelines to continuously build and deploy Azure Resource Manager template projects. 若要了解详细信息,请参阅VS project with 管道与 Azure Pipelines 持续集成To learn more, see VS project with pipelines and Continuous integration with Azure Pipelines.

  • 导出代码:可以通过导出资源组的当前状态或查看用于特定部署的模板,获取现有资源组的模板。Exportable code: You can get a template for an existing resource group by either exporting the current state of the resource group, or viewing the template used for a particular deployment. 查看导出的模板是了解模板语法的有用方法。Viewing the exported template is a helpful way to learn about the template syntax.

  • 创作工具:可以Visual Studio Code和模板工具扩展创建模板。Authoring tools: You can author templates with Visual Studio Code and the template tool extension. 你将获得 Intellisense、语法突出显示、内联帮助以及其他许多语言功能。You get intellisense, syntax highlighting, in-line help, and many other language functions. 除了 Visual Studio code 以外,还可以使用Visual studioIn addition to Visual Studio code, you can also use Visual Studio.

模板文件Template file

在模板中,可以编写模板表达式来扩展 JSON 的功能。Within your template, you can write template expressions that extend the capabilities of JSON. 这些表达式使用资源管理器提供的函数These expressions make use of the functions provided by Resource Manager.

模板包含以下节:The template has the following sections:

  • 参数 - 在部署过程中提供值,以便可将同一模板用于不同的环境。Parameters - Provide values during deployment that allow the same template to be used with different environments.

  • 变量 - 定义在模板中重复使用的值。Variables - Define values that are reused in your templates. 可以从参数值构造变量。They can be constructed from parameter values.

  • 用户定义的函数 - 创建自定义函数用于简化模板。User-defined functions - Create customized functions that simplify your template.

  • 资源 - 指定要部署的资源。Resources - Specify the resources to deploy.

  • 输出 - 从已部署的资源返回值。Outputs - Return values from the deployed resources.

模板部署过程Template deployment process

部署模板时,资源管理器会将模板转换为 REST API 操作。When you deploy a template, Resource Manager converts the template into REST API operations. 例如,当资源管理器收到具有以下资源定义的模板:For example, when Resource Manager receives a template with the following resource definition:

"resources": [
  {
    "apiVersion": "2016-01-01",
    "type": "Microsoft.Storage/storageAccounts",
    "name": "mystorageaccount",
    "location": "westus",
    "sku": {
      "name": "Standard_LRS"
    },
    "kind": "Storage",
    "properties": {
    }
  }
]

它将定义转换为以下 REST API 操作,后者将发送到 Microsoft.Storage 资源提供程序:It converts the definition to the following REST API operation, which is sent to the Microsoft.Storage resource provider:

PUT
https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/mystorageaccount?api-version=2016-01-01
REQUEST BODY
{
  "location": "westus",
  "properties": {
  }
  "sku": {
    "name": "Standard_LRS"
  },
  "kind": "Storage"
}

模板设计Template design

模板和资源组的定义方式完全取决于用户及其所需的解决方案管理方式。How you define templates and resource groups is entirely up to you and how you want to manage your solution. 例如,可以通过单个模板将三层应用程序部署到单个资源组。For example, you can deploy your three tier application through a single template to a single resource group.

三层模板

但无需在单个模板中定义整个基础结构。But, you don't have to define your entire infrastructure in a single template. 通常,合理的做法是将部署要求划分成一组有针对性的模板。Often, it makes sense to divide your deployment requirements into a set of targeted, purpose-specific templates. 可以轻松地将这些模板重复用于不同的解决方案。You can easily reuse these templates for different solutions. 若要部署特定的解决方案,请创建链接所有所需模板的主模板。To deploy a particular solution, you create a master template that links all the required templates. 下图显示了如何通过包含三个嵌套模板的父模板部署三层解决方案。The following image shows how to deploy a three tier solution through a parent template that includes three nested templates.

嵌套层模板

要各层具有单独的生命周期,可将三个层部署到单独的资源组。If you envision your tiers having separate lifecycles, you can deploy your three tiers to separate resource groups. 请注意,仍可将这些资源链接到其他资源组中的资源。Notice the resources can still be linked to resources in other resource groups.

层模板

有关嵌套模板的信息,请参阅将链接的模板用于 Azure 资源管理器For information about nested templates, see Using linked templates with Azure Resource Manager.

后续步骤Next steps