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

定义 Azure Resource Manager 模板中部署资源的顺序Define the order for deploying resources in Azure Resource Manager templates

对于给定的资源,可能有部署资源之前必须存在的其他资源。For a given resource, there can be other resources that must exist before the resource is deployed. 例如,SQL Server 必须存在,才能尝试部署 SQL 数据库。For example, a SQL server must exist before attempting to deploy a SQL database. 可通过将一个资源标记为依赖于其他资源来定义此关系。You define this relationship by marking one resource as dependent on the other resource. 使用 dependsOn 元素或 reference 函数定义依赖项。You define a dependency with the dependsOn element, or by using the reference function.

Resource Manager 将评估资源之间的依赖关系,并根据其依赖顺序进行部署。Resource Manager evaluates the dependencies between resources, and deploys them in their dependent order. 如果资源互不依赖,Resource Manager 将并行部署资源。When resources are not dependent on each other, Resource Manager deploys them in parallel. 只需为在同一模板中部署的资源定义依赖关系。You only need to define dependencies for resources that are deployed in the same template.

dependsOndependsOn

在模板中,使用 dependsOn 元素可将一个资源定义为与一个或多个资源相依赖。Within your template, the dependsOn element enables you to define one resource as a dependent on one or more resources. 它的值可以是一个资源名称间采用逗号进行分隔的列表。Its value can be a comma-separated list of resource names.

以下示例显示了一个虚拟机规模集,该集依赖于负载均衡器、虚拟网络以及创建多个存储帐户的循环。The following example shows a virtual machine scale set that depends on a load balancer, virtual network, and a loop that creates multiple storage accounts. 下面的示例中未显示其他这些资源,但它们需要存在于模板的其他位置。These other resources are not shown in the following example, but they would need to exist elsewhere in the template.

{
  "type": "Microsoft.Compute/virtualMachineScaleSets",
  "name": "[variables('namingInfix')]",
  "location": "[variables('location')]",
  "apiVersion": "2016-03-30",
  "tags": {
    "displayName": "VMScaleSet"
  },
  "dependsOn": [
    "[variables('loadBalancerName')]",
    "[variables('virtualNetworkName')]",
    "storageLoop",
  ],
  ...
}

在前面的示例中,通过名为 storageLoop 的复制循环创建的资源上包含一个依赖关系。In the preceding example, a dependency is included on the resources that are created through a copy loop named storageLoop. 有关示例,请参阅在 Azure Resource Manager 中创建多个资源实例For an example, see Create multiple instances of resources in Azure Resource Manager.

定义依赖关系时,可以包括资源提供程序命名空间和资源类型,以避免模糊。When defining dependencies, you can include the resource provider namespace and resource type to avoid ambiguity. 例如,为明确表示可能与其他资源同名的负载均衡器和虚拟网络,可使用以下格式:For example, to clarify a load balancer and virtual network that may have the same names as other resources, use the following format:

"dependsOn": [
  "[concat('Microsoft.Network/loadBalancers/', variables('loadBalancerName'))]",
  "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
]

尽管你可能倾向使用 dependsOn 来映射资源之间的关系,但请务必了解这么做的理由。While you may be inclined to use dependsOn to map relationships between your resources, it's important to understand why you're doing it. 例如,若要记录资源的互连方式,那么,dependsOn 方法并不合适。For example, to document how resources are interconnected, dependsOn is not the right approach. 部署之后,无法查询 dependsOn 元素中定义的资源。You cannot query which resources were defined in the dependsOn element after deployment. 通过使用 dependsOn,可以影响部署时间,因为 Resource Manager 不会并行部署两个具有依赖关系的资源。By using dependsOn, you potentially impact deployment time because Resource Manager does not deploy in parallel two resources that have a dependency. 若要记录资源之间的关系,请改为使用资源链接To document relationships between resources, instead use resource linking.

子资源Child resources

