使用 ARM 模板创建 NIC 时如何从另一个虚拟 network/resource 组引用子网

How to reference subnets from another virtual network/resource group when creating a NIC using ARM templates

我正在尝试为资源组中的 VM 创建 NIC。我遇到的问题是我试图从 Azure 中的另一个资源组引用子网。因此,我不得不在 ARM 模板中使用订阅级部署来引用它。

"subnetref": "[concat(subscription().id, '/resourceGroups/', parameters('HUB Network RG'), '/providers/Microsoft.Network/virtualNetworks/', parameters('HUB VNet'), '/virtualNetworks/subnets', parameters('HUB DC Subnet'))]"

以上是我要创建的子网引用变量。然后我有下面的我正在尝试创建的 VM NIC。

{
    "type": "Microsoft.Network/networkInterfaces",
    "name": "[variables('nicnamedc1')]",
    "location": "[variables('location')]",
    "apiVersion": "2018-10-01",
    "properties": {
        "ipConfigurations": [
            {
                "name": "ipconfig1",
                "properties": {
                    "privateIPAllocationMethod": "Dynamic",
                    "subnet": {
                        "id": "[variables('subnetRef')]"
                    }
                }
            }
        ]
    }
},

然后我得到以下错误。

New-AzDeployment : 14:54:23 - Resource Microsoft.Network/networkInterfaces 'before-nic' failed with message '{ "error": { "code": "InvalidRequestFormat", "message": "Cannot parse the request.", "details": [ { "code": "InvalidJsonReferenceFormat", "message": "Reference Id /subscriptions/404422c0-743d-4459-9db0-01892d0c7348/resourceGroups/hu b-network-rg/providers/Microsoft.Network/virtualNetworks/bsrgh-hub-vnetvirtualNetworks/subnetsdomain is not formatted correctly. The Id is expected to reference resources of type virtualNetworks/subnets. Path properties.ipConfigurations[0].properties.subnet."

我认为这是在抱怨我在顶部完成 subnetref 变量的格式。有更好的方法吗?还是我哪里出错了?

您忘记了代码中的 /

"subnetref": "[concat(subscription().id, '/resourceGroups/', parameters('HUB Network RG'), '/providers/Microsoft.Network/virtualNetworks/', parameters('HUB VNet'), '/virtualNetworks/subnets/', parameters('HUB DC Subnet'))]"

但你最好使用 resourceId() 函数:

resourceId(parameters('HUB Network RG'), 'Microsoft.Network/virtualNetworks/subnets', parameters('HUB VNet'), parameters('HUB DC Subnet'))

它更短且更不容易出错