可以使用“dependsOn”属性来指定模板中的资源之间的依赖关系。这样,当子网资源被引用时,模板将首先创建该资源,然后创建其他资源。以下是一个示例:
{
"apiVersion": "2018-04-01",
"type": "Microsoft.Network/virtualNetworks",
"name": "myVNet",
"location": "[resourceGroup().location]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"10.0.0.0/16"
]
},
"subnets": [
{
"name": "mySubnet",
"properties": {
"addressPrefix": "10.0.0.0/24"
}
}
]
}
},
{
"apiVersion": "2018-04-01",
"type": "Microsoft.Network/virtualNetworks/subnets",
"name": "[concat('myVNet/', 'mySubnet2')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.Network/virtualNetworks', 'myVNet')]"
],
"properties": {
"addressPrefix": "10.0.1.0/24"
}
}
在示例中,第二个资源依赖于第一个资源。这将确保在创建第二个资源之前,第一个资源已被创建。如果尝试删除第一个资源,则将无法删除第二个资源,因为它仍然依赖于第一个资源。