资源属性允许指定与所定义的资源相关的子资源。The resources property allows you to specify child resources that are related to the resource being defined. 子资源总共只能定义五级。Child resources can only be defined five levels deep. 请务必注意子资源和父资源之间不能创建隐式依赖关系。It is important to note that an implicit dependency is not created between a child resource and the parent resource. 如果要在父级资源后部署子资源,则必须使用 dependsOn 属性明确声明该依赖关系。If you need the child resource to be deployed after the parent resource, you must explicitly state that dependency with the dependsOn property.

每个父资源仅接受特定的资源类型作为子资源。Each parent resource accepts only certain resource types as child resources. 可接受的资源类型在父资源的模板架构中指定。The accepted resource types are specified in the template schema of the parent resource. 子资源类型的名称包含父资源类型的名称,例如 Microsoft.Web/sites/configMicrosoft.Web/sites/extensions 都是 Microsoft.Web/sites 的子资源。The name of child resource type includes the name of the parent resource type, such as Microsoft.Web/sites/config and Microsoft.Web/sites/extensions are both child resources of the Microsoft.Web/sites.

以下示例演示了 SQL 服务器和 SQL 数据库。The following example shows a SQL server and SQL database. 请注意,在 SQL 数据库与 SQL 服务器之间定义了显式依赖关系,尽管数据库是服务器的子级。Notice that an explicit dependency is defined between the SQL database and SQL server, even though the database is a child of the server.

"resources": [
  {
    "name": "[variables('sqlserverName')]",
    "type": "Microsoft.Sql/servers",
    "location": "[resourceGroup().location]",
    "tags": {
      "displayName": "SqlServer"
    },
    "apiVersion": "2014-04-01-preview",
    "properties": {
      "administratorLogin": "[parameters('administratorLogin')]",
      "administratorLoginPassword": "[parameters('administratorLoginPassword')]"
    },
    "resources": [
      {
        "name": "[parameters('databaseName')]",
        "type": "databases",
        "location": "[resourceGroup().location]",
        "tags": {
          "displayName": "Database"
        },
        "apiVersion": "2014-04-01-preview",
        "dependsOn": [
          "[variables('sqlserverName')]"
        ],
        "properties": {
          "edition": "[parameters('edition')]",
          "collation": "[parameters('collation')]",
          "maxSizeBytes": "[parameters('maxSizeBytes')]",
          "requestedServiceObjectiveName": "[parameters('requestedServiceObjectiveName')]"
        }
      }
    ]
  }
]

引用函数reference function

引用函数使表达式能够从其他 JSON 名值对或运行时资源中派生其值。The reference function enables an expression to derive its value from other JSON name and value pairs or runtime resources. 引用表达式隐式声明一个资源依赖于另一个资源。Reference expressions implicitly declare that one resource depends on another. 常规格式为:The general format is:

reference('resourceName').propertyPath

在以下示例中,CDN 终结点显式依赖于 CDN 配置文件,隐式依赖于 Web 应用。In the following example, a CDN endpoint explicitly depends on the CDN profile, and implicitly depends on a web app.

