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

教程:创建包含依赖的资源的 Azure 资源管理器模板Tutorial: Create Azure Resource Manager templates with dependent resources

了解如何创建 Azure 资源管理器模板以部署多个资源和配置部署顺序。Learn how to create an Azure Resource Manager template to deploy multiple resources and configure the deployment order. 创建模板以后,请通过 Azure 门户使用 Cloud Shell 部署该模板。After you create the template, you deploy the template using the Cloud shell from the Azure portal.

本教程介绍如何创建存储帐户、虚拟机、虚拟网络以及一些其他的依赖资源。In this tutorial, you create a storage account, a virtual machine, a virtual network, and some other dependent resources. 某些资源的部署依赖于另一资源的存在。Some of the resources cannot be deployed until another resource exists. 例如,创建虚拟机的前提是其存储帐户和网络接口存在。For example, you can't create the virtual machine until its storage account and network interface exist. 可通过将一个资源标记为依赖于其他资源来定义此关系。You define this relationship by making one resource as dependent on the other resources. Resource Manager 将评估资源之间的依赖关系,并根据其依赖顺序进行部署。Resource Manager evaluates the dependencies between resources, and deploys them in their dependent order. 如果资源互不依赖,资源管理器将以并行方式部署资源。When resources aren't dependent on each other, Resource Manager deploys them in parallel. 有关详细信息,请参阅定义 Azure 资源管理器模板中部署资源的顺序For more information, see Define the order for deploying resources in Azure Resource Manager Templates.

资源管理器模板依赖资源部署顺序图

本教程涵盖以下任务:This tutorial covers the following tasks:

  • 打开快速入门模板Open a QuickStart template
  • 浏览模板Explore the template
  • 部署模板Deploy the template

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

先决条件Prerequisites

若要完成本文,需要做好以下准备:To complete this article, you need:

打开快速入门模板Open a Quickstart template

Azure 快速入门模板是资源管理器模板的存储库。Azure QuickStart Templates is a repository for Resource Manager templates. 无需从头开始创建模板,只需找到一个示例模板并对其自定义即可。Instead of creating a template from scratch, you can find a sample template and customize it. 本教程中使用的模板称为部署简单的 Windows VMThe template used in this tutorial is called Deploy a simple Windows VM.

  1. 在 Visual Studio Code 中,选择“文件”>“打开文件”。 From Visual Studio Code, select File>Open File.

  2. 在“文件名”中粘贴以下 URL: In File name, paste the following URL:

    https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-vm-simple-windows/azuredeploy.json
    
  3. 选择“打开”以打开该文件。 Select Open to open the file.

  4. 选择“文件”>“另存为”,将该文件的副本保存到名为 azuredeploy.json 的本地计算机。 Select File>Save As to save a copy of the file to your local computer with the name azuredeploy.json.

浏览模板Explore the template

浏览此部分的模板时,请尝试回答以下问题:When you explore the template in this section, try to answer these questions:

  • 在此模板中定义了多少 Azure 资源?How many Azure resources defined in this template?
  • 其中一个资源是 Azure 存储帐户。One of the resources is an Azure storage account. 该定义是否与上一教程中使用的定义类似?Does the definition look like the one used in the last tutorial?
  • 对于此模板中定义的资源,能否找到模板参考?Can you find the template references for the resources defined in this template?
  • 能否找到资源的依赖项?Can you find the dependencies of the resources?
  1. 在 Visual Studio Code 中折叠元素,直到只能在 resources 中看到第一级元素和第二级元素:From Visual Studio Code, collapse the elements until you only see the first-level elements and the second-level elements inside resources:

    Visual Studio Code Azure 资源管理器模板

    有五个通过此模板定义的资源:There are five resources defined by the template:

    • Microsoft.Storage/storageAccountsMicrosoft.Storage/storageAccounts. 请参阅模板参考See the template reference.

    • Microsoft.Network/publicIPAddressesMicrosoft.Network/publicIPAddresses. 请参阅模板参考See the template reference.

    • Microsoft.Network/virtualNetworksMicrosoft.Network/virtualNetworks. 请参阅模板参考See the template reference.

    • Microsoft.Network/networkInterfacesMicrosoft.Network/networkInterfaces. 请参阅模板参考See the template reference.

    • Microsoft.Compute/virtualMachinesMicrosoft.Compute/virtualMachines. 请参阅模板参考See the template reference.

      在自定义模板之前,不妨对其进行一些基本的了解。It is helpful to get some basic understanding of the template before customizing it.

  2. 展开第一个资源。Expand the first resource. 它是一个存储帐户。It is a storage account. 将资源定义和模板参考进行比较。Compare the resource definition to the template reference.

    Visual Studio Code Azure 资源管理器模板存储帐户定义

  3. 展开第二个资源。Expand the second resource. 资源类型为 Microsoft.Network/publicIPAddressesThe resource type is Microsoft.Network/publicIPAddresses. 将资源定义和模板参考进行比较。Compare the resource definition to the template reference.

    Visual Studio Code Azure 资源管理器模板公共 IP 地址定义

  4. 展开第四个资源。Expand the fourth resource. 资源类型为 Microsoft.Network/networkInterfacesThe resource type is Microsoft.Network/networkInterfaces:

    Visual Studio Code Azure 资源管理器模板 dependsOn

    使用 dependsOn 元素可将一个资源定义为与一个或多个资源相依赖。The dependsOn element enables you to define one resource as a dependent on one or more resources. 此资源依赖于两个其他的资源:The resource depends on two other resources:

    • Microsoft.Network/publicIPAddresses
    • Microsoft.Network/virtualNetworks
  5. 展开第五个资源。Expand the fifth resource. 此资源为虚拟机。This resource is a virtual machine. 它依赖于两个其他的资源:It depends on two other resources:

    • Microsoft.Storage/storageAccounts
    • Microsoft.Network/networkInterfaces

