Terraform 配置虚拟机失败。Linux/Windows资源块合并

Terraform 配置虚拟机失败。Linux/Windows资源块合并,terraform,terraform-provider-azure,Terraform,Terraform Provider Azure,这就是我得到的错误: Error: A resource with the ID "/subscriptions/subid/resourceGroups/tf-nonprod/providers/Microsoft.Compute/virtualMachines/windows-web1" already exists - to be managed via Terraform this resource needs to be imported into the Stat

这就是我得到的错误:

Error: A resource with the ID "/subscriptions/subid/resourceGroups/tf-nonprod/providers/Microsoft.Compute/virtualMachines/windows-web1" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_linux_virtual_machine" for more information.

  on .terraform\modules\vm_windows_app1\vm\main.tf line 3, in resource "azurerm_linux_virtual_machine" "vm_linux":
   3: resource "azurerm_linux_virtual_machine" "vm_linux" {
我的地形相关文件夹结构是:

nonprod
    |_ backend.tf
    |_ outputs.tf
    |_ provider.tf
    |_ resource_groups.tf
    |_ storage.tf
    |_ subnets.tf
    |_ variables.tf
    |_ vnets.tf
    |_ vms_lin.tf
    |_ vms_win.tf

modules
|_ resource_group
          |_ outputs.tf
          |_ variables.tf
          |_ main.tf
|_ storage
          |_ outputs.tf
          |_ variables.tf
          |_ main.tf
|_ network
          |_ vnet
                 |_ outputs.tf
                 |_ variables.tf
                 |_ main.tf
          |_ subnet
                 |_ outputs.tf
                 |_ variables.tf
                 |_ main.tf
|_ vm
          |_ data.tf
          |_ main.tf
          |_ nic.tf
          |_ outputs.tf
          |_ variables.tf

非棒-vms_-lin.tf


module "vm_linux_app1" {
  source                  = "git::ssh://git@ssh.dev.azure.com/v3/sampleuser/my_code/terraform_modules//vm"
  name                    = module.rg_app1.rg_name
  location                = module.rg_app1.rg_location
  vnet_name               = module.vnet_app1.vnet_name
  subnet_name             = module.subnet1_app1.subnet_name
  base_hostname           = "linux-web1"
  admin_username          = "azureuser"
  admin_password          = "W3lcomeWorld12!!"
  caching_option          = "ReadWrite"
  storage_account_type    = "Standard_LRS"
  vm_size                 = "Standard_B1s"
  vm_publisher            = "Canonical" 
  vm_offer                = "UbuntuServer"
  vm_sku                  = "18.04-LTS"
  vm_img_version          = "latest"
}
非命令-vms_-win.tf


module "vm_windows_app1" {
  source                  = "git::ssh://git@ssh.dev.azure.com/v3/sampleuser/my_code/terraform_modules//vm"
  name                    = module.rg_app1.rg_name
  location                = module.rg_app1.rg_location
  vnet_name               = module.vnet_app1.vnet_name
  subnet_name             = module.subnet1_app1.subnet_name
  base_hostname           = "windows-web1"
  admin_username          = "azureuser"
  admin_password          = "W3lcomeWorld12!!"
  caching_option          = "ReadWrite"
  storage_account_type    = "Standard_LRS"
  vm_size                 = "Standard_B1s"
  vm_publisher            = "MicrosoftWindowsServer" 
  vm_offer                = "WindowsServer"
  vm_sku                  = "2019-Datacenter"
  vm_img_version          = "latest"
}

modules-vm-main.tf


# Create the Virtual Machine
resource "azurerm_linux_virtual_machine" "vm_linux" {
  name                                = var.base_hostname
  location                            = var.location
  resource_group_name                 = var.name
  network_interface_ids               = azurerm_network_interface.vm_nic.*.id
  size                                = var.vm_size
  ##### azurerm_key_vault_secret data source to store password
  admin_username                            = var.admin_username
  admin_password                            = var.admin_password
  disable_password_authentication     = false

  source_image_reference {
    publisher                         = var.vm_publisher
    offer                             = var.vm_offer
    sku                               = var.vm_sku
    version                           = var.vm_img_version
  }

  os_disk {
    caching                           = var.caching_option
    storage_account_type              = var.storage_account_type
  }

}

resource "azurerm_windows_virtual_machine" "vm_windows" {
  name                                = var.base_hostname
  location                            = var.location
  resource_group_name                 = var.name
  network_interface_ids               =azurerm_network_interface.vm_nic.*.id
  size                                = var.vm_size
  admin_username                      = var.admin_username
  admin_password                      = var.admin_password

  source_image_reference {
    publisher                         = var.vm_publisher
    offer                             = var.vm_offer
    sku                               = var.vm_sku
    version                           = var.vm_img_version
  }

  os_disk {
    caching                           = var.caching_option
    storage_account_type              = var.storage_account_type
  }

}

我注释掉vms_lin.tf中的所有代码并运行terraform plan/apply,然后它成功地创建了Win VM

然后我取消vms_lin.tf中的所有代码,以提供Linux VM并运行terraform plan:

Terraform will perform the following actions:

  # module.vm_linux_app1.azurerm_linux_virtual_machine.vm_linux will be created
  + resource "azurerm_linux_virtual_machine" "vm_linux" {
      + admin_password                  = (sensitive value)
      + admin_username                  = "azureuser"
      + allow_extension_operations      = true
      + computer_name                   = (known after apply)
      + disable_password_authentication = false
      + id                              = (known after apply)
      + location                        = "australiasoutheast"
      + max_bid_price                   = -1
      + name                            = "linux-web1"
      + network_interface_ids           = (known after apply)
      + priority                        = "Regular"
      + private_ip_address              = (known after apply)
      + private_ip_addresses            = (known after apply)
      + provision_vm_agent              = true
      + public_ip_address               = (known after apply)
      + public_ip_addresses             = (known after apply)
      + resource_group_name             = "tf-nonprod"
      + size                            = "Standard_B1s"
      + virtual_machine_id              = (known after apply)
      + zone                            = (known after apply)

      + os_disk {
          + caching                   = "ReadWrite"
          + disk_size_gb              = (known after apply)
          + name                      = (known after apply)
          + storage_account_type      = "Standard_LRS"
          + write_accelerator_enabled = false
        }

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

  # module.vm_linux_app1.azurerm_network_interface.vm_nic will be created
  + resource "azurerm_network_interface" "vm_nic" {
      + applied_dns_servers           = (known after apply)
      + dns_servers                   = (known after apply)
      + enable_accelerated_networking = false
      + enable_ip_forwarding          = false
      + id                            = (known after apply)
      + internal_dns_name_label       = (known after apply)
      + internal_domain_name_suffix   = (known after apply)
      + location                      = "australiasoutheast"
      + mac_address                   = (known after apply)
      + name                          = "linux-web1-nic"
      + private_ip_address            = (known after apply)
      + private_ip_addresses          = (known after apply)
      + resource_group_name           = "tf-nonprod"
      + virtual_machine_id            = (known after apply)

      + ip_configuration {
          + name                          = "nic-pip"
          + primary                       = (known after apply)
          + private_ip_address            = (known after apply)
          + private_ip_address_allocation = "dynamic"
          + private_ip_address_version    = "IPv4"
          + public_ip_address_id          = (known after apply)
          + subnet_id                     = "/subscriptions/subid/resourceGroups/tf-nonprod/providers/Microsoft.Network/virtualNetworks/vnet-app1/subnets/subnet1"
        }
    }

  # module.vm_linux_app1.azurerm_public_ip.vm_pip will be created
  + resource "azurerm_public_ip" "vm_pip" {
      + allocation_method       = "Dynamic"
      + fqdn                    = (known after apply)
      + id                      = (known after apply)
      + idle_timeout_in_minutes = 4
      + ip_address              = (known after apply)
      + ip_version              = "IPv4"
      + location                = "australiasoutheast"
      + name                    = "linux-web1-pip"
      + resource_group_name     = "tf-nonprod"
      + sku                     = "Basic"
    }

  # module.vm_linux_app1.azurerm_windows_virtual_machine.vm_windows will be created
  + resource "azurerm_windows_virtual_machine" "vm_windows" {
      + admin_password             = (sensitive value)
      + admin_username             = "azureuser"
      + allow_extension_operations = true
      + computer_name              = (known after apply)
      + enable_automatic_updates   = true
      + id                         = (known after apply)
      + location                   = "australiasoutheast"
      + max_bid_price              = -1
      + name                       = "linux-web1"
      + network_interface_ids      = (known after apply)
      + priority                   = "Regular"
      + private_ip_address         = (known after apply)
      + private_ip_addresses       = (known after apply)
      + provision_vm_agent         = true
      + public_ip_address          = (known after apply)
      + public_ip_addresses        = (known after apply)
      + resource_group_name        = "tf-nonprod"
      + size                       = "Standard_B1s"
      + virtual_machine_id         = (known after apply)
      + zone                       = (known after apply)

      + os_disk {
          + caching                   = "ReadWrite"
          + disk_size_gb              = (known after apply)
          + name                      = (known after apply)
          + storage_account_type      = "Standard_LRS"
          + write_accelerator_enabled = false
        }

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

  # module.vm_windows_app1.azurerm_linux_virtual_machine.vm_linux will be created
  + resource "azurerm_linux_virtual_machine" "vm_linux" {
      + admin_password                  = (sensitive value)
      + admin_username                  = "azureuser"
      + allow_extension_operations      = true
      + computer_name                   = (known after apply)
      + disable_password_authentication = false
      + id                              = (known after apply)
      + location                        = "australiasoutheast"
      + max_bid_price                   = -1
      + name                            = "windows-web1"
      + network_interface_ids           = [
          + "/subscriptions/subid/resourceGroups/tf-nonprod/providers/Microsoft.Network/networkInterfaces/windows-web1-nic",
        ]
      + priority                        = "Regular"
      + private_ip_address              = (known after apply)
      + private_ip_addresses            = (known after apply)
      + provision_vm_agent              = true
      + public_ip_address               = (known after apply)
      + public_ip_addresses             = (known after apply)
      + resource_group_name             = "tf-nonprod"
      + size                            = "Standard_B1s"
      + virtual_machine_id              = (known after apply)
      + zone                            = (known after apply)

      + os_disk {
          + caching                   = "ReadWrite"
          + disk_size_gb              = (known after apply)
          + name                      = (known after apply)
          + storage_account_type      = "Standard_LRS"
          + write_accelerator_enabled = false
        }

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

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

------------------------------------------------------------------------

它正在尝试重新创建Win VM。。。它似乎将Linux/Win资源中的代码混合在一起

我对Terraform还是相当陌生,我宁愿尝试让它工作,而不是将我的Linux/Win模块分成两个文件夹

我该怎么做


任何帮助都将不胜感激:)

您的模块无条件地调用linux和windows资源。通常会传递一个标志或某种方法来确定您想要的是windows vm还是linux vm。使用三元运算符返回1或0构建或不构建资源时,将分配此标志以对资源计数。我建议您将模块拆分为一个单独的模块。

谢谢,巴德,现在只需简单地拆分即可。工作:)