dmacvicar / terraform-provider-libvirt

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

terraform-provider-libvirt keeps changing network_interface #851

Open bjvrielink opened 3 years ago

bjvrielink commented 3 years ago

System Information

Linux distribution

Alpine Linux v3.13

Terraform version

Terraform v0.15.3
on linux_amd64
+ provider registry.terraform.io/dmacvicar/libvirt v0.6.3
+ provider registry.terraform.io/hashicorp/aws v3.27.0
+ provider registry.terraform.io/hashicorp/dns v3.1.0
+ provider registry.terraform.io/hashicorp/local v2.1.0

Provider and libvirt versions

./terraform-provider-libvirt 076b2da9551370b622307983059515b6ff83e37d
Compiled against library: libvirt 6.10.0
Using library: libvirt 6.10.0
2021/05/16 10:34:31 virError(Code=38, Domain=7, Message='Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory')

Checklist

Description of Issue/Question

Each and every time I run terraform plan or apply, terraform-provider-libvirt wants to change the network interfaces of the VM's.

Setup

resource "libvirt_network" "dmz" {
  name = "dmz"
  autostart = true
  mode = "bridge"
  bridge = "dmz"
}

resource "libvirt_domain" "this" {
  autostart = true
  name = var.host_name
  memory = var.mem_size
  vcpu = var.cpu_count
  cloudinit = libvirt_cloudinit_disk.this.id

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

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

  network_interface {
    network_id = libvirt_network.dmz.id
  }

  disk {
    volume_id = libvirt_volume.this.id
  }
}

Steps to Reproduce Issue

When terraform plan runs, it wants to change the network interface:

      ~ network_interface {
          - bridge         = "dmz" -> null
          + network_id     = "5e23fbb5-0243-432e-84b0-c5cb07d4b5a8"

This change does not impact the running VM. The risk is that when you have a changed resource each and every time you run terraform plan, there will be a day that you overlook another change (on the same resource) that has impact.


Additional information:

I run terraform as a docker container (hashicorp/terraform:latest) as part of a Gitlab CI pipeline. The docker host has SELinux, but not in a way that should affect this issue.

marshallford commented 2 years ago

@bjvrielink Did you ever find a workaround for this?

bjvrielink commented 2 years ago

I worked around this by changing how the domain connects to the network. What I had was:

resource "libvirt_domain" "this" {
   network_interface {
     network_id = var.network_id
   }
}

I refactored my code into:

resource "libvirt_domain" "this" {
   network_interface {
     bridge = var.network
   }
}