下图演示了此模板的资源和依赖项信息:The following diagram illustrates the resources and the dependency information for this template:

Visual Studio Code Azure 资源管理器模板依赖项图

指定依赖项可以让资源管理器有效地部署此解决方案。By specifying the dependencies, Resource Manager efficiently deploys the solution. 它以并行方式部署存储帐户、公共 IP 地址和虚拟网络,因为这些没有依赖项。It deploys the storage account, public IP address, and virtual network in parallel because they have no dependencies. 部署公共 IP 地址和虚拟网络资源以后,会创建网络接口。After the public IP address and virtual network are deployed, the network interface is created. 所有其他的资源都部署以后,资源管理器会部署虚拟机。When all other resources are deployed, Resource Manager deploys the virtual machine.

部署模板Deploy the template

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

可通过多种方法来部署模板。There are many methods for deploying templates. 本教程从 Azure 门户使用 Cloud Shell。In this tutorial, you use Cloud Shell from the Azure portal.

  1. 登录到 Cloud ShellSign in to the Cloud Shell.

  2. 选择 Cloud Shell 左上角的“PowerShell” ,然后选择“确认”。 Select PowerShell from the upper left corner of the Cloud shell, and then select Confirm. 在本教程中,请使用 PowerShell。You use PowerShell in this tutorial.

  3. 在 Cloud Shell 中选择“上传文件”: Select Upload file from the Cloud shell:

    Azure 门户 - Cloud Shell - 上传文件

  4. 选择前面在本教程中保存的模板。Select the template you saved earlier in the tutorial. 默认名称为 azuredeploy.jsonThe default name is azuredeploy.json. 如果某个文件的文件名相同,则会覆盖旧文件,没有任何通知。If you have a file with the same file name, the old file is overwritten without any notification.

    还可以选择使用 ls $HOME 命令和 cat $HOME/azuredeploy.json 命令来验证文件是否已成功上传。You can optionally use the ls $HOME command and the cat $HOME/azuredeploy.json command to verify the files areis uploaded successfully.

  5. 在 Cloud Shell 中运行以下 PowerShell 命令。From the Cloud shell, run the following PowerShell commands. 若要提高安全性,请使用为虚拟机管理员帐户生成的密码。To increase security, use a generated password for the virtual machine administrator account. 请参阅先决条件See Prerequisites.

    $resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"
    $location = Read-Host -Prompt "Enter the location (i.e. centralus)"
    $adminUsername = Read-Host -Prompt "Enter the virtual machine admin username"
    $adminPassword = Read-Host -Prompt "Enter the admin password" -AsSecureString
    $dnsLabelPrefix = Read-Host -Prompt "Enter the DNS label prefix"
    
    New-AzResourceGroup -Name $resourceGroupName -Location "$location"
    New-AzResourceGroupDeployment `
        -ResourceGroupName $resourceGroupName `
        -adminUsername $adminUsername `
        -adminPassword $adminPassword `
        -dnsLabelPrefix $dnsLabelPrefix `
        -TemplateFile "$HOME/azuredeploy.json"
    
  6. 运行以下 PowerShell 命令,列出新建的虚拟机:Run the following PowerShell command to list the newly created virtual machine:

    $resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"
    Get-AzVM -Name SimpleWinVM -ResourceGroupName $resourceGroupName
    

    虚拟机名称在模板中硬编码为 SimpleWinVMThe virtual machine name is hard-coded as SimpleWinVM inside the template.

  7. 通过 RDP 连接到虚拟机,验证虚拟机是否已成功创建。RDP to the virtual machine to verify the virtual machine has been created successfully.

清理资源Clean up resources

不再需要 Azure 资源时,请通过删除资源组来清理部署的资源。When the Azure resources are no longer needed, 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. 应会看到,该资源组中总共有六个资源。You shall see a total of six resources in the resource group.
  4. 在顶部菜单中选择“删除资源组”。 Select Delete resource group from the top menu.

后续步骤Next steps

本教程介绍如何通过开发和部署模板来创建虚拟机、虚拟网络和依赖资源。In this tutorial, you developed and deployed a template to create a virtual machine, a virtual network, and the dependent resources. 若要了解如何根据条件部署 Azure 资源,请参阅:To learn how to deploy Azure resources based on conditions, see: