简体   繁体   中英

Azure ARM Template with VM and subnet

I have a resource group called network-rg containing a virtual network. I am trying to deploy a Virual Machine in a new resource group vm-rg . The VM should be connected to a new subnet on the vnet in network-rg . I am using one single ARM template with both the subnet and the VM and deploying to vm-rg. How do I specify the subnet in the ARM template when its vnet is in another resource group than the primary/default group for the deployment?

I would need to explicitly reference the vnet with the resource group. This would be similar to how a Network Interface deploy is referencing the subnet ID in its ipConfigurations properties list:

  "apiVersion": "2015-05-01-preview",
  "type": "Microsoft.Network/networkInterfaces",
  "name": "[parameters('nicName')]",
  "location": "[parameters('location')]",
  "properties": {
      "ipConfigurations": [{
          "name": "ipconfig1",
          "properties": {
              "subnet": {
                  "id": "[variables('subnet1Ref')]"
              }
          }
      }]
   }

It seems you cannot create a subnet in another resource group in one template when you create a new resource group with resources. There no property for you to refer to the Vnet in another group.

If you really want to create a new subnet in another group in one template, you could take a look at linked and nested template . Hope this will help you.

You will need to add a variables (Resource in the template that references the parameter file) example below:

In the main.JSON file:

{
    "name": "[parameters('networkInterfaceName')]",
    "type": "Microsoft.Network/networkInterfaces",
    "apiVersion": "2018-04-01",
    "location": "[parameters('location')]",
    "dependsOn": [
      "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
    ],
    "properties": {
      "ipConfigurations": [
        {
          "name": "ipconfig1",
          "properties": {
            "subnet": {
              "id": "[variables('subnetRef')]"
            },
            "privateIPAllocationMethod": "Dynamic",
            "publicIpAddress": {
              "id": "[resourceId(parameters('resourceGroupName'),'Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]"
            }
          }
        }
      ],
      "enableAcceleratedNetworking": true,
      "networkSecurityGroup": {
        "id": "[resourceId(parameters('nsgResourceGroupName'), 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]"
      }
    }
  }   

Add the following variables in main.JSON:

{
    "name": "[parameters('networkInterfaceName')]",
    "type": "Microsoft.Network/networkInterfaces",
    "apiVersion": "2018-04-01",
    "location": "[parameters('location')]",
    "dependsOn": [
      "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
    ],
    "properties": {
      "ipConfigurations": [
        {
          "name": "ipconfig1",
          "properties": {
            "subnet": {
              "id": "[variables('subnetRef')]"
            },
            "privateIPAllocationMethod": "Dynamic",
          }
        }
      ],
    }
  }

Then add the following parameters in the main.JSON file:

"parameters": {
  "virtualNetworkName": {
  "type": "string"
},
"subnetName": {
  "type": "string"
},

In the main.parameters.json add the variable information:

"virtualNetworkName": {
  "value": "<vnet name>"
},
"resourceGroupName": {
  "value": "<whatever the rg is for the Network>"
},
"subnetName": {
  "value": "<subnet name>"
},

Hope that helps.

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