我正在使用Jenkins运行我的Terraform脚本。 它可以正常工作几天,但是在多次销毁并(重新)创建虚拟机之后,它不再起作用。在创建时,我得到:
* azurerm_virtual_machine.windows: Code="OSProvisioningInternalError" Message="OS Provisioning failed for VM 'e2r2s08ibld0001' due to an internal error."
删除也不起作用:
* azurerm_network_interface.windows: Error deleting Network Interface "e2r2s08ibld0001-nic" (Resource Group "Buildslaves_rg"): network.InterfacesClient#Delete: Failure sending request: StatusCode=400 -- Original Error: Code="NicInUse" Message="Network Interface /subscriptions/****/resourceGroups/Buildslaves_rg/providers/Microsoft.Network/networkInterfaces/e2r2s08ibld0001-nic is used by existing resource /subscriptions/****/resourceGroups/Buildslaves_rg/providers/Microsoft.Compute/virtualMachines/e2r2s08ibld0001. In order to delete the network interface, it must be dissociated from the resource. To learn more, see aka.ms/deletenic." Details=[]
好的,它说“ nic in use”,但我希望terraform以正确的顺序删除所有资源? 我可以使用Azure Portel成功删除我的资源。
但是,当我尝试通过terraform(重新)创建VM时,我再次遇到相同的错误。
我也尝试删除tfstate文件,但错误保持不变。
这是我的main.tf
的相关部分:
resource "azurerm_network_interface" "windows" {
name = "${var.vm_windows["name"]}-nic"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
# network_security_group_id = "${azurerm_network_security_group.main.id}"
tags = "${var.tags}"
enable_accelerated_networking = true
ip_configuration {
name = "ipconfig1"
subnet_id = "${data.azurerm_subnet.main.id}"
private_ip_address_allocation = "Dynamic"
# private_ip_address = "${var.vm_windows["private_ip_address"]}"
}
}
resource "azurerm_virtual_machine" "windows" {
name = "${var.vm_windows["name"]}"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
network_interface_ids = ["${azurerm_network_interface.windows.id}"]
vm_size = "${var.vm_windows["size"]}"
tags = "${var.tags}"
# This means the OS Disk will be deleted when Terraform destroys the Virtual Machine
# NOTE: This may not be optimal in all cases.
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
storage_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2019-Datacenter"
version = "latest"
}
storage_os_disk {
name = "${var.vm_windows["name"]}-os"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "${var.vm_windows["disk_os_type"]}"
}
storage_data_disk {
name = "${var.vm_windows["name"]}-data"
caching = "ReadOnly"
create_option = "Empty"
disk_size_gb = "${var.vm_windows["disk_data_size"]}"
lun = 0
managed_disk_type = "${var.vm_windows["disk_data_type"]}"
}
os_profile {
computer_name = "${var.vm_windows["name"]}"
admin_username = "${var.vm_admin_username}"
admin_password = "${var.vm_admin_password}"
# custom_data = "${local.custom_data_content}"
}
os_profile_windows_config {
provision_vm_agent = true
enable_automatic_upgrades = true
# Auto-Login's required to configure WinRM
additional_unattend_config {
pass = "oobeSystem"
component = "Microsoft-Windows-Shell-Setup"
setting_name = "AutoLogon"
content = "<AutoLogon><Password><Value>${var.vm_admin_password}</Value></Password><Enabled>true</Enabled><LogonCount>1</LogonCount><Username>${var.vm_admin_username}</Username></AutoLogon>"
}
# See https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples/virtual-machines/provisioners/windows
additional_unattend_config {
pass = "oobeSystem"
component = "Microsoft-Windows-Shell-Setup"
setting_name = "FirstLogonCommands"
content = "${file("./files/FirstLogonCommands.xml")}"
}
}
}
答案 0 :(得分:0)
我刚刚发现了自己的错误:
在我的FirstLogonCommands.xml
中,我有两个条目具有相同的Order
号(复制和粘贴问题)。
修复此问题后,它可以正常工作!