{
    "name": "[variables('endpointName')]",
    "type": "endpoints",
    "location": "[resourceGroup().location]",
    "apiVersion": "2016-04-02",
    "dependsOn": [
            "[variables('profileName')]"
    ],
    "properties": {
        "originHostHeader": "[reference(variables('webAppName')).hostNames[0]]",
        ...
    }

可以使用此元素或 dependsOn 元素来指定依赖关系,但不需要同时使用它们用于同一依赖资源。You can use either this element or the dependsOn element to specify dependencies, but you do not need to use both for the same dependent resource. 只要可能,可使用隐式引用以避免添加不必要的依赖项。Whenever possible, use an implicit reference to avoid adding an unnecessary dependency.

若要了解详细信息,请参阅引用函数To learn more, see reference function.

关于设置依赖项的建议Recommendations for setting dependencies

在决定要设置的依赖项时,请遵循以下准则:When deciding what dependencies to set, use the following guidelines:

  • 尽可能少设置依赖项。Set as few dependencies as possible.
  • 将子资源设置为依赖于其父资源。Set a child resource as dependent on its parent resource.
  • 使用 reference 函数在需要共享属性的资源之间设置隐式依赖项。Use the reference function to set implicit dependencies between resources that need to share a property. 在已经定义隐式依赖项的情况下,请勿添加显式依赖项 (dependsOn)。Do not add an explicit dependency (dependsOn) when you have already defined an implicit dependency. 此方法降低了设置不必要依赖项的风险。This approach reduces the risk of having unnecessary dependencies.
  • 如果没有其他资源提供的功能某项资源就无法创建时,可设置依赖项。Set a dependency when a resource cannot be created without functionality from another resource. 如果资源仅在部署后进行交互,请勿设置依赖项。Do not set a dependency if the resources only interact after deployment.
  • 让依赖项级联,无需对其进行显式设置。Let dependencies cascade without setting them explicitly. 例如,虚拟机依赖于虚拟网络接口,虚拟网络接口依赖于虚拟网络和公共 IP 地址。For example, your virtual machine depends on a virtual network interface, and the virtual network interface depends on a virtual network and public IP addresses. 因此,虚拟机在所有这三个资源之后部署,但请勿将虚拟机显式设置为依赖于所有这三个资源。Therefore, the virtual machine is deployed after all three resources, but do not explicitly set the virtual machine as dependent on all three resources. 此方法阐明了依赖顺序,在以后更改模板会更容易。This approach clarifies the dependency order and makes it easier to change the template later.
  • 如果某个值可以在部署之前确定,请尝试在没有依赖项的情况下部署资源。If a value can be determined before deployment, try deploying the resource without a dependency. 例如,如果某个配置值需要另一资源的名称,则可能不需要依赖项。For example, if a configuration value needs the name of another resource, you might not need a dependency. 本指南并非始终适用,因为某些资源可验证其他资源是否存在。This guidance does not always work because some resources verify the existence of the other resource. 如果收到错误,请添加一个依赖项。If you receive an error, add a dependency.

Resource Manager 可在模板验证过程中确定循环依赖项。Resource Manager identifies circular dependencies during template validation. 如果收到的错误指出存在循环依赖项,可重新评估模板,看是否存在不需要且可删除的依赖项。If you receive an error stating that a circular dependency exists, evaluate your template to see if any dependencies are not needed and can be removed. 如果删除依赖项不起作用,则可将一些部署操作移至子资源中来避免循环依赖项(这些子资源是在具有循环依赖项的资源之后部署的)。If removing dependencies does not work, you can avoid circular dependencies by moving some deployment operations into child resources that are deployed after the resources that have the circular dependency. 例如,假设要部署两个虚拟机,但必须在每个虚拟机上设置引用另一虚拟机的属性。For example, suppose you are deploying two virtual machines but you must set properties on each one that refer to the other. 可以按下述顺序部署这两个虚拟机:You can deploy them in the following order:

  1. vm1vm1
  2. vm2vm2
  3. vm1 上的扩展依赖于 vm1 和 vm2。Extension on vm1 depends on vm1 and vm2. 扩展在 vm1 上设置的值是从 vm2 获取的。The extension sets values on vm1 that it gets from vm2.
  4. vm2 上的扩展依赖于 vm1 和 vm2。Extension on vm2 depends on vm1 and vm2. 扩展在 vm2 上设置的值是从 vm1 获取的。The extension sets values on vm2 that it gets from vm1.

有关评估部署顺序和解决依赖项错误的信息,请参阅排查使用 Azure Resource Manager 时的常见 Azure 部署错误For information about assessing the deployment order and resolving dependency errors, see Troubleshoot common Azure deployment errors with Azure Resource Manager.

后续步骤Next steps