0
votes

I'm new to ARM Templates. I've downloaded an ARM Template from the Portal after building a VM with 1 managed Data Disk. My objective is to use ARM Templates to build several VMs in a row. For now, with identical parameters, except for the VM Name and of course NIC and Disks Names. I noticed the parameters.json file had hardcoded values and that wouldn't work as a template, so I started modifying to see how could I make it more dynamic. However I don't understand the Data Disks structure, which, in this template, is divided among different components and that's making me struggle with Dynamic Naming for the Disks.

Data disks appear in the template as a Resource and then as a property of the VM, inside a copy function. However in the parameters file there are two objects, dataDisks and dataDisksResources. I don't understand why the parameters have two different objects instead of one (for example, everything inside dataDisks instead of also having a dataDisksResources) and I also don't get why the parameters of the VM disk property are different and more than the parameters of the Disk Resource.

This is the template.json

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string"
        },
        "subnetName": {
            "type": "string"
        },
        "virtualNetworkId": {
            "type": "string"
        },
        "virtualMachineName": {
            "type": "string"
        },
        "virtualMachineRG": {
            "type": "string"
        },
        "osDiskType": {
            "type": "string"
        },
        "dataDisks": {
            "type": "array"
        },
        "dataDiskResources": {
            "type": "array"
        },
        "virtualMachineSize": {
            "type": "string"
        },
        "adminUsername": {
            "type": "string"
        },
        "adminPassword": {
            "type": "secureString"
        },
        "diagnosticsStorageAccountName": {
            "type": "string"
        },
        "diagnosticsStorageAccountId": {
            "type": "string"
        },
        "diagnosticsStorageAccountType": {
            "type": "string"
        },
        "diagnosticsStorageAccountKind": {
            "type": "string"
        }
    },
    "variables": {
        "vnetId": "[parameters('virtualNetworkId')]",
        "subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]",
        "nicName": "[concat(parameters('virtualMachineName'), substring(uniqueString(resourceGroup().id),0,4))]"
    },
    "resources": [
        {
            "name": "[variables('nicName')]",
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2019-07-01",
            "location": "[parameters('location')]",
            "dependsOn": [],
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            },
                            "privateIPAllocationMethod": "Dynamic"
                        }
                    }
                ]
            },
            "tags": {
            }
        },
        {
            "name": "[concat(parameters('virtualMachineName'),'_DataDisk_0')]",
            "type": "Microsoft.Compute/disks",
            "apiVersion": "2019-07-01",
            "location": "[parameters('location')]",
            "properties": "[parameters('dataDiskResources')[copyIndex()].properties]",
            "sku": {
                "name": "[parameters('dataDiskResources')[copyIndex()].sku]"
            },
            "copy": {
                "name": "managedDiskResources",
                "count": "[length(parameters('dataDiskResources'))]"
            },
            "tags": {
            }
        },
        {
            "name": "[parameters('virtualMachineName')]",
            "type": "Microsoft.Compute/virtualMachines",
            "apiVersion": "2019-07-01",
            "location": "[parameters('location')]",
            "dependsOn": [
                "managedDiskResources",
                "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]",
                "[concat('Microsoft.Storage/storageAccounts/', parameters('diagnosticsStorageAccountName'))]"
            ],
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('virtualMachineSize')]"
                },
                "storageProfile": {
                    "osDisk": {
                        "createOption": "fromImage",
                        "managedDisk": {
                            "storageAccountType": "[parameters('osDiskType')]"
                        }
                    },
                    "imageReference": {
                        "publisher": "MicrosoftVisualStudio",
                        "offer": "VisualStudio",
                        "sku": "VS-2017-Ent-Latest-Win10-N",
                        "version": "latest"
                    },
                    "copy": [
                        {
                            "name": "dataDisks",
                            "count": "[length(parameters('dataDisks'))]",
                            "input": {
                                "lun": "[parameters('dataDisks')[copyIndex('dataDisks')].lun]",
                                "createOption": "[parameters('dataDisks')[copyIndex('dataDisks')].createOption]",
                                "caching": "[parameters('dataDisks')[copyIndex('dataDisks')].caching]",
                                "writeAcceleratorEnabled": "[parameters('dataDisks')[copyIndex('dataDisks')].writeAcceleratorEnabled]",
                                "diskSizeGB": "[parameters('dataDisks')[copyIndex('dataDisks')].diskSizeGB]",
                                "managedDisk": {
                                    "id": "[coalesce(parameters('dataDisks')[copyIndex('dataDisks')].id, if(equals(parameters('dataDisks')[copyIndex('dataDisks')].name, json('null')), json('null'), resourceId('Microsoft.Compute/disks', parameters('dataDisks')[copyIndex('dataDisks')].name)))]",
                                    "storageAccountType": "[parameters('dataDisks')[copyIndex('dataDisks')].storageAccountType]"
                                }
                            }
                        }
                    ]
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]"
                        }
                    ]
                },
                "osProfile": {
                    "computerName": "[parameters('virtualMachineName')]",
                    "adminUsername": "[parameters('adminUsername')]",
                    "adminPassword": "[parameters('adminPassword')]",
                    "windowsConfiguration": {
                        "enableAutomaticUpdates": true,
                        "provisionVmAgent": true
                    }
                },
                "licenseType": "Windows_Server",
                "diagnosticsProfile": {
                    "bootDiagnostics": {
                        "enabled": true,
                        "storageUri": "[concat('https://', parameters('diagnosticsStorageAccountName'), '.blob.core.windows.net/')]"
                    }
                }
            },
            "tags": {
            }
        },
        {
            "name": "[parameters('diagnosticsStorageAccountName')]",
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-06-01",
            "location": "[parameters('location')]",
            "properties": {},
            "kind": "[parameters('diagnosticsStorageAccountKind')]",
            "sku": {
                "name": "[parameters('diagnosticsStorageAccountType')]"
            },
            "tags": {
            }
        }
    ],
    "outputs": {
        "adminUsername": {
            "type": "string",
            "value": "[parameters('adminUsername')]"
        }
    }
}

