简体   繁体   English

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

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

recently I started learning/working with ARM templates and JSON so I'm a complete newbie to this.最近我开始学习/使用 ARM 模板和 JSON 所以我完全是新手。 I've been asked to make a template that creates a virtual machine selecting an existing virtual network and subnet within a subscription.我被要求制作一个模板来创建一个虚拟机,并在订阅中选择现有的虚拟网络和子网。

Everything works fine, except that whenever I make the deployment, the template creates a new vnet and subnet with randomized names instead of letting me pick from an existing one (the VM creates correctly though).一切正常,除了每当我进行部署时,模板都会创建一个具有随机名称的新 vnet 和子网,而不是让我从现有的 vnet 和子网中选择(尽管 VM 可以正确创建)。

I used https://github.com/Azure/azure-quickstart-templates/blob/master/101-vm-simple-rhel/azuredeploy.json quickstart template as a base and added a few lines (which I will put below) to let me type the name of my vnet and subnet as it does with the VM name, but it keeps creating new ones even though I type the name correctly.我使用https://github.com/Azure/azure-quickstart-templates/blob/master/101-vm-simple-rhel/azuredeploy.json快速入门模板作为基础并添加了几行(我将在下面放置)让我像使用 VM 名称一样键入我的 vnet 和子网的名称,但即使我正确键入了名称,它也会不断创建新的名称。

The lines I added to the code in the Parameters section are:我在参数部分的代码中添加的行是:

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

Thank you in advance for your time!提前感谢您的宝贵时间!

To create a VM with the existing VNet base on the quickstart template you used, you only need to delete the virtual network resource in the resources block and the dependency on it and all the variables about the VNet and subnet except the variable subnetRef , then change this variable with your parameters like this if the VNet in the same resource group with the VM:要基于您使用的快速入门模板创建具有现有 VNet 的 VM,您只需删除resources块中的虚拟网络资源及其依赖关系以及除变量subnetRef之外的有关 VNet 和子网的所有变量,然后更改如果 VNet 与 VM 位于同一资源组中,则此变量与您的参数类似:

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

If the existing VNet in another resource group but in the same subscription, then the variable subnetRef should be changed like this:如果现有 VNet 在另一个资源组但在同一个订阅中,则应按如下方式更改变量subnetRef

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

According to the changes, the template will use the existing VNet and subnet instead of creating new ones.根据更改,模板将使用现有的 VNet 和子网,而不是创建新的。

Take a look at this sample:看看这个样本:

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

It shows how you can use a pattern for new/existing/none on resources in a template.它展示了如何在模板中的资源上使用新/现有/无资源的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM