0

I'm trying to use the libvirt kvm terraform provider (https://github.com/dmacvicar/terraform-provider-libvirt) to connect an example virtual machine to an existing network bridge I've created. I'm using the simple example from here; https://github.com/fabianlee/terraform-libvirt-ubuntu-examples.git . Here's the tf, the bridge details are below that; any suggestions would be gratefully received.

# variables that can be overriden
variable "hostname" { default = "simple" }
variable "domain" { default = "example.com" }
variable "memoryMB" { default = 1024*1 }
variable "cpu" { default = 1 }

# instance the provider
provider "libvirt" {
  uri = "qemu:///system"
}

# fetch the latest ubuntu release image from their mirrors
resource "libvirt_volume" "os_image" {
  name = "${var.hostname}-os_image"
  pool = "default"
  source = "https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img"
  format = "qcow2"
}

# Use CloudInit ISO to add ssh-key to the instance
resource "libvirt_cloudinit_disk" "commoninit" {
          name = "${var.hostname}-commoninit.iso"
          pool = "default"
          user_data = data.template_file.user_data.rendered
          network_config = data.template_file.network_config.rendered
}

resource "libvirt_network" "vm_network" {
  name = "test_net"
  mode = "bridge"
  bridge = "br0"
}


data "template_file" "user_data" {
  template = file("${path.module}/cloud_init.cfg")
  vars = {
    hostname = var.hostname
    fqdn = "${var.hostname}.${var.domain}"
  }
}

data "template_file" "network_config" {
  template = file("${path.module}/network_config_dhcp.cfg")
}


# Create the machine
resource "libvirt_domain" "domain-ubuntu" {
  name = var.hostname
  memory = var.memoryMB
  vcpu = var.cpu

  disk {
       volume_id = libvirt_volume.os_image.id
  }
  network_interface {
       network_name = "default"
  }

  cloudinit = libvirt_cloudinit_disk.commoninit.id

  # IMPORTANT
  # Ubuntu can hang is a isa-serial is not present at boot time.
  # If you find your CPU 100% and never is available this is why
  console {
    type        = "pty"
    target_port = "0"
    target_type = "serial"
  }

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

terraform { 
  required_version = ">= 0.12"
}

output "ips" {
  # show IP, run 'terraform refresh' if not populated
  value = libvirt_domain.domain-ubuntu.*.network_interface.0.addresses
}

Bridge br0:

5: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:1d:7d:0d:2a:9d brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.250/24 brd 192.168.1.255 scope global br0
       valid_lft forever preferred_lft forever
    inet6 fde6:4511:f54:0:21d:7dff:fe0d:2a9d/64 scope global dynamic mngtmpaddr 
       valid_lft forever preferred_lft forever
    inet6 fe80::21d:7dff:fe0d:2a9d/64 scope link 
       valid_lft forever preferred_lft forever
2
  • 1
    What is the problem you are having? Aug 21, 2020 at 14:30
  • I can't get it to connect the vm to the bridge I've created. I just want to be able to access the machine from anywhere on the internal network, and have no nat issues due to kvm creating a network.
    – James
    Aug 21, 2020 at 20:40

1 Answer 1

0

in the resource "libvirt_domain" "domain-ubuntu" you should try to configure:

network_interface {
    network_name = libvirt_network.vm_network.name
}

instead of:

network_interface {
    network_name = "default"
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .