Sample Header Ad - 728x90

assign IP to kvm guest on bridge (terrafom)

1 vote
1 answer
587 views
I am trying to deploy VM guest via terraform on KVM. the guest VM is deployed but does not get an IP. what am I doing wrong? here is my main.tf :
terraform {
  required_providers {
    libvirt  = {
      source = "dmacvicar/libvirt"
    }
  }
}

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

resource "libvirt_volume" "centos7-qcow2" {
    name   = "centos7.qcow2"
    pool   = "default"
    source = "http://......qcow2 "
    format = "qcow2"

}

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

data "template_file" "cloudinit_network" {
  template = "${file("${path.module}/network.cfg")}"
  vars = {}
}

resource "libvirt_cloudinit_disk" "commoninit" {
  name         = "commoninit.iso"
  user_data    = "${data.template_file.user_data.rendered}"
  network_config = data.template_file.cloudinit_network.rendered
}


resource "libvirt_domain" "gw2" {
  name   = "gw2"
  memory = "8192"
  vcpu   = 4
  cloudinit = libvirt_cloudinit_disk.commoninit.id
  qemu_agent = true
  
  network_interface {
    addresses = ["10.100.86.201"]
    bridge = "br0"
    #wait_for_lease = true
  }

  boot_device {
   dev = [ "hd", "network"]
  }

  disk {
    volume_id = "${libvirt_volume.centos7-qcow2.id}"
  }

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

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

output "ips" {
  value = libvirt_domain.gw2.*.network_interface.0.addresses
}
my network.cfg file:
version: 2
ethernets:
  eth0:
    dhcp4: true
    dhcp6: false
Thank you for the help! ___________________ my terraform version: Terraform v1.0.10 on linux_amd64 + provider registry.terraform.io/dmacvicar/libvirt v0.6.11 + provider registry.terraform.io/hashicorp/template v2.2.0
Asked by user536811 (11 rep)
Aug 8, 2022, 02:22 PM
Last activity: Mar 2, 2023, 03:56 PM