使用 terraform 向 VM 添加额外的存储

Adding additional storage to VM with terraform

向 VM 添加额外存储的最佳方法是什么,使用下面的脚本 我正在使用有效的计数选项,但 storage_data_disk 使用导致它失败。

# Create virtual machine
resource "azurerm_linux_virtual_machine" "myterraformvm" {
count = 2
name                  = "testpoc0${count.index + 1}"
location              = "westeurope"
resource_group_name   = azurerm_resource_group.myterraformgroup.name
 #network_interface_ids = azurerm_network_interface.myterraformnic.*.id
network_interface_ids = [element(azurerm_network_interface.myterraformnic.*.id, count.index + 1)]
size                  = "Standard_B2s"

os_disk {
    name              = "OsDisk${count.index + 1}"
    caching           = "ReadWrite"
    storage_account_type = "Premium_LRS"
}

source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
}

computer_name  = "testpoc0${count.index}"
admin_username = "azureuser"
disable_password_authentication = true

admin_ssh_key {
    username       = "azureuser"
    public_key     = file("~/.ssh/poc.pub")
}

boot_diagnostics {
    storage_account_uri = azurerm_storage_account.mystorageaccount.primary_blob_endpoint
    }

tags = {
    environment = "poc"
    }
}

按照建议,您可以使用 Azure 托管磁盘资源创建数据磁盘,然后使用 Azure 虚拟机磁盘附件将磁盘附加到相应的虚拟机。

resource "azurerm_linux_virtual_machine" "myterraformvm" {
count = 2
name                  = "testpoc0${count.index + 1}"
location              = data.azurerm_resource_group.example.location
resource_group_name   = data.azurerm_resource_group.example.name
 #network_interface_ids = azurerm_network_interface.myterraformnic.*.id
network_interface_ids = [element(azurerm_network_interface.myterraformnic.*.id, count.index + 1)]
size                  = "Standard_B2s"

os_disk {
    name              = "OsDisk${count.index + 1}"
    caching           = "ReadWrite"
    storage_account_type = "Premium_LRS"
}

source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
}

computer_name  = "testpoc0${count.index}"
admin_username = "azureuser"
disable_password_authentication = true

admin_ssh_key {
    username       = "azureuser"
    public_key     = file("~/.ssh/id_rsa.pub")
}
}
resource "azurerm_managed_disk" "example" {
    count = 2
  name                 = "testpoc0${count.index + 1}-md"
  location             = data.azurerm_resource_group.example.location
  resource_group_name  = data.azurerm_resource_group.example.name
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  disk_size_gb         = 10
}


resource "azurerm_virtual_machine_data_disk_attachment" "example" {
    count=2
  managed_disk_id    = azurerm_managed_disk.example[count.index].id
  virtual_machine_id = azurerm_linux_virtual_machine.myterraformvm[count.index].id
  lun                ="10"
  caching            = "ReadWrite"
}

输出:

来自门户:

参考:

azurerm_virtual_machine_data_disk_attachment | Resources | hashicorp/azurerm | Terraform Registry