这是我得到的错误:
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
nonprod-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"
}
nonprod-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 Modules分成2个文件夹。
我应该怎么做?
任何帮助将不胜感激:)
答案 0 :(得分:0)
您的模块无条件调用linux和Windows资源。通常,会传递一个标志或某种方法来确定您是否要使用Windows vm或linux vm。将使用三元运算符返回1或0来构建或不构建资源,以分配此标志以依靠资源。我建议您将模块分成一个单独的模块。