确定虚拟机的IP地址时出错;获取网卡时出错;发送失败请求

时间:2019-02-20 16:54:20

标签: azure terraform terraform-provider-azure

我正在尝试使用terraform在群集中创建Windows vm,并且在创建时遇到错误

错误

Error: Error applying plan:

1 error(s) occurred:

* module.vnet.azurerm_virtual_machine.vm[0]: 1 error(s) occurred:

* azurerm_virtual_machine.vm.0: Error determining IP Address for Virtual Machine "vm0" Error obtaining NIC "nic0" 
     

network.InterfacesClient#Get:发送请求失败:StatusCode = 0-   原始错误:获取        wsarecv:连接尝试失败,因为经过一段时间或建立的连接方未正确响应   连接失败,因为连接的主机无法响应。

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

我正在尝试的地形模块 为了简单起见,我没有在以下代码中添加vnet,资源组等内容

resource "azurerm_lb_nat_rule" "tcp" {
  resource_group_name            = "${azurerm_resource_group.main.name}"
  loadbalancer_id                = "${azurerm_lb.main.id}"
  name                           = "RDP-VM-${count.index}"
  protocol                       = "tcp"
  frontend_port                  = "5000${count.index + 1}"
  backend_port                   = 3389
  frontend_ip_configuration_name = "pip-${var.location_id}-${var.environment}-${var.cost_centre}-${var.project}-${var.seq_id}"
  count                          = "${var.vm_count}"
}

resource "azurerm_lb_rule" "lb_rule" {
  resource_group_name            = "${azurerm_resource_group.main.name}"
  loadbalancer_id                = "${azurerm_lb.main.id}"
  name                           = "lbrule-${var.location_id}-${var.environment}-${var.cost_centre}-${var.project}-${var.seq_id}"
  protocol                       = "tcp"
  frontend_port                  = 80
  backend_port                   = 80
  frontend_ip_configuration_name = "pip-${var.location_id}-${var.environment}-${var.cost_centre}-${var.project}-${var.seq_id}"
  enable_floating_ip             = false
  backend_address_pool_id        = "${azurerm_lb_backend_address_pool.main.id}"
  idle_timeout_in_minutes        = 5
  probe_id                       = "${azurerm_lb_probe.main.id}"
  depends_on                     = ["azurerm_lb_probe.main"]
}

resource "azurerm_network_interface" "main" {
  name                = "nic${count.index}"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"
  count               = "${var.vm_count}"

  ip_configuration {
    name                                    = "ipconfig${count.index}"
    subnet_id                               = "${azurerm_subnet.main.id}"
    private_ip_address_allocation           = "Dynamic"
  }
}

resource "azurerm_network_interface_nat_rule_association" "main" {
  ip_configuration_name     = "ipconfig${count.index}"
  network_interface_id      ="${element(azurerm_network_interface.main.*.id, count.index)}"
  nat_rule_id               = "${element(azurerm_lb_nat_rule.tcp.*.id, count.index)}"
  count                     = "${var.vm_count}"
  depends_on = ["azurerm_network_interface.main","azurerm_lb_nat_rule.tcp"]
}

resource "azurerm_network_interface_backend_address_pool_association" "main" {
  network_interface_id      ="${element(azurerm_network_interface.main.*.id, count.index)}"
  ip_configuration_name     = "ipconfig${count.index}"
  backend_address_pool_id = "${azurerm_lb_backend_address_pool.main.id}"
  count                   = "${var.vm_count}"
  depends_on = ["azurerm_network_interface.main","azurerm_lb_backend_address_pool.main"]
}

resource "azurerm_virtual_machine" "vm" {
  name                  = "vm${count.index}"
  location              = "${azurerm_resource_group.main.location}"
  resource_group_name   = "${azurerm_resource_group.main.name}"
  availability_set_id   = "${azurerm_availability_set.main.id}"
  vm_size               = "${var.vm_size}"
  network_interface_ids = ["${element(azurerm_network_interface.main.*.id, count.index)}"]
  count                 = "${var.vm_count}"

  storage_image_reference {
    publisher = "${var.image_publisher}"
    offer     = "${var.image_offer}"
    sku       = "${var.image_sku}"
    version   = "${var.image_version}"
  }

  storage_os_disk {
    name          = "osdisk${count.index}"
    create_option = "FromImage"
  }

  os_profile {
    computer_name  = "${var.vm_name}-${count.index}"
    admin_username = "${var.admin_username}"
    admin_password = "${var.admin_password}"
  }

  os_profile_windows_config {}
}

我正在使用以上代码作为模块

module kofax-vnet{
//some variable values
    location_id="euwest"
    location="westeurope"
    vnet_cidr_list=["10.125.0.0/16"]
    subnet_cidr="10.125.0.0/20"
    vm_count= "2"
    vm_size="Standard_B4ms"
    image_publisher="MicrosoftWindowsServer"
    image_offer="WindowsServer"
    image_sku="2016-Datacenter"
    image_version="latest"
    vm_name="myvm"
    admin_username="someuser"
    admin_password="somepassword"
}

1 个答案:

答案 0 :(得分:0)

我通过在 azurerm_virtual_machine

中添加 depends_on 解决了此问题
resource "azurerm_virtual_machine" "vm" {
//some code
      depends_on = ["azurerm_network_interface.main"]
}