And this is the parameters.json

 {   
        "location": {
            "value": "location"
        },
        "subnetName": {
            "value": "subnetname"
        },
        "virtualNetworkId": {
            "value": "networkid"
        },
        "virtualMachineRG": {
            "value": "vmRG"
        },
        "osDiskType": {
            "value": "Standard_LRS"
        },
        "dataDisks": {
            "value": [
                {
                    "lun": 0,
                    "createOption": "attach",
                    "caching": "None",
                    "writeAcceleratorEnabled": false,
                    "id": null,
                    "storageAccountType": null,
                    "name": null,
                    "diskSizeGB": null,
                    "diskEncryptionSet": {
                        "id": null
                    }
                }
            ]
        },
        "dataDiskResources": {
            "value": [
                {
                    "sku": "Standard_LRS",
                    "properties": {
                        "diskSizeGB": 128,
                        "creationData": {
                            "createOption": "empty"
                        }
                    }
                }
            ]
        },
        "virtualMachineSize": {
            "value": "Standard_B4ms"
        },
        "adminUsername": {
            "value": "admin"
        },
        "diagnosticsStorageAccountName": {
            "value": "rg01diag"
        },
        "diagnosticsStorageAccountId": {
            "value": "Microsoft.Storage/storageAccounts/rg01diag"
        },
        "diagnosticsStorageAccountType": {
            "value": "Standard_LRS"
        },
        "diagnosticsStorageAccountKind": {
            "value": "Storage"
        } }

I also can't find any documentation for this kind of template. All the quick templates I find have a simpler version of this. For example they state all the disks properties inside the same template file, the parameters and properties are fewer and there isn't any dataDisksResources object anywhere.

I want to understand how would I need to modify these Disk structure to add dynamic naming that names them, for example, as Azure portal does (VMName_DataDisk_Lunnumber)

1

1 Answers

1
votes

Because you have to specify different input when you create the data disk and when you attach it, but you dont have to create it, you can just tell the VM to create those. thsis would be one way of doing that:

      "dataDisks": [
        {
          "diskSizeGB": "[parameters('sizeOfEachDataDiskInGB')]",
          "lun": 0,
          "createOption": "Empty"
        },
        {
          "diskSizeGB": "[parameters('sizeOfEachDataDiskInGB')]",
          "lun": 1,
          "createOption": "Empty"
        },
        {
          "diskSizeGB": "[parameters('sizeOfEachDataDiskInGB')]",
          "lun": 2,
          "createOption": "Empty"
        },
        {
          "diskSizeGB": "[parameters('sizeOfEachDataDiskInGB')]",
          "lun": 3,
          "createOption": "Empty"
        }
      ],

and you dont have to have a separate disk resource, these would be created automatically. you can also add a property called name to specify a name for those.

https://github.com/Azure/azure-quickstart-templates/blob/master/101-vm-multiple-data-disk/azuredeploy.json