使用现有 VNet 和子网创建 VM 的 ARM 模板

ARM template to create a VM using an existing VNet and subnet

最近我开始 learning/working 使用 ARM 模板和 JSON 所以我完全是个新手。我被要求制作一个模板来创建一个虚拟机,该虚拟机在订阅中选择一个现有的虚拟网络和子网。

一切正常,除了每当我进行部署时,模板都会创建一个新的 vnet 和具有随机名称的子网,而不是让我从现有的中选择(尽管 VM 正确创建)。

我使用 https://github.com/Azure/azure-quickstart-templates/blob/master/101-vm-simple-rhel/azuredeploy.json 快速入门模板作为基础并添加了几行(我将在下面放置)让我像输入 VM 名称一样输入 vnet 和子网的名称,但是它即使我输入正确的名称,也会继续创建新的。

我在 Parameters 部分的代码中添加的行是:

"virtualNetworkName": {
    "type": "string",
    "metadata": {
        "description": "VNet to which the VM will connect."
    }
},
"subnetName": {
    "type": "string",
    "metadata": {
        "description": "Subnet to which the VM will connect."
    }
}

提前感谢您的宝贵时间!

要根据您使用的快速入门模板使用现有VNet创建VM,您只需要删除resources块中的虚拟网络资源及其依赖项以及有关VNet的所有变量和子网,但变量 subnetRef 除外,如果 VNet 与 VM 在同一资源组中,则使用您的参数更改此变量:

"subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetworkName'), parameters('subnetName'))]",

如果现有 VNet 在另一个资源组中但在同一个订阅中,则变量 subnetRef 应更改为:

"subnetRef": "[resourceId('otherResourceGroup', 'Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetworkName'), parameters('subnetName'))]",

根据更改,模板将使用现有的 VNet 和子网,而不是创建新的。

看看这个样本:

https://github.com/Azure/azure-quickstart-templates/tree/master/100-marketplace-sample

它展示了如何在模板中的资源上使用 new/existing/none 的模式。