dmacvicar / terraform-provider-libvirt

Terraform provider to provision infrastructure with Linux's KVM using libvirt
Apache License 2.0
1.59k stars 458 forks source link

'Error: Invalid index: The given key does not identify an element in this collection value.' when running provisioner #806

Closed ghost closed 3 years ago

ghost commented 3 years ago

System Information

Linux distribution

Fedora 33

Terraform version

+ provider registry.terraform.io/dmacvicar/libvirt v0.6.2
+ provider registry.terraform.io/hashicorp/template v2.2.0

Provider and libvirt versions


Using library: libvirt 6.6.0
Running hypervisor: QEMU 5.1.0
Running against daemon: 6.6.0
___

## Description of Issue/Question

I am trying to run a provisioner, but it acts like it doesn't get the network address quick enough. There is a race condition between when the provisioner runs, and when it actally gets the machine's IP

### Setup

terraform {
 required_version = ">= 0.13"
  required_providers {
    libvirt = {
      source  = "dmacvicar/libvirt"
      version = "0.6.2"
    }
  }
}

provider "libvirt" {
  uri = "qemu:///system"
}

resource "libvirt_volume" "rhel8-qcow2" {
  name   = "rhel8-qcow2"
  pool   = "default"
  source = "./images/rhel-8.3-x86_64-kvm.qcow2"
  format = "qcow2"
}

data "template_file" "user_data" {
  template = file("${path.module}/cloud_init.yml")
}

resource "libvirt_cloudinit_disk" "commoninit" {
  name           = "commoninit.iso"
  user_data      = data.template_file.user_data.rendered
  pool           = "default"
}

resource "libvirt_domain" "domain-rhel8" {
  name   = "rhel8-terraform"
  memory = "512"
  vcpu   = 1

  cloudinit = libvirt_cloudinit_disk.commoninit.id

  network_interface {
    network_name = "default"
  }

  console {
    type        = "pty"
    target_port = "0"
    target_type = "serial"
  }

  console {
    type        = "pty"
    target_type = "virtio"
    target_port = "1"
  }

  disk {
    volume_id = libvirt_volume.rhel8-qcow2.id
  }

  graphics {
    type        = "spice"
    listen_type = "address"
    autoport    = true
  }

  provisioner "remote-exec" {
    inline = [
      "dnf update",
    ]

    connection {
      host     = self.network_interface.0.addresses.0
      type     = "ssh"
      user     = "root"
    }
  }
}

### Steps to Reproduce Issue

I've tried installing the guest agent to see if that helps to no avail.

___
MalloZup commented 3 years ago

hi @gwfisher thx for issue! can you format better and provide more output on the issue?

E.g would be helpfull to have the console output. tia! :tulip:

We also released a 0.6.3 version of provider, seeing you are using the 0.6.2

ghost commented 3 years ago

So I compiled this straight from github.

This is what it says when I try and provision the virtual machine with terraform.

libvirt_cloudinit_disk.commoninit: Creating... libvirt_volume.rhel8-qcow2: Creating... libvirt_volume.rhel8-qcow2: Creation complete after 2s [id=/var/lib/libvirt/images/rhel8-qcow2] libvirt_cloudinit_disk.commoninit: Creation complete after 3s [id=/var/lib/libvirt/images/commoninit.iso;aa844a0a-5cd0-43a9-8801-c5d9b589dc61] libvirt_domain.domain-rhel8: Creating...

Error: Invalid index: The given key does not identify an element in this collection value.

ghost commented 3 years ago

I found a fix for it from another issue. you have to add

network_interface { network_name = "default" wait_for_lease = true }

to make sure the virtual machine gets an IP address before running a provisioner.