Azure虚拟机在使用Terraform增加磁盘大小时重新启动

dzhpxtsq  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(136)

Azure Portal增加管理盘大小时不需要重新启动,但Terraform增加管理盘大小时需要重新启动。是否有防止重新启动的代码?我单独使用Terraform代码制作磁盘。如果有人有类似的经验和解决方案,请与我们分享。

  • vm.tf
data "azurerm_resource_group" "system_rg_name" {
  name = azurerm_resource_group.system_rg.name
}

data "azurerm_subnet" "network_subnet" {
  name                 = var.network_subnet
  virtual_network_name = var.network_vnet
  resource_group_name  = var.network_rg
}

resource "azurerm_network_interface" "nic" {
  for_each            = var.vm_template
  name                = "${each.value.vm_name}-${each.value.nic}"
  location            = var.location
  resource_group_name = data.azurerm_resource_group.system_rg_name.name

  enable_accelerated_networking = true

  ip_configuration {
    name                          = "internal"
    subnet_id                     = data.azurerm_subnet.network_subnet.id
    private_ip_address_allocation = var.private-ip-type
    private_ip_address            = each.value.private_ip
  }
}

resource "azurerm_windows_virtual_machine" "vm" {
  for_each            = var.vm_template
  name                = each.value.vm_name
  location            = var.location
  resource_group_name = data.azurerm_resource_group.system_rg_name.name
  license_type        = each.value.license_type
  size                = each.value.vm_type

  additional_capabilities {
    ultra_ssd_enabled = true
  }

  network_interface_ids = [
    azurerm_network_interface.nic[each.key].id,
  ]

  identity {
    type = "SystemAssigned"
  }

  os_disk {
    name                 = "${each.value.vm_name}-${each.value.os_disk}"
    caching              = "ReadWrite"
    storage_account_type = each.value.osdisk_type
    disk_size_gb         = each.value.osdisk_size
  }

  source_image_reference {
    publisher = each.value.os_publisher
    offer     = each.value.os_offer
    sku       = each.value.os_sku
    version   = each.value.os_version
  }

  computer_name  = each.value.hostname
  admin_username = each.value.adminid
  admin_password = data.azurerm_key_vault_secret.password.value
  timezone       = "Korea Standard Time"

  lifecycle {
    ignore_changes = [
      zone,
    ]
  }
}

字符串

  • manageddisk.tf
locals {
  disk_vm_flat = merge([
    for vm, vm_vals in var.vm_template : {
      for disk, disk_vals in var.disk_template :
      "${vm_vals.vm_name}-${disk_vals.managed_disk_name}" => {
        vm      = vm
        vm_name = vm_vals.vm_name
        managed_disk_name    = disk_vals.managed_disk_name
        managed_disk_size    = disk_vals.managed_disk_size
        managed_disk_lun     = disk_vals.managed_disk_lun
        managed_disk_type    = disk_vals.managed_disk_type
        managed_disk_caching = disk_vals.managed_disk_caching
        os_type              = disk_vals.os_type
        create_option        = disk_vals.create_option
      } if disk_vals.managed_disk_size != "0"
    }
  ]...)
}

resource "azurerm_managed_disk" "managed_disk" {
  for_each = local.disk_vm_flat
  name     = "${each.value.vm_name}-${each.value.managed_disk_name}"
  location             = var.location
  resource_group_name  = data.azurerm_resource_group.system_rg_name.name
  storage_account_type = each.value.managed_disk_type
  create_option        = each.value.create_option
  os_type              = each.value.os_type
  disk_size_gb         = each.value.managed_disk_size
}

resource "azurerm_virtual_machine_data_disk_attachment" "disk_attach" {
  for_each = local.disk_vm_flat
  managed_disk_id = azurerm_managed_disk.managed_disk[each.key].id
  lun             = each.value.managed_disk_lun
  caching         = each.value.managed_disk_caching
  virtual_machine_id = azurerm_linux_virtual_machine.vm[each.value.vm].id
}

  • var.tf
variable "location" {
  type    = string
  default = "Korea Central"
}

### Resource Group Valiable - Network
variable "network_rg" {
  type    = string
  default = ""
}

### Virtual Network Valiable
variable "network_vnet" {
  type    = string
  default = ""
}

