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

设置子资源的名称和类型Set name and type for child resources

子资源是只存在于另一资源的上下文内的资源。Child resources are resources that exist only within the context of another resource. 例如,如果没有虚拟机,则虚拟机扩展不能存在。For example, a virtual machine extension can't exist without a virtual machine. 扩展资源是虚拟机的子项。The extension resource is a child of the virtual machine.

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

在 Azure 资源管理器模板(ARM 模板)中,可以在父资源内部或外部指定子资源。In an Azure Resource Manager template (ARM template), you can specify the child resource either within the parent resource or outside of the parent resource. 以下示例显示子资源包括在父资源的资源属性中。The following example shows the child resource included within the resources property of the parent resource.

"resources": [
  {
    <parent-resource>
    "resources": [
      <child-resource>
    ]
  }
]

子资源总共只能定义五级。Child resources can only be defined five levels deep.

下一示例显示子资源位于父资源外部。The next example shows the child resource outside of the parent resource. 如果父资源未部署在同一模板中,或者想要使用 copy 创建多个子资源,可以使用此方法。You might use this approach if the parent resource isn't deployed in the same template, or if want to use copy to create more than one child resource.

"resources": [
  {
    <parent-resource>
  },
  {
    <child-resource>
  }
]

为资源名称和类型提供的值因子资源是在父资源内部还是外部定义而异。The values you provide for the resource name and type vary based on whether the child resource is defined inside or outside of the parent resource.

在父资源内部Within parent resource

在父资源类型内部定义时,请将类型和名称值格式化为一个不带斜杠的单词。When defined within the parent resource type, you format the type and name values as a single word without slashes.

"type": "{child-resource-type}",
"name": "{child-resource-name}",

以下示例显示一个虚拟网络,有一个子网。The following example shows a virtual network and with a subnet. 请注意,子网包含在虚拟网络的资源数组中。Notice that the subnet is included within the resources array for the virtual network. 名称设置为 Subnet1,类型设置为 subnetsThe name is set to Subnet1 and the type is set to subnets. 子资源被标记为依赖父资源,因为在部署子资源之前,必须存在父资源。The child resource is marked as dependent on the parent resource because the parent resource must exist before the child resource can be deployed.

"resources": [
  {
    "type": "Microsoft.Network/virtualNetworks",
    "apiVersion": "2018-10-01",
    "name": "VNet1",
    "location": "[parameters('location')]",
    "properties": {
      "addressSpace": {
        "addressPrefixes": [
          "10.0.0.0/16"
        ]
      }
    },
    "resources": [
      {
        "type": "subnets",
        "apiVersion": "2018-10-01",
        "name": "Subnet1",
        "location": "[parameters('location')]",
        "dependsOn": [
          "VNet1"
        ],
        "properties": {
          "addressPrefix": "10.0.0.0/24"
        }
      }
    ]
  }
]

完整的资源类型仍然为 Microsoft.Network/virtualNetworks/subnetsThe full resource type is still Microsoft.Network/virtualNetworks/subnets. 可不提供 Microsoft.Network/virtualNetworks/,因为假设它继承父资源类型。You don't provide Microsoft.Network/virtualNetworks/ because it's assumed from the parent resource type.

子资源名称设置为 Subnet1,但完整名称包含父名称。The child resource name is set to Subnet1 but the full name includes the parent name. 可不提供 VNet1,因为假设它继承父资源。You don't provide VNet1 because it's assumed from the parent resource.

父资源外部Outside parent resource

在父资源外部定义时,请使用斜杠来格式化类型和名称值,使之包含父类型和名称。When defined outside of the parent resource, you format the type and with slashes to include the parent type and name.

"type": "{resource-provider-namespace}/{parent-resource-type}/{child-resource-type}",
"name": "{parent-resource-name}/{child-resource-name}",

以下示例显示一个虚拟网络和子网,二者都在根级别进行定义。The following example shows a virtual network and subnet that are both defined at the root level. 请注意,子网不包含在虚拟网络的资源数组中。Notice that the subnet isn't included within the resources array for the virtual network. 名称设置为 VNet1/Subnet1,类型设置为 Microsoft.Network/virtualNetworks/subnetsThe name is set to VNet1/Subnet1 and the type is set to Microsoft.Network/virtualNetworks/subnets. 子资源被标记为依赖父资源,因为在部署子资源之前,必须存在父资源。The child resource is marked as dependent on the parent resource because the parent resource must exist before the child resource can be deployed.

"resources": [
  {
    "type": "Microsoft.Network/virtualNetworks",
    "apiVersion": "2018-10-01",
    "name": "VNet1",
    "location": "[parameters('location')]",
    "properties": {
      "addressSpace": {
        "addressPrefixes": [
          "10.0.0.0/16"
        ]
      }
    }
  },
  {
    "type": "Microsoft.Network/virtualNetworks/subnets",
    "apiVersion": "2018-10-01",
    "location": "[parameters('location')]",
    "name": "VNet1/Subnet1",
    "dependsOn": [
      "VNet1"
    ],
    "properties": {
      "addressPrefix": "10.0.0.0/24"
    }
  }
]

后续步骤Next steps