使用复制对象创建多个Azure VM

使用复制对象创建多个Azure VM,azure,arm-template,Azure,Arm Template,我有一个ARM模板和参数文件,它成功地在Azure中部署了一个加入域的VM 虚拟机 NIC 操作系统磁盘 它需要更新以部署500个虚拟机,增加名称后缀-01、-02、-03等。我试图在模板的参考资料部分使用copy对象,但遇到了问题,所以我想回顾一下我是如何实现这一点的 来自原始ARM模板的代码片段 "resources": [ { "type": "Microsoft.Network/networkInterfaces",

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

虚拟机 NIC 操作系统磁盘 它需要更新以部署500个虚拟机,增加名称后缀-01、-02、-03等。我试图在模板的参考资料部分使用copy对象,但遇到了问题,所以我想回顾一下我是如何实现这一点的

来自原始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'))]"
            ]
        },
我只是在虚拟机上使用拷贝,还是必须在NIC和OS磁盘上添加拷贝? 我尝试过的语法之一。我可以重试的示例语法将非常有用。 编辑: 更新了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()))]"
            ]
        },
最新错误:

新AzResourceGroupDeployment:9:41:51 PM-错误:Code=InvalidTemplate;Message=部署模板验证失败:模板中未定义资源“Microsoft.Compute/virtualMachines/vmname”。请看https://aka.ms/arm-template 有关用法的详细信息

参数文件包含此变量

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

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

A2。我建议您只使用VM名称后缀和复制索引,而不是像01、02等。您可以看到函数。然后,您可以更改您提供的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())]"
            ]
        },

还有问题吗?它能解决你的问题吗?如果是,请接受它作为答案。为什么不给出任何回应?对你有用吗?!很抱歉,由于我遇到了更多问题,因此延迟了响应。上面的原始帖子中没有看到编辑。我正在尝试找出在每个循环中向dnsLabelPrefix变量的增量值插入copyIndex的其他位置。该错误意味着您可能忘记定义虚拟机名称中使用的变量。我真的不明白你是什么意思?正如我看到的,您只是复制了我对您的问题的答案。dnsLabelPrefix变量用于命名nicName、vmNamePrefix、osDisk和VM扩展。所以我认为它的价值也需要增加。它的值在参数文件中设置,但我不确定该文件中是否可以使用copyIndex。
"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())]"
            ]
        },