配置 VM 失败。 Linux/Windows 资源块合并
Failure provisioning VM. Linux/Windows resource blocks merging
这是我遇到的错误:
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" {
我的 terraform 相关文件夹结构是:
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"
}
模块-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 计划:
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 模块分成 2 个文件夹。
我该怎么办?
如有任何帮助,我们将不胜感激:)
您的模块在没有条件的情况下同时调用了 linux 和 windows 资源。通常会传递一个标志或某种方法来确定您是想要 windows 虚拟机还是 linux 虚拟机。此标志将被分配以使用三元运算符 return 1 或 0 来依赖资源来构建它或不构建它。我建议您将该模块分解为一个单独的模块。
这是我遇到的错误:
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" {
我的 terraform 相关文件夹结构是:
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"
}
模块-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 计划:
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 模块分成 2 个文件夹。
我该怎么办?
如有任何帮助,我们将不胜感激:)
您的模块在没有条件的情况下同时调用了 linux 和 windows 资源。通常会传递一个标志或某种方法来确定您是想要 windows 虚拟机还是 linux 虚拟机。此标志将被分配以使用三元运算符 return 1 或 0 来依赖资源来构建它或不构建它。我建议您将该模块分解为一个单独的模块。