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



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

  • 虚拟机
  • 网卡
  • 操作系统磁盘

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

https://learn.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 和操作系统磁盘上添加它?
  2. 我尝试过的语法之一。我可以重试的示例语法会很有用。
"name": "[concat(variables('vmSettings').vmNamePrefix), copyIndex()]"

编辑: 更新了 ARM 模板,添加了缺少的右括号"(",并暂时硬编码"count"值 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" },

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

回答 2.我建议您只将 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())]"
]
},

最新更新