Я не могу спать из-за этого, поэтому решил попробовать здесь. Я размещаю 4 гостя KVM на том же хосте, что и мой проект terraform. Я следую за этими двумя руководствами https://titosoft.github.io/kvm/terraform-and-kvm/ https://blog.gruntwork.io/terraform-tips-tricks- loops-if-reports-and-gotchas-f739bbae55f9 но я все еще как-то растерялся.
Все, чего я хочу добиться, - это создать n-количество гостевых машин с именами, которые я предварительно определил в списке e. грамм. master01, worker01, worker02, worker03. Мой пул называется guest_images. Файлы qcow2, гости KVM и имена хостов должны быть названы в соответствии со списком.
ОБНОВЛЕНИЕ Удалось решить большинство моих проблем. Если кто-то борется с тем же, вот код. Последний нерешенный вопрос: все имена хостов в настоящее время обозначают «ubuntu». Это должно быть исправлено через user_data, но это не работает: user_data = "$ {data.template_file.user_data.rendered [count.index]}"
Кто-нибудь знает, как это исправить?
provider "libvirt" {
uri = "qemu:///system"
}
variable "vm_machines" {
description = "Create machines with these names"
type = list(string)
default = ["master01", "worker01", "worker02", "worker03"]
}
# We fetch the latest ubuntu release image from their mirrors
resource "libvirt_volume" "ubuntu" {
name = "${var.vm_machines[count.index]}.qcow2"
count = length(var.vm_machines)
pool = "guest_images"
source = "http://cloud-images.ubuntu.com/releases/bionic/release-20191008/ubuntu-18.04-server-cloudimg-amd64.img"
format = "qcow2"
}
# Create a network for our VMs
resource "libvirt_network" "vm_network" {
name = "vm_network"
addresses = ["10.224.1.0/24"]
dhcp {
enabled = true
}
}
# Use CloudInit to add our ssh-key to the instance
resource "libvirt_cloudinit_disk" "commoninit" {
name = "commoninit.iso"
pool = "guest_images"
user_data = "${data.template_file.user_data.rendered}"
network_config = "${data.template_file.network_config.rendered}"
}
data "template_file" "user_data" {
template = "${file("${path.module}/cloud_init.cfg")}"
}
data "template_file" "network_config" {
template = "${file("${path.module}/network_config.cfg")}"
}
# Create the machine
resource "libvirt_domain" "ubuntu" {
count = length(var.vm_machines)
name = var.vm_machines[count.index]
memory = "8196"
vcpu = 2
cloudinit = "${libvirt_cloudinit_disk.commoninit.id}"
network_interface {
network_id = "${libvirt_network.vm_network.id}"
network_name = "vm_network"
}
# 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"
}
console {
type = "pty"
target_type = "virtio"
target_port = "1"
}
disk {
volume_id = libvirt_volume.ubuntu[count.index].id
}
graphics {
type = "vnc"
listen_type = "address"
autoport = "true"
}
}