azure - 使用复制对象创建多个 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'))]"
]
},
- 我是简单地在 VM 上使用副本还是必须在 NIC 和 OS 磁盘上添加它?
- 我尝试过的一种语法。我可以重试的示例语法会很有用。
"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" },
解决方案
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())]"
]
},
推荐阅读
- java - 如何准确测量 API POST 请求的响应时间
- mysql - 无法加载身份验证插件'caching_sha2_password' - MySql 和 Ruby on Rails
- tomcat - Tomcat 静态缓存“cacheTtl”
- javascript - 淘汰赛js中不同视图模型的计算返回值
- flutter - 未捕获(承诺中)错误:预期类型为“列表”的值
',但得到了 '_JsonMap' 类型之一 - kubernetes - 在 Helm 模板或标准 yaml 资源文件中使用 valueFrom 时添加引号
- c++ - 怎么可能绑定到这个转发函数?
- python - 如果存在于其他列表中,Python 从列表中删除项目
- php - 不同用户的 Laravel 布局
- linux - “smee” webhook 传递客户端停止工作(事件 { 类型:'错误',消息:未定义})?