### Subnet Valiable
variable "network_subnet" {
  type    = string
  default = ""
}

### Resource Group Valiable
variable "system_rg" {
  type    = string
  default = "testrg"
}

### Private IP Setting
variable "private-ip-type" {
  type    = string
  default = "Static"
}

variable "vm_template" {
  type = map(any)
  default = {
    vm1 = {
      vm_name      = "test01vm"
      hostname     = "test01vm"
      os_publisher = "MicrosoftWindowsServer"
      os_offer     = "WindowsServer"
      os_sku       = "2022-datacenter-azure-edition"
      license_type = "Windows_Server"
      os_version   = "latest"
      osdisk_type  = "StandardSSD_LRS"
      osdisk_size  = "128"
      vm_type      = "Standard_D4s_v5"
      private_ip   = "10.x.x.x"
      nic          = "nic01"
      os_disk      = "osdisk01"
    }
    vm2 = {
      vm_name      = "test02vm"
      hostname     = "test02vm"
      os_publisher = "MicrosoftWindowsServer"
      os_offer     = "WindowsServer"
      os_sku       = "2022-datacenter-azure-edition"
      license_type = "Windows_Server"
      os_version   = "latest"
      osdisk_type  = "StandardSSD_LRS"
      osdisk_size  = "128"
      vm_type      = "Standard_D4s_v5"
      private_ip   = "10.x.x.x"
      nic          = "nic01"
      os_disk      = "osdisk01"
    }
  }
}

variable "disk_template" {
  type = map(any)
  default = {
    disk1 = {
      managed_disk_name    = "localdisk"
      managed_disk_type    = "StandardSSD_LRS"
      managed_disk_size    = "12"
      managed_disk_lun     = "1"
      managed_disk_caching = "ReadWrite"
      create_option        = "Empty"
      os_type              = "Windows"
    }
  }
}

rta7y2nd

rta7y2nd1#

我尝试在不使用Terraform重新启动的情况下提供更改管理磁盘大小的要求,我成功地实现了这一要求。
在Azure中,通过Azure Portal更改操作系统磁盘的大小通常不需要重新启动VM。但是,Terraform的行为因其处理磁盘恢复操作的方式而有所不同。
为了避免重新启动,Terraform需要将磁盘调整操作视为非中断性更改。这可能并不简单,因为Terraform的行为由Azure提供商的实现和Azure API的行为控制。

我的地形配置:

main.tf

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = var.resource_group_name
  location = var.location
}

resource "azurerm_virtual_network" "example" {
  name                = "demovk-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "demovk-subnet"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.1.0/24"]
}

resource "azurerm_network_interface" "example" {
  name                = "demovk-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_windows_virtual_machine" "example" {
  name                = "examplevk-vm"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  size                = "Standard_B2s"
  admin_username      = "adminuser"
  network_interface_ids = [azurerm_network_interface.example.id]
  admin_password      = "Password1234!"

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2019-Datacenter"
    version   = "latest"
  }
}

字符串

manageddisk.tf:

resource "azurerm_managed_disk" "example_os" {
  name                 = "demovk-osdisk"
  location             = azurerm_resource_group.example.location
  resource_group_name  = azurerm_resource_group.example.name
  storage_account_type = "Premium_SSD"
  create_option        = "Empty"
  disk_size_gb         = 256

  lifecycle {
    ignore_changes = [disk_size_gb]
  }
}

resource "azurerm_virtual_machine_data_disk_attachment" "example_os_attach" {
  managed_disk_id    = azurerm_managed_disk.example_os.id
  virtual_machine_id = azurerm_windows_virtual_machine.example.id
  lun                = 10
  caching            = "ReadWrite"
}


variable.tf

variable "location" {
  description = "The location/region in which to create the resources"
  default     = "East US"
}

variable "resource_group_name" {
  description = "The name of the resource group in which to create the resources"
  default     = "demorg-vk1"
}

输出:


的数据



现在更改托管磁盘的大小,并检查更改是否按预期反映

manageddisk.tf:

resource "azurerm_managed_disk" "example_os" {
      .
      .
      .
      disk_size_gb         = 512
    
      lifecycle {
        ignore_changes = [disk_size_gb]
      }
    }


并继续进行上述的地形改造步骤。

应用terraform



相关问题