Step by Step: Move Virtual Machine between Virtual Network in Microsoft Azure ARM model 2


Recently I need to move few production Azure VMs between two virtual networks. Right now is not to simple because we need to remove current VM and redeploy into new vNET with old configuration. When our VM is deployed in Azure Resource Manager (ARM) model, we must edit JSON template.

Below you will find my way to edit the JSON template.

Test environment

To map the approximate production environment I prepared a test environment.

It consists of four Resource Groups which contain the following resources:

  • 1 VM with configuration (network card, public IP address. NSG rule)
  • 2 virtual networks
  • 1 storage account

Resource groups

I created four Resource Groups divided into individual functions:

2016-07-24_20-55-04.png

NewNetwork and OldNetwork contains vNETs with names respectively NewNetwork and OldNetwork.

Storage – contains storage account named ewteststorage where VM VHD disks are stored.

In VirtualMachines is a virtual machine running Windows Server 2012 R2 named testvm.

Virtual Machine configuration

Virtual Machine has been deployed from standard Windows Server 2012 R2 template in DS2 v2 size.

Disks

To the VM I joined 4 additional Data Disks named testvm-disk(01,02,03,04). And then I configured one virtual disk with StorageSpaces.

2016-07-24_22-08-39

2016-07-24_22-12-36

Network

Virtual Machine contains one network card attached to the OldNetwork.

2016-07-24_22-43-59

Target configuration

Transfer a virtual machine (network card) to the network NewNetwork maintaining the current configuration.

Export current configuration

The first step that we need to do is to export the configuration of the virtual machine to a JSON. Form Azure Portal (portal.azure.com) you can do this in few steps shown below:

  1. Choose correct Resource Group
  2. Then click on Virtual Machine icon

2016-07-24_22-18-58

3. In the Settings blade scroll down and click Automation script.

2016-07-24_22-23-37

4. A the end click on Download and download zip archive to your favorite location.

2016-07-24_22-23-53

Template preparation

Before we can re-use the template we have to adequately prepare JSON file.

Template construction

JSON template describing the configuration of Azure Resource Manager consists of six components: $schema, contentVersion arameters, Variables, Resources Outputs.

  • $schemarequired. Location of the JSON schema file that describes the version of the template language. You should use the URL shown above.
  • conventVersionrequired. Version of the template (such as 1.0.0.0). You can provide any value for this element.
  • Parameters – not required. Values that are provided when deployment is executed to customize resource deployment.
  • Variables – not required. Values that are used as JSON fragments in the template to simplify template language expressions.
  • Resourcesrequired. Resource types that are deployed or updated in a resource group.
  • Outputs – not required. Values that are returned after deployment.

ZIP archive and programs that you can use to edit templates

After downloading and unpack the zip file in the directory you will find six files. You need only file named template.json.

JSON templates you can edit easily with Visual Studio. For example you can use free Community version.

Another interesting program is a Visual Studio Code with armsnippet extension.

Edit templat

Now when we know our environment and know what we want we can start edit our ARM template.

Main steps are:

  1. Remove unnecessary parameters and appeals to them.
  2. Conversion Parameters to the Variables.
  3. Changing the appeals of the Parameters to the Variables.
  4. Changing the target network.
  5. Changing the configuration of the virtual machine.
  6. Changing the network interface configuration.
Remove unnecessary parameters and appeals to them

My ARM template looks like this:

We will deploy our virtual machine from existing VHD disk, so we don’t need to define all parameters, e.g. admin password (of course if we want to use existing one). So we can safely remove virtualMachines_testvm_adminPassword parameter.
We also can remove virtualMachines_testvm_primary – it’s not required when we defining network interface parameters.

When we remove these parameters above, we will be following:

“virtualMachines_testvm_name”
“networkInterfaces_testvm33_name”
“networkSecurityGroups_testvm_name”
“publicIPAddresses_testvm_name”
“networkInterfaces_testvm33_id”

Conversion Parameters to the Variables

After the initial cleaning, we can proceed to the next step. Now we need to convert Parameters to Variables. Cut of all parameters definitions and paste them to variables. Then correct phrase as shown below:

Original:

After:

Correct rest of variables.

Changing the appeals of the Parameters to the Variables

Originally, our resources have a references to parameters e.g. in network card:

That’s mean that the network card name will be generated based on networkInterfaces_testvm33_name parameter. We need to change parameters word to variables. You can use “replace all” (Ctrl+h).

“replace all” replace also first Parameters word which are a definition of parameters block. We need correct that.

Changing the target network

Now is the time when we finally can change our target network to the new one. If you look at my JSON template you can find networkInterfaces_testvm33_id variable (after correction) which definiens vNET configuration. We need to replace OldNetwork word to NewNetwork. In my test environment these two networks belongs to different Resource Groups, so we need also change target RG.

Original:

After:

And it’s almost over. Now we need correct some VM configuration.

Changing the configuration of the virtual machine

At the firs step we need to correct disks parameters.

We don’t need imageReference (1) because we will deploy VM from existing disk. Remove all imageReference block. Next, replace in points 2 and 3 creationOption to Attach. In point 4 remove information about size of disk. Our data disk already exists with correct size.

Additionally we need to declare which OS is installed on OS disk. We can do this with “osType”: “Windows”, in osDisk block:

The last step is to remove the osProfile block and existing references to non existing variables (which we removed earlier):

Before:

After:

Changing the network interface configuration

Last step in template preparing stage is a correction of network interface configuration. When we export our ARM template, virtual machine private IP also was exported. In new virtual network we will have different address space. So we need to remove or change this configuration. I chose second option.

Before:

After:

Uff… that is the end of template preparation. Now is time to deploy new VM.

Removing existing VM

Before we can deploy VM into new network, we need to remove existing virtual machine. Look at the steps below:

New VM creation

Finally! Now we can deploy our ARM template and create VM into the new vNET.

Let’s do that:

  1. Click New.
  2. In the search box write Template Deployment and press ENTER.
  3. Select the appropriate position.
  4. And then click Create.

In an open blade click Edit template (1) and in the editable area paste our prepared ARM template (2). Click Save (3).

Next select existing (1) and appropriate (2) Resource Group. Accept Legal terms (3,4) and finally click Create.

After few minutes our new Virtual Machine will be ready to use in a new vNET.

My new VM:

As you can see StorageSpaces configuration has been preservedOnly the private and public IP address has been changed.

And that’s it. If you have any question feel free to comment below.

It's only fair to share...Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0