简体   繁体   English

ARM 模板 - 将动态数据磁盘添加到 VM 的值和参数?

[英]ARM Templates - Values and parameters for Adding Dynamic Data disks to VMs?

I'm new to ARM Templates.我是 ARM 模板的新手。 I've downloaded an ARM Template from the Portal after building a VM with 1 managed Data Disk.在使用 1 个托管数据磁盘构建 VM 后,我从门户下载了一个 ARM 模板。 My objective is to use ARM Templates to build several VMs in a row.我的目标是使用 ARM 模板来连续构建多个 VM。 For now, with identical parameters, except for the VM Name and of course NIC and Disks Names.目前,使用相同的参数,除了 VM 名称,当然还有 NIC 和磁盘名称。 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.我注意到 parameters.json 文件有硬编码的值,它不能用作模板,所以我开始修改以查看如何使它更具动态性。 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.但是在参数文件中有两个对象,dataDisks 和 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.我不明白为什么参数有两个不同的对象而不是一个(例如,dataDisks 中的所有内容而不是 dataDisksResources),我也不明白为什么 VM 磁盘属性的参数不同,并且超过磁盘资源参数。

This is the template.json这是模板.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这是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.例如,它们在同一个模板文件中声明了所有磁盘属性,参数和属性较少,并且任何地方都没有任何 dataDisksResources 对象。

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)我想了解我需要如何修改这些磁盘结构以添加动态命名来命名它们,例如 Azure 门户所做的 (VMName_DataDisk_Lunnumber)

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.因为您必须在创建数据磁盘和附加数据磁盘时指定不同的输入,但您不必创建它,所以您可以只告诉 VM 创建这些输入。 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.您还可以添加一个名为name的属性来为它们指定一个名称。

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

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

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