首页 > 解决方案 > 使用复制对象创建多个 Azure VM

问题描述

我有一个 ARM 模板和参数文件,它成功地在 Azure 中部署了一个加入域的 VM。

它需要更新以部署 500 个虚拟机,增加名称后缀 -01、-02、-03 等。我正在尝试在模板的资源部分中使用复制对象,但遇到了问题,所以我想回顾一下我是如何处理这个问题的.

https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/create-multiple-instances

来自原始 ARM 模板的片段

    "resources": [
        {
            "type": "Microsoft.Network/networkInterfaces",
            "name": "[variables('nicName')]",
            "apiVersion": "[variables('apiVersion')]",
            "tags": "[parameters('tag')]",
            "location": "[parameters('location')]",
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig",
                        "properties": {
                            "privateIPAllocationMethod": "Dynamic",
                            "subnet": {
                                "id": "[parameters('subnetID')]"
                            }
                        }
                    }
                ]
            }
        },
        {
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[variables('vmSettings').vmNamePrefix]",
            "apiVersion": "2017-03-30",
            "tags": "[parameters('tag')]",
            "location": "[parameters('location')]",
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('vmSize')]"
                },
                "osProfile": {
                    "computerName": "[variables('vmSettings').vmNamePrefix]",
                    "adminUsername": "[variables('vmSettings').adminUserName]",
                    "adminPassword": "[variables('vmSettings').adminPassword]"
                },
                "storageProfile": {
                    "imageReference": "[variables('imageReference')]",
                    "osDisk": {
                        "name": "[concat(parameters('dnsLabelPrefix'), '-os')]",
                        "caching": "ReadWrite",
                        "createOption": "FromImage"
                    },
                    "dataDisks": [ ]
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]"
                        }
                    ]
                }
            },
            "dependsOn": [
                "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]"
            ]
        },
  1. 我是简单地在 VM 上使用副本还是必须在 NIC 和 OS 磁盘上添加它?
  2. 我尝试过的一种语法。我可以重试的示例语法会很有用。
"name": "[concat(variables('vmSettings').vmNamePrefix), copyIndex()]"

编辑:更新了 ARM 模板,现在添加了缺少的右括号“)”和硬编码“计数”值 3,以简化测试。最新版本是

    "resources": [
        {
            "type": "Microsoft.Network/networkInterfaces",
            "name": "[concat(variables('nicName'), '-', copyIndex())]",
            "apiVersion": "[variables('apiVersion')]",
            "tags": "[parameters('tag')]",
            "location": "[parameters('location')]",
            "copy": {
                "name": "nicLoop",
                "count": 3
            },
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig",
                        "properties": {
                            "privateIPAllocationMethod": "Dynamic",
                            "subnet": {
                                "id": "[parameters('subnetID')]"
                            }
                        }
                    }
                ]
            }
        },
        {
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[concat(variables('vmSettings').vmNamePrefix, '-', copyIndex())]",
            "apiVersion": "2017-03-30",
            "tags": "[parameters('tag')]",
            "location": "[parameters('location')]",
            "copy": {
                "name": "vmLoop",
                "count": 3
            },
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('vmSize')]"
                },
                "osProfile": {
                    "computerName": "[variables('vmSettings').vmNamePrefix]",
                    "adminUsername": "[variables('vmSettings').adminUserName]",
                    "adminPassword": "[variables('vmSettings').adminPassword]"
                },
                "storageProfile": {
                    "imageReference": "[variables('imageReference')]",
                    "osDisk": {
                        "name": "[concat(parameters('dnsLabelPrefix'), '-os')]",
                        "caching": "ReadWrite",
                        "createOption": "FromImage"
                    },
                    "dataDisks": [ ]
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('nicName'), '-', copyIndex()))]"
                        }
                    ]
                }
            },
            "dependsOn": [
                "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('nicName'), '-', copyIndex()))]"
            ]
        },

最新错误:

New-AzResourceGroupDeployment : 9:41:51 PM - Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The resource 'Microsoft.Compute/virtualMachines/vmname' is not defined in the template. Please see https://aka.ms/arm-template for usage details.'.

参数文件有这个变量

        "dnsLabelPrefix": { "value": "vmname" },

标签: azurearm-template

解决方案


A1。您需要在 VM 和 NIC 中添加副本,而不是在 OS 磁盘中添加副本。

A2。我建议你只使用带有复制索引的 VM 名称后缀,而不是像 01、02 等。你可以看到函数copyIndex()。然后,您可以更改您提供的 Nic 和 VM 的模板,如下所示:

"resources": [
        {
            "type": "Microsoft.Network/networkInterfaces",
            "name": "[concat(variables('nicName'), '-', copyIndex())]",
            "apiVersion": "[variables('apiVersion')]",
            "tags": "[parameters('tag')]",
            "location": "[parameters('location')]",
            "copy": {
                "name": "nicLoop",
                "count": "[parameters('numberOfInstances')]"
            },
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig",
                        "properties": {
                            "privateIPAllocationMethod": "Dynamic",
                            "subnet": {
                                "id": "[parameters('subnetID')]"
                            }
                        }
                    }
                ]
            }
        },
        {
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[concat(variables('vmSettings').vmNamePrefix, '-', copyIndex()]",
            "apiVersion": "2017-03-30",
            "tags": "[parameters('tag')]",
            "location": "[parameters('location')]",
            "copy": {
                "name": "vmLoop",
                "count": "[parameters('numberOfInstances')]"
            },
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('vmSize')]"
                },
                "osProfile": {
                    "computerName": "[variables('vmSettings').vmNamePrefix]",
                    "adminUsername": "[variables('vmSettings').adminUserName]",
                    "adminPassword": "[variables('vmSettings').adminPassword]"
                },
                "storageProfile": {
                    "imageReference": "[variables('imageReference')]",
                    "osDisk": {
                        "name": "[concat(parameters('dnsLabelPrefix'), '-os')]",
                        "caching": "ReadWrite",
                        "createOption": "FromImage"
                    },
                    "dataDisks": [ ]
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('nicName'), '-', copyIndex())]"
                        }
                    ]
                }
            },
            "dependsOn": [
                "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'), '-', copyIndex())]"
            ]
        },

推荐阅读