简体   繁体   English

如何在Azure中一次启动多个虚拟机

[英]How to launch multiple virtual machines at once in Azure

Terraform's count meta attribute is not working when I'm trying to launch multiple Azure VMs, and also there's no such option available on Azure Management Console. 当我尝试启动多个Azure VM时,Terraform的count元属性不起作用,而且Azure管理控制台上没有可用的此类选项。

The code works just fine by launching just a single VM, and it doesn't even give any error during the plan or apply stages of execution. 通过仅启动单个VM,该代码就可以正常工作,并且在计划或应用执行阶段都不会出现任何错误。

resource "azurerm_virtual_machine" "main" {
  name                  = "terra-vm"
  location              = "${var.location}"
  count                 = 2
  resource_group_name   = "${var.resourcegpname}"
 network_interface_ids = ["${element(var.netif, count.index)}"]
  vm_size               = "Standard_B1s"

  delete_os_disk_on_termination = true
  delete_data_disks_on_termination = true


  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"
  }
  storage_os_disk {
    name              = "myosdisk1"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }
  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
    custom_data = "sudo apt-get install apache2 -y && sudo systemctl 
start apache2"
  }
  os_profile_linux_config {
    disable_password_authentication = "false"
  }
  tags {
    environment = "staging"
  }
 }

 variable "netif" {
 type = "list"
 }
 variable "resourcegpname" {}
 variable "location" {}

Like AWS I was expecting it should launch a specified number of virtual machines. 像AWS一样,我期望它应该启动指定数量的虚拟机。

I think it does not create just a single VM for you. 我认为它不能为您创建单个VM。 The reason is that you just create a single OS disk and then the VMs are overridden one by one with the single OS disk, and that result in just a single VM at the end. 原因是,您只创建了一个OS磁盘,然后将VM用一个OS磁盘一个接一个地覆盖,最终导致只有一个VM。

So you should create different OS disk name with your VM, the other resources will also need to create more, it all dependant on the number of your VM. 因此,您应该使用VM创建不同的OS磁盘名称,其他资源也需要创建更多的磁盘名称,这完全取决于VM的数量。 You can do that like this: 您可以这样做:

resource "azurerm_virtual_machine" "test" {
 count                 = 2
 name                  = "acctvm${count.index}"
 location              = "${azurerm_resource_group.test.location}"
 availability_set_id   = "${azurerm_availability_set.avset.id}"
 resource_group_name   = "${azurerm_resource_group.test.name}"
 network_interface_ids = ["${element(azurerm_network_interface.test.*.id, count.index)}"]
 vm_size               = "Standard_DS1_v2"

 # Uncomment this line to delete the OS disk automatically when deleting the VM
 # delete_os_disk_on_termination = true

 # Uncomment this line to delete the data disks automatically when deleting the VM
 # delete_data_disks_on_termination = true

 storage_image_reference {
   publisher = "Canonical"
   offer     = "UbuntuServer"
   sku       = "16.04-LTS"
   version   = "latest"
 }

 storage_os_disk {
   name              = "myosdisk${count.index}"   <--here is core in your case
   caching           = "ReadWrite"
   create_option     = "FromImage"
   managed_disk_type = "Standard_LRS"
 }

 # Optional data disks
 storage_data_disk {
   name              = "datadisk_new_${count.index}"
   managed_disk_type = "Standard_LRS"
   create_option     = "Empty"
   lun               = 0
   disk_size_gb      = "1023"
 }

 storage_data_disk {
   name            = "${element(azurerm_managed_disk.test.*.name, count.index)}"
   managed_disk_id = "${element(azurerm_managed_disk.test.*.id, count.index)}"
   create_option   = "Attach"
   lun             = 1
   disk_size_gb    = "${element(azurerm_managed_disk.test.*.disk_size_gb, count.index)}"
 }

 os_profile {
   computer_name  = "hostname"
   admin_username = "testadmin"
   admin_password = "Password1234!"
 }

 os_profile_linux_config {
   disable_password_authentication = false
 }

 tags {
   environment = "staging"
 }
}

You can get the whole template from Create Multiple VMs through Terraform . 您可以通过Terraform从“ 创建多个虚拟机”中获取整个模板。

another way of fixing this is just omitting the os disk name: 解决此问题的另一种方法是省略os磁盘名称:

storage_os_disk {
  caching           = "ReadWrite"
  create_option     = "FromImage"
  managed_disk_type = "Standard_LRS"
}

It will generate random names for you. 它将为您生成随机名称。

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

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