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

时间:2019-02-12 06:01:35

标签: azure terraform

当我尝试启动多个Azure VM时,Terraform的count元属性不起作用,而且Azure管理控制台上没有可用的此类选项。

仅启动一个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" {}

像AWS一样,我期望它应该启动指定数量的虚拟机。

2 个答案:

答案 0 :(得分:0)

我认为它不会为您创建单个VM。原因是,您只创建了一个OS磁盘,然后用该单个OS磁盘一一覆盖了VM,结果最后只有一个VM。

因此,您应该为您的VM创建不同的OS磁盘名称,其他资源也需要创建更多,这完全取决于您的VM数量。您可以这样做:

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"
 }
}

您可以从Create Multiple VMs through Terraform获取整个模板。

答案 1 :(得分:0)

解决此问题的另一种方法是省略os磁盘名称:

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

它将为您生成随机名称。