Terraform azurerm_managed_disk在Terraform应用并销毁VM之后总是更换

发布于 2025-02-13 04:40:19 字数 5799 浏览 1 评论 0 原文

如下所示,我正在尝试使用azurerm_managed_disk部署新的虚拟机。 到第一次运行良好,但是我运行Terraform计划/应用Azurerm_Managed_disk资源的每一刻都在重建,因此使用此磁盘的VM也是重建的。有人知道发生了什么吗?

代码:

resource "azurerm_snapshot" "snapshot" {
  name                = "az1srzertosnaphot"
  location            = azurerm_resource_group.resource_group.location
  resource_group_name = azurerm_resource_group.resource_group.name
  create_option       = "Copy"
  source_resource_id  = data.azurerm_managed_disk.zerto_managed_disk.id
}

resource "azurerm_managed_disk" "zerto_managed_disk" {
  name                 = "az1srzerto_managed_disk"
  location             = azurerm_resource_group.resource_group.location
  resource_group_name  = azurerm_resource_group.resource_group.name
  storage_account_type = "Standard_LRS"
  create_option        = "Copy"
  source_resource_id   = azurerm_snapshot.snapshot.id
  disk_size_gb         = "127"
}

resource "azurerm_virtual_machine" "zerto_virtual_machine" {
  name                  = var.vm_zerto_name
  location              = azurerm_resource_group.resource_group.location
  resource_group_name   = azurerm_resource_group.resource_group.name
  vm_size               = "Standard_D2s_v3"
  network_interface_ids = [azurerm_network_interface.vm_zerto_interface.id]
  

  storage_os_disk {
    name              = azurerm_managed_disk.zerto_managed_disk.name
    caching           = "ReadWrite"
    disk_size_gb      = "127"
    os_type           = "Windows"
    managed_disk_type = "Standard_LRS"
    managed_disk_id   = azurerm_managed_disk.zerto_managed_disk.id
    create_option     = "Attach"
  }

  os_profile_windows_config {
    provision_vm_agent = false

遵循Terraform计划

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # module.disaster_recovery.azurerm_managed_disk.zerto_managed_disk must be replaced
-/+ resource "azurerm_managed_disk" "zerto_managed_disk" {
      ~ disk_iops_read_only           = 0 -> (known after apply)
      ~ disk_iops_read_write          = 500 -> (known after apply)
      ~ disk_mbps_read_only           = 0 -> (known after apply)
      ~ disk_mbps_read_write          = 60 -> (known after apply)
      - hyper_v_generation            = "V1" -> null # forces replacement
      ~ id                            = "/subscriptions/XXXXXXXXXXXX/resourceGroups/az-prd-euw-disasterrecovery-rg-001/providers/Microsoft.Compute/disks/az1srzerto_managed_disk" -> (known after apply)
      + logical_sector_size           = (known after apply)
      ~ max_shares                    = 0 -> (known after apply)
        name                          = "az1srzerto_managed_disk"
      - on_demand_bursting_enabled    = false -> null
      - os_type                       = "Windows" -> null
      + source_uri                    = (known after apply)
      - tags                          = {} -> null
      + tier                          = (known after apply)
      - trusted_launch_enabled        = false -> null
        # (7 unchanged attributes hidden)
    }

  # module.disaster_recovery.azurerm_virtual_machine.zerto_virtual_machine must be replaced
-/+ resource "azurerm_virtual_machine" "zerto_virtual_machine" {
      + availability_set_id              = (known after apply)
      ~ id                               = "/subscriptions/XXXXXXXXXX/resourceGroups/az-prd-euw-disasterrecovery-rg-001/providers/Microsoft.Compute/virtualMachines/az1srzerto" -> (known after apply)
      + license_type                     = (known after apply)
        name                             = "az1srzerto"
      - tags                             = {} -> null
      - zones                            = [] -> null
        # (6 unchanged attributes hidden)

      + identity {
          + identity_ids = (known after apply)
          + principal_id = (known after apply)
          + type         = (known after apply)
        }


      + storage_data_disk {
          + caching                   = (known after apply)
          + create_option             = (known after apply)
          + disk_size_gb              = (known after apply)
          + lun                       = (known after apply)
          + managed_disk_id           = (known after apply)
          + managed_disk_type         = (known after apply)
          + name                      = (known after apply)
          + vhd_uri                   = (known after apply)
          + write_accelerator_enabled = (known after apply)
        }

      + storage_image_reference {
          + id        = (known after apply)
          + offer     = (known after apply)
          + publisher = (known after apply)
          + sku       = (known after apply)
          + version   = (known after apply)
        }

      ~ storage_os_disk {
          ~ managed_disk_id           = "/subscriptions/XXXXXXXXXX/resourceGroups/az-prd-euw-disasterrecovery-rg-001/providers/Microsoft.Compute/disks/az1srzerto_managed_disk" -> (known after apply) # forces replacement
            name                      = "az1srzerto_managed_disk"
            # (6 unchanged attributes hidden)
        }
        # (1 unchanged block hidden)
    }

Plan: 2 to add, 0 to change, 2 to destroy.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────    

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

有什么想法吗?

As you can see below, I'm trying to deploy a new virtual machine using azurerm_managed_disk.
By the first time it works fine but every moment that I run terraform plan/apply the azurerm_managed_disk resource is rebuild that hence the VM that is using this disk is rebuild as well. Does anyone know what is happing ?

Code:

resource "azurerm_snapshot" "snapshot" {
  name                = "az1srzertosnaphot"
  location            = azurerm_resource_group.resource_group.location
  resource_group_name = azurerm_resource_group.resource_group.name
  create_option       = "Copy"
  source_resource_id  = data.azurerm_managed_disk.zerto_managed_disk.id
}

resource "azurerm_managed_disk" "zerto_managed_disk" {
  name                 = "az1srzerto_managed_disk"
  location             = azurerm_resource_group.resource_group.location
  resource_group_name  = azurerm_resource_group.resource_group.name
  storage_account_type = "Standard_LRS"
  create_option        = "Copy"
  source_resource_id   = azurerm_snapshot.snapshot.id
  disk_size_gb         = "127"
}

resource "azurerm_virtual_machine" "zerto_virtual_machine" {
  name                  = var.vm_zerto_name
  location              = azurerm_resource_group.resource_group.location
  resource_group_name   = azurerm_resource_group.resource_group.name
  vm_size               = "Standard_D2s_v3"
  network_interface_ids = [azurerm_network_interface.vm_zerto_interface.id]
  

  storage_os_disk {
    name              = azurerm_managed_disk.zerto_managed_disk.name
    caching           = "ReadWrite"
    disk_size_gb      = "127"
    os_type           = "Windows"
    managed_disk_type = "Standard_LRS"
    managed_disk_id   = azurerm_managed_disk.zerto_managed_disk.id
    create_option     = "Attach"
  }

  os_profile_windows_config {
    provision_vm_agent = false

Follow the terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # module.disaster_recovery.azurerm_managed_disk.zerto_managed_disk must be replaced
-/+ resource "azurerm_managed_disk" "zerto_managed_disk" {
      ~ disk_iops_read_only           = 0 -> (known after apply)
      ~ disk_iops_read_write          = 500 -> (known after apply)
      ~ disk_mbps_read_only           = 0 -> (known after apply)
      ~ disk_mbps_read_write          = 60 -> (known after apply)
      - hyper_v_generation            = "V1" -> null # forces replacement
      ~ id                            = "/subscriptions/XXXXXXXXXXXX/resourceGroups/az-prd-euw-disasterrecovery-rg-001/providers/Microsoft.Compute/disks/az1srzerto_managed_disk" -> (known after apply)
      + logical_sector_size           = (known after apply)
      ~ max_shares                    = 0 -> (known after apply)
        name                          = "az1srzerto_managed_disk"
      - on_demand_bursting_enabled    = false -> null
      - os_type                       = "Windows" -> null
      + source_uri                    = (known after apply)
      - tags                          = {} -> null
      + tier                          = (known after apply)
      - trusted_launch_enabled        = false -> null
        # (7 unchanged attributes hidden)
    }

  # module.disaster_recovery.azurerm_virtual_machine.zerto_virtual_machine must be replaced
-/+ resource "azurerm_virtual_machine" "zerto_virtual_machine" {
      + availability_set_id              = (known after apply)
      ~ id                               = "/subscriptions/XXXXXXXXXX/resourceGroups/az-prd-euw-disasterrecovery-rg-001/providers/Microsoft.Compute/virtualMachines/az1srzerto" -> (known after apply)
      + license_type                     = (known after apply)
        name                             = "az1srzerto"
      - tags                             = {} -> null
      - zones                            = [] -> null
        # (6 unchanged attributes hidden)

      + identity {
          + identity_ids = (known after apply)
          + principal_id = (known after apply)
          + type         = (known after apply)
        }


      + storage_data_disk {
          + caching                   = (known after apply)
          + create_option             = (known after apply)
          + disk_size_gb              = (known after apply)
          + lun                       = (known after apply)
          + managed_disk_id           = (known after apply)
          + managed_disk_type         = (known after apply)
          + name                      = (known after apply)
          + vhd_uri                   = (known after apply)
          + write_accelerator_enabled = (known after apply)
        }

      + storage_image_reference {
          + id        = (known after apply)
          + offer     = (known after apply)
          + publisher = (known after apply)
          + sku       = (known after apply)
          + version   = (known after apply)
        }

      ~ storage_os_disk {
          ~ managed_disk_id           = "/subscriptions/XXXXXXXXXX/resourceGroups/az-prd-euw-disasterrecovery-rg-001/providers/Microsoft.Compute/disks/az1srzerto_managed_disk" -> (known after apply) # forces replacement
            name                      = "az1srzerto_managed_disk"
            # (6 unchanged attributes hidden)
        }
        # (1 unchanged block hidden)
    }

Plan: 2 to add, 0 to change, 2 to destroy.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────    

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

Any idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗藏城府 2025-02-20 04:40:19

我们尝试使用带有托管磁盘和快照的Terraform创建虚拟机。

如 @ marko e 所建议的, 我们还观察到了不同的参数(即,create_option =“ empty”,然后将其更改为“复制”) << /em>。

错误详细信息: -

在更改值之前 : -

我们正在使用的代码如下。

main.tf

provider   "azurerm"   { 
    features   {} 
 } 


variable "prefix" {
  default = "examplem"
}

locals {
  vm_name = "${var.prefix}-vm"
}

resource "azurerm_resource_group" "example" {
  name     = "${var.prefix}-resources"
  location = "West Europe"
}

resource "azurerm_virtual_network" "main" {
  name                = "${var.prefix}-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "internal" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.main.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "main" {
  name                = "${var.prefix}-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

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

resource "azurerm_virtual_machine" "example" {
  name                  = local.vm_name
  location              = azurerm_resource_group.example.location
  resource_group_name   = azurerm_resource_group.example.name
  network_interface_ids = [azurerm_network_interface.main.id]
  vm_size               = "Standard_F2"

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

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

  os_profile {
    computer_name  = local.vm_name
    admin_username = "yourusername"
    admin_password = "yourpassword!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}


resource "azurerm_managed_disk" "example" {
  name                 = "${local.vm_name}-disk1"
  location             = azurerm_resource_group.example.location
  resource_group_name  = azurerm_resource_group.example.name
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  //source_resource_id   = azurerm_snapshot.example.id
  disk_size_gb         = 10
}

resource "azurerm_snapshot" "example" {
  name                = "snapshot"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  create_option       = "Copy"
  source_resource_id  = azurerm_managed_disk.example.id
}

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

建议您 解决此问题,请确保您已经提供了与您之前在托管磁盘上提到的相同的参数。

- hyper_v_generation            = "V1"

成功应用: -

有关更多信息,请参阅此 博客hashicorp |

We have tried the same to create virtual machine using terraform with managed disk and snapshot.

As @Marko E suggested, We have also observed the same thing with different parameter (i.e,create_option= "Empty" and changed it to "copy") .

ERROR DETAILS:-
enter image description here

AFTER CHANGING THE VALUE AS BEFORE:-
enter image description here

The code that we are using is below.

main.tf:

provider   "azurerm"   { 
    features   {} 
 } 


variable "prefix" {
  default = "examplem"
}

locals {
  vm_name = "${var.prefix}-vm"
}

resource "azurerm_resource_group" "example" {
  name     = "${var.prefix}-resources"
  location = "West Europe"
}

resource "azurerm_virtual_network" "main" {
  name                = "${var.prefix}-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "internal" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.main.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "main" {
  name                = "${var.prefix}-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

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

resource "azurerm_virtual_machine" "example" {
  name                  = local.vm_name
  location              = azurerm_resource_group.example.location
  resource_group_name   = azurerm_resource_group.example.name
  network_interface_ids = [azurerm_network_interface.main.id]
  vm_size               = "Standard_F2"

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

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

  os_profile {
    computer_name  = local.vm_name
    admin_username = "yourusername"
    admin_password = "yourpassword!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}


resource "azurerm_managed_disk" "example" {
  name                 = "${local.vm_name}-disk1"
  location             = azurerm_resource_group.example.location
  resource_group_name  = azurerm_resource_group.example.name
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  //source_resource_id   = azurerm_snapshot.example.id
  disk_size_gb         = 10
}

resource "azurerm_snapshot" "example" {
  name                = "snapshot"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  create_option       = "Copy"
  source_resource_id  = azurerm_managed_disk.example.id
}

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

Suggest you, to resolve this issue, make sure that you have provided the same parameter with value as you have mentioned before on your managed disk.

- hyper_v_generation            = "V1"

SUCCESSFULLY APPLIED:-

enter image description here

For more information please refer this Blog by hashicorp|Manages a Disk Snapshot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文