首页 > 解决方案 > 如何修复 vNet 对等模板

问题描述

我正在使用嵌套模板来创建中心辐射型 Azure 网络部署。我有 3 个嵌套模板,目前它一次创建 1 个。当我创建 vNet 对等互连时,尚未创建其他虚拟网络,因此第一个嵌套模板未完成,因为它试图引用不存在的 vNet。我在 vNet 对等互连之前尝试过依赖,尽管它未能说明模板中从未提及资源。

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/Microsoft.Resources.json",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vnetName": {
            "type": "string",
            "metadata": {
                "description": "VNet name"
            }
        },
        "vnetAddressPrefix": {
            "type": "string",
            "metadata": {
                "description": "Address prefix"
            }
        },
        "internalvNetName": {
            "type": "string",
            "metadata": {
                "description": "Internal vNet Name"
            }
        },
        "internalvNetAddressPrefix": {
            "type": "string",
            "metadata": {
                "description": "Internal vNet Address Prefix"
            }
        },
        "internal-subnet1Prefix": {
            "type": "string",
            "metadata": {
                "description": "Subnet 1 prefix"
            }
        },
        "internal-subnet1": {
            "type": "string",
            "metadata": {
                "description": "internal Subnet 1 Name"
            }
        },
        "internal-NSG": {
            "type": "string",
            "metadata": {
                "description": "internal NSG Name"
            }
        },
        "DMZResourceGroup": {
            "type": "string",
            "metadata": {
                "description": "resource group for DMZ environment"
            }
        },
        "HUBResourceGroup": {
            "type": "string",
            "metadata": {
                "description": "resource group for HUB environment"
            }
        },
        "internalServersRG": {
            "type": "string",
            "metadata": {
                "description": "resource group for Internal servers"
            }
        },
        "DMZvnet": {
            "type": "string",
            "metadata": {
                "description": "VNet for DMZ environment"
            }
        },
        "DMZvnetAddressPrefix": {
            "type": "string",
            "metadata": {
                "description": "DMZ VNet Address prefix"
            }
        },
        "DMZFESubnet1Prefix": {
            "type": "string",
            "metadata": {
                "description": "Subnet1 for DMZ environment. This is the Front-End subnet"
            }
        },
        "DMZBESubnet2Prefix": {
            "type": "string",
            "metadata": {
                "description": "Subnet1 for DMZ environment. This is the Back-End subnet"
            }
        },
        "DMZNSG": {
            "type": "string",
            "metadata": {
                "description": "NSG for DMZ environment"
            }
        },
        "DMZFEsubnetName": {
            "type": "string",
            "metadata": {
                "description": "Front-End subnet for DMZ environment"
            }
        },
        "DMZBEsubnetName": {
            "type": "string",
            "metadata": {
                "description": "Back-End subnet for DMZ environment"
            }
        },
        "subnet1Prefix": {
            "type": "string",
            "metadata": {
                "description": "Subnet 1 prefix"
            }
        },
        "subnet1Name": {
            "type": "string",
            "metadata": {
                "description": "Subnet 1 Name"
            }
        },
        "subnet2Prefix": {
            "type": "string",
            "metadata": {
                "description": "Subnet 2 Prefix"
            }
        },
        "subnet2Name": {
            "type": "string",
            "metadata": {
                "description": "Subnet 2 Name"
            }
        },
        "subnet3Prefix": {
            "type": "string",
            "metadata": {
                "description": "Subnet 3 Prefix"
            }
        },
        "subnet3Name": {
            "type": "string",
            "metadata": {
                "description": "Subnet 3 Name"
            }
        },
        "networkSecurityGroupName": {
            "type": "string"
        }
    },
    "variables": {
        "location": "West Europe",
        "HUBvNettoDMZvNet": "[concat(parameters('VNetName'), '-', parameters('DMZvnet'))]",
        "DMZvNettoHUBvNet": "[concat(parameters('DMZvnet'), '-', parameters('VNetName'))]"
    },
    "resources": [
        {
            "name": "[parameters('DMZResourceGroup')]",
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2018-05-01",
            "location": "[variables('Location')]",
            "tags": {},
            "properties": {}
        },
        {
            "name": "[parameters('HUBResourceGroup')]",
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2018-05-01",
            "location": "[variables('Location')]",
            "tags": {},
            "properties": {}
        },
        {
            "name": "[parameters('internalserversrg')]",
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2018-05-01",
            "location": "[variables('Location')]",
            "tags": {},
            "properties": {}
        },
        {
            "apiVersion": "2017-05-10",
            "name": "hubtemplate",
            "type": "Microsoft.Resources/deployments",
            "resourceGroup": "[parameters('Hubresourcegroup')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', parameters('HUBResourceGroup'))]"
            ],
            "properties": {
                "mode": "incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                    "contentversion": "1.0.0.0",
                    "parameters": {},
                    "variables": {},
                    "resources": [
                        {
                            "apiVersion": "2017-06-01",
                            "type": "Microsoft.Network/virtualNetworks",
                            "name": "[parameters('vnetName')]",
                            "location": "[variables('Location')]",
                            "properties": {
                                "addressSpace": {
                                    "addressPrefixes": [
                                        "[parameters('vnetAddressPrefix')]"
                                    ]
                                },
                                "subnets": [
                                    {
                                        "name": "[parameters('subnet1Name')]",
                                        "properties": {
                                            "addressPrefix": "[parameters('subnet1Prefix')]"
                                        }
                                    },
                                    {
                                        "name": "[parameters('subnet2Name')]",
                                        "properties": {
                                            "addressPrefix": "[parameters('subnet2Prefix')]"
                                        }
                                    },
                                    {
                                        "name": "[parameters('subnet3Name')]",
                                        "properties": {
                                            "addressPrefix": "[parameters('subnet3Prefix')]"
                                        }
                                    }
                                ]
                            }
                        },
                        {
                            "name": "[concat(parameters('vnetname'), '/peered-to-', parameters('dmzvnet'))]",
                            "type": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings",
                            "apiVersion": "2018-08-01",
                            "location": "[variables('location')]",
                            "dependsOn": [
                                "[concat('Microsoft.Network/virtualNetworks/', parameters('VNetName'))]"
                            ],
                            "properties": {
                                "allowVirtualNetworkAccess": true,
                                "allowForwardedTraffic": true,
                                "allowGatewayTransit": true,
                                "useRemoteGateways": false,
                                "remoteVirtualNetwork": {
                                    "id": "[concat(subscription().id, '/resourceGroups/', parameters('DMZresourcegroup'), '/providers/Microsoft.Network/virtualNetworks/', parameters('DMZvNet'))]"
                                }
                            }
                        },
                        {
                            "name": "[concat(parameters('vnetname'), '/peered-to-', parameters('internalvNetName'))]",
                            "type": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings",
                            "apiVersion": "2018-08-01",
                            "location": "[variables('location')]",
                            "dependsOn": [
                                "[concat('Microsoft.Network/virtualNetworks/', parameters('VNetName'))]"
                            ],
                            "properties": {
                                "allowVirtualNetworkAccess": true,
                                "allowForwardedTraffic": true,
                                "allowGatewayTransit": true,
                                "useRemoteGateways": false,
                                "remoteVirtualNetwork": {
                                    "id": "[concat(subscription().id, '/resourceGroups/', parameters('internalServersRG'), '/providers/Microsoft.Network/virtualNetworks/', parameters('internalvNetName'))]"
                                }
                            }
                        },
                        {
                            "apiVersion": "2015-06-15",
                            "type": "Microsoft.Network/networkSecurityGroups",
                            "name": "[parameters('networkSecurityGroupName')]",
                            "location": "[variables('location')]",
                            "properties": {
                                "securityRules": [
                                    {
                                        "name": "allow-rdp",
                                        "properties": {
                                            "description": "allow rdp",
                                            "protocol": "*",
                                            "sourcePortRange": "*",
                                            "destinationPortRange": "3389",
                                            "sourceAddressPrefix": "*",
                                            "destinationAddressPrefix": "10.1.1.0",
                                            "access": "Allow",
                                            "priority": 110,
                                            "direction": "Inbound"
                                        }
                                    },
                                    {
                                        "name": "allow-smb",
                                        "properties": {
                                            "description": "allow smb",
                                            "protocol": "*",
                                            "sourcePortRange": "*",
                                            "destinationPortRange": "445",
                                            "sourceAddressPrefix": "*",
                                            "destinationAddressPrefix": "10.1.1.0",
                                            "access": "Allow",
                                            "priority": 112,
                                            "direction": "Inbound"
                                        }
                                    }
                                ]
                            }
                        }
                    ],
                    "outputs": {}
                }
            }
        },
        {
            "apiVersion": "2017-05-10",
            "name": "dmztemplate",
            "type": "Microsoft.Resources/deployments",
            "resourceGroup": "[parameters('DMZresourcegroup')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', parameters('DMZResourceGroup'))]",
                "HUBTemplate"
            ],
            "properties": {
                "mode": "incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                    "contentversion": "1.0.0.0",
                    "parameters": {},
                    "variables": {},
                    "resources": [
                        {
                            "apiVersion": "2017-06-01",
                            "type": "Microsoft.Network/virtualNetworks",
                            "name": "[parameters('DMZvNet')]",
                            "location": "[variables('Location')]",
                            "properties": {
                                "addressSpace": {
                                    "addressPrefixes": [
                                        "[parameters('DMZvNetaddressPrefix')]"
                                    ]
                                },
                                "subnets": [
                                    {
                                        "name": "[parameters('DMZFEsubnetName')]",
                                        "properties": {
                                            "addressPrefix": "[parameters('DMZFESubnet1Prefix')]"
                                        }
                                    },
                                    {
                                        "name": "[parameters('DMZBEsubnetName')]",
                                        "properties": {
                                            "addressPrefix": "[parameters('DMZBESubnet2Prefix')]"
                                        }
                                    }
                                ]
                            }
                        },
                        {
                            "name": "[concat(parameters('dmzvnet'), '/peered-to-', parameters('vnetname'))]",
                            "type": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings",
                            "apiVersion": "2018-08-01",
                            "location": "[variables('location')]",
                            "dependsOn": [
                                "[concat('Microsoft.Network/virtualNetworks/', parameters('dmzvnet'))]"
                            ],
                            "properties": {
                                "allowVirtualNetworkAccess": true,
                                "allowForwardedTraffic": true,
                                "allowGatewayTransit": true,
                                "useRemoteGateways": false,
                                "remoteVirtualNetwork": {
                                    "id": "[concat(subscription().id, '/resourceGroups/', parameters('HUBResourceGroup'), '/providers/Microsoft.Network/virtualNetworks/', parameters('vnetname'))]"
                                }
                            }
                        },
                        {
                            "name": "[concat(parameters('dmzvnet'), '/peered-to-', parameters('internalvNetName'))]",
                            "type": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings",
                            "apiVersion": "2018-08-01",
                            "location": "[variables('location')]",
                            "dependsOn": [
                                "[concat('Microsoft.Network/virtualNetworks/', parameters('dmzvnet'))]"
                            ],
                            "properties": {
                                "allowVirtualNetworkAccess": true,
                                "allowForwardedTraffic": true,
                                "allowGatewayTransit": true,
                                "useRemoteGateways": false,
                                "remoteVirtualNetwork": {
                                    "id": "[concat(subscription().id, '/resourceGroups/', parameters('internalServersRG'), '/providers/Microsoft.Network/virtualNetworks/', parameters('internalvNetName'))]"
                                }
                            }
                        },
                        {
                            "apiVersion": "2015-06-15",
                            "type": "Microsoft.Network/networkSecurityGroups",
                            "name": "[parameters('DMZNSG')]",
                            "location": "[variables('location')]",
                            "properties": {
                                "securityRules": [
                                    {
                                        "name": "allow-rdp",
                                        "properties": {
                                            "description": "allow rdp",
                                            "protocol": "*",
                                            "sourcePortRange": "*",
                                            "destinationPortRange": "3389",
                                            "sourceAddressPrefix": "*",
                                            "destinationAddressPrefix": "10.0.1.0",
                                            "access": "Allow",
                                            "priority": 110,
                                            "direction": "Inbound"
                                        }
                                    }
                                ]
                            }
                        }
                    ],
                    "outputs": {}
                }
            }
        },
        {
            "apiVersion": "2017-05-10",
            "name": "internalserverstemplate",
            "type": "Microsoft.Resources/deployments",
            "resourceGroup": "[parameters('internalserversrg')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', parameters('internalserversrg'))]",
                "DMZTemplate"
            ],
            "properties": {
                "mode": "incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                    "contentversion": "1.0.0.0",
                    "parameters": {},
                    "variables": {},
                    "resources": [
                        {
                            "apiVersion": "2017-06-01",
                            "type": "Microsoft.Network/virtualNetworks",
                            "name": "[parameters('internalvNetName')]",
                            "location": "[variables('Location')]",
                            "properties": {
                                "addressSpace": {
                                    "addressPrefixes": [
                                        "[parameters('internalvNetAddressPrefix')]"
                                    ]
                                },
                                "subnets": [
                                    {
                                        "name": "[parameters('internal-subnet1')]",
                                        "properties": {
                                            "addressPrefix": "[parameters('internal-subnet1Prefix')]"
                                        }
                                    }
                                ]
                            }
                        },
                        {
                            "name": "[concat(parameters('internalvNetName'), '/peered-to-', parameters('dmzvnet'))]",
                            "type": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings",
                            "apiVersion": "2018-08-01",
                            "location": "[variables('location')]",
                            "dependsOn": [
                                "[concat('Microsoft.Network/virtualNetworks/', parameters('internalvNetName'))]"
                            ],
                            "properties": {
                                "allowVirtualNetworkAccess": true,
                                "allowForwardedTraffic": true,
                                "allowGatewayTransit": true,
                                "useRemoteGateways": false,
                                "remoteVirtualNetwork": {
                                    "id": "[concat(subscription().id, '/resourceGroups/', parameters('DMZresourceGroup'), '/providers/Microsoft.Network/virtualNetworks/', parameters('DMZvNet'))]"
                                }
                            }
                        },
                        {
                            "name": "[concat(parameters('internalvNetName'), '/peered-to-', parameters('vnetname'))]",
                            "type": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings",
                            "apiVersion": "2018-08-01",
                            "location": "[variables('location')]",
                            "dependsOn": [
                                "[concat('Microsoft.Network/virtualNetworks/', parameters('internalvNetName'))]"
                            ],
                            "properties": {
                                "allowVirtualNetworkAccess": true,
                                "allowForwardedTraffic": true,
                                "allowGatewayTransit": true,
                                "useRemoteGateways": false,
                                "remoteVirtualNetwork": {
                                    "id": "[concat(subscription().id, '/resourceGroups/', parameters('HUBresourceGroup'), '/providers/Microsoft.Network/virtualNetworks/', parameters('vnetname'))]"
                                }
                            }
                        },
                        {
                            "apiVersion": "2015-06-15",
                            "type": "Microsoft.Network/networkSecurityGroups",
                            "name": "[parameters('internal-NSG')]",
                            "location": "[variables('location')]",
                            "properties": {
                                "securityRules": [
                                    {
                                        "name": "allow-rdp",
                                        "properties": {
                                            "description": "allow rdp",
                                            "protocol": "*",
                                            "sourcePortRange": "*",
                                            "destinationPortRange": "3389",
                                            "sourceAddressPrefix": "*",
                                            "destinationAddressPrefix": "10.0.1.0",
                                            "access": "Allow",
                                            "priority": 110,
                                            "direction": "Inbound"
                                        }
                                    }
                                ]
                            }
                        }
                    ],
                    "outputs": {}
                }
            }
        }
    ]
}

标签: azureazure-resource-managerazure-template

解决方案


我会做几乎你正在做的事情,但我会为对等创建嵌套模板,因为你有鸡和蛋的问题。您需要创建 vnet,等待其邻居 vnet 出现,然后创建对等互连。但是在部署中,您只能等待同一部署中的资源,因此这基本上是不可能的,除非您“一次”创建所有 vnet,并且只有在所有 vnet 准备好后才开始将事物对等起来。


推荐阅读