0

I'm trying to create stack of multiple vms on KVM_HOST with terraform. The catch is, I'm using pxeboot & kickstart to do installation. Kickstart file needs to carry some dynamic information, like static IPs, hostname etc, therefore I have to create it for each vm, and this kickstart file needs to stay there till vm reads it & starts installation. Then next kickstart file is created for vm 2, and it needs to stay there till vm2 reads it for installation. So there should be some delay in creating these resources, either with loop or depend_on parameter, but what I'm getting is, kickstart files for all vms are generated right away, last one is overriding, of course, so correct info is not passed.

here's two resources which I need to loop in sequence for number of variable value qty

variable "vm_count" {
  type    = number
  default = 1
}

below are resources

resource "null_resource" "delay" {
  count = var.vm_count

  provisioner "local-exec" {
    command = "sleep 150"
  }
  depends_on = [libvirt_domain.vm]
}

resource "local_file" "kickstart_file" {
  count = var.vm_count
  content = templatefile("ks.tpl", {
    ip                = "192.168.1.${117 + count.index}"
    netmask           = var.netmask
    gateway           = var.gateway
    dns               = var.dns
    hostname          = "${var.hostname}-${count.index + 1}"
    disk_size_rounded = ((var.disk_size - 1) * 1024) -1
  })
  filename = "/var/www/html/kickstart/ks-terraform.cfg"
}

resource "libvirt_domain" "vm" {
  count  = var.vm_count
  name   = "${var.hostname}-${count.index + 1}"
  memory = var.memory
  vcpu   = var.cpu
  arch   = "x86_64"

  cpu {
    mode = "host-passthrough"
  }

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

  disk {
    volume_id = libvirt_volume.default_disk[count.index].id
  }

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

  network_interface {
    bridge = "virbr0"
    macvtap  = false
  }

  depends_on = [local_file.kickstart_file, null_resource.delay]
}

how can I improve the logic with depends on or for_each?

4
  • I had a similar setup with AWS virtual machines, but I used tags to pass info into the newly launched VM (and for the VM to pass status info back out). AWS tags are key/value metadata that are associated with a VM; the processes running in the VM can read and write the tags, and things outside the VM (other VMs and the virtualization system itself) can read the tags. Does your KVM system have something similar that you can use instead of files that are external to the VMs?
    – Sotto Voce
    Commented Feb 11 at 14:38
  • @SottoVoce I really don't know.
    – Sollosa
    Commented Feb 11 at 15:37
  • 1
    Why not just change the name of the kickstart file to include the hostname in it, so if you're building 3 VMs then you generate 3 kickstart files and they can all exist at the same time. Commented Feb 11 at 15:52
  • @StephenHarris because of pxeboot env, the path to kickstart file is hardcoded into pxeboot menu, if you know what I mean.
    – Sollosa
    Commented Feb 11 at 16:36

1 Answer 1

0

Ok, what I found is amazing, pxeboot allows pxemenu to be vm-specific with use of mac address, this way I create unique pxemenu file for each vm, which can reference to separate kickstart file for each vm. good thing is it doesn't need a service restart.

just create a menu file for each vm at path

/var/lib/tftp/pxelinux.cfg/01-(followed by mac address of the vm, separated by dashes not colons

like this

/var/lib/tftpboot/pxelinux.cfg/01-52-54-dd-dd-dd-dd

and it's menu file contains reference to kickstart file, like this

default menu.c32
prompt 0
timeout 300
ontimeout 2

menu title PXE Boot Menu
label 1
  menu label ^1 - Install Rocky Linux 9
  kernel images/vmlinuz
  append initrd=images/initrd.img showopts inst.repo=http://{ PXEBOOT_SERVER }/os-dvd/ devfs=nomount inst.ks=http://{ PXEBOOT_SERVER }/kickstart/os-minimal-vm1.cfg

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.