0

我正在尝试对不同订阅(集线器和辐条模型)中的现有 vnet 进行 vnet 对等。我想动态地提供订阅 ID(不是硬编码)。我知道我们可以将 susbcription().id 用于相同的订阅,但不同订阅的功能是什么

4

1 回答 1

0

如评论中所述,在订阅 A 中部署模板时,没有获取订阅 B id 的功能。您必须手动提供此中提到的订阅 B id Microsoft Document

例子:

您可以使用以下模板在不同订阅中对 VNET 进行 vnet 对等互连:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vnetAName": {
            "type": "string",
            "defaultValue":"ansuman-vnet",
            "metadata": {
                "description": "Name of the first VNET"
            }
        },
        "vnetBName": {
            "type": "string",
            "defaultValue":"vnet-ansuman",
            "metadata": {
                "description": "Name of the Second VNET"
            }
        },
        "vnetAPrefix": {
            "type": "string",
            "defaultValue": "10.0.0.0/16",
            "metadata": {
                "description": "Prefix of the first VNET"
            }
        },
        "vnetBPrefix": {
            "type": "string",
            "defaultValue": "10.1.0.0/16",
            "metadata": {
                "description": "Prefix of the Second VNET"
            }
        },
        "subscriptionAID": {
            "type": "string",
            "metadata": {
                "description": "the Subscription ID for the first VNET"
            },
            "defaultValue": "subA"

        },
        "resourceGroupAName": {
            "type": "string",
            "defaultValue": "ansumantest",
            "metadata": {
                "description": "the resource group name for the first VNET"
            }
        },
        "subscriptionBID": {
            "type": "string",
            "defaultValue": "subB",
            "metadata": {
                "description": "the Subscription ID for the second VNET"
            }
        },
        "resourceGroupBName": {
            "type": "string",
            "defaultValue": "rgB",
            "metadata": {
                "description": "the resource group name for the second VNET"
            }
        },
        "location": {
            "type": "string",
            "defaultValue": "West US 2"
        }
    },
    "variables": {
        "vnetAtoVnetBPeeringName": "[concat(parameters('vnetAName'),'-to-',parameters('vnetBName'))]",
        "vnetBtoVnetAPeeringName": "[concat(parameters('vnetBName'),'-to-',parameters('vnetAName'))]"
    },
    "resources": [
        {
            "apiVersion": "2020-06-01",
            "name": "createPeeringAtoB",
            "type": "Microsoft.Resources/deployments",
            "location": "[parameters('location')]",
            "subscriptionId": "[parameters('subscriptionAID')]",
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2019-08-01/subscriptionDeploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {},
                    "variables": {},
                    "resources": [
                        {
                            "type": "Microsoft.Resources/deployments",
                            "apiVersion": "2020-06-01",
                            "name": "createNetworkPeeringfromA",
                            "location": "[parameters('location')]",
                            "properties": {
                                  "mode": "Incremental",
                                  "template": {
                                  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                                  "contentVersion": "1.0.0.0",
                                  "resources": [
                                    {
                                        "type": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings",
                                        "apiVersion": "2020-05-01",
                                        "name": "[concat(parameters('vnetAName'), '/', variables('vnetAtoVnetBPeeringName'))]",
                                        "properties": {
                                            "peeringState": "Connected",
                                            "remoteVirtualNetwork": {
                                                "id": "[concat('/subscriptions/',parameters('subscriptionBID'),'/resourceGroups/',parameters('resourceGroupBName'),'/providers/Microsoft.Network/virtualNetworks/', parameters('vnetBName'))]"
                                            },
                                            "allowVirtualNetworkAccess": true,
                                            "allowForwardedTraffic": true,
                                            "allowGatewayTransit": false,
                                            "useRemoteGateways": false,
                                            "remoteAddressSpace": {
                                                "addressPrefixes": [
                                                    "[parameters('vnetBPrefix')]"
                                                ]
                                            }
                                        }
                                    }
                                  ]
                                }
                            }
                        }
                    ]
                }
            }
        },
        {
            "apiVersion": "2020-06-01",
            "name": "createPeeringBtoA",
            "type": "Microsoft.Resources/deployments",
            "location": "[parameters('location')]",
            "subscriptionId": "[parameters('subscriptionBID')]",
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2019-08-01/subscriptionDeploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {},
                    "variables": {},
                    "resources": [
                        {
                            "type": "Microsoft.Resources/deployments",
                            "apiVersion": "2020-06-01",
                            "name": "createNetworkPeeringfromB",
                            "location": "[parameters('location')]",
                            "properties": {
                                  "mode": "Incremental",
                                  "template": {
                                  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                                  "contentVersion": "1.0.0.0",
                                  "resources": [
                                    {
                                        "type": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings",
                                        "apiVersion": "2020-05-01",
                                        "name": "[concat(parameters('vnetBName'), '/', variables('vnetBtoVnetAPeeringName'))]",
                                        "properties": {
                                            "peeringState": "Connected",
                                            "remoteVirtualNetwork": {
                                                "id": "[concat('/subscriptions/',parameters('subscriptionAID'),'/resourceGroups/',parameters('resourceGroupAName'),'/providers/Microsoft.Network/virtualNetworks/', parameters('vnetAName'))]"
                                            },
                                            "allowVirtualNetworkAccess": true,
                                            "allowForwardedTraffic": true,
                                            "allowGatewayTransit": false,
                                            "useRemoteGateways": false,
                                            "remoteAddressSpace": {
                                                "addressPrefixes": [
                                                    "[parameters('vnetAPrefix')]"
                                                ]
                                            }
                                        }
                                    }
                                  ]
                                }
                            }
                        }
                    ]
                }
            }
        } 
    ],
    "outputs": {     
    }
}

输出:

在此处输入图像描述 在此处输入图像描述

注意:如果您将上述代码部署到订阅 A,那么您可以替换"[parameters('subscriptionAID')]"subscription().id,类似地,如果您将其部署到订阅 B,那么您可以替换"[parameters('subscriptionBID')]"subscription().id. Assubscription().id仅获取当前订阅的值,即模板部署到的位置

于 2021-12-14T09:29:51.660 回答