简体   繁体   中英

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. 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).

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.

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:

"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:

"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.

Take a look at this 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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