terraform/main.tf
2024-12-05 09:12:47 +07:00

93 lines
2.0 KiB
HCL

data "template_file" "user_data" {
count = length(var.domains)
template = file("${path.module}/cloud_init.yml")
vars = {
login = "admin"
<<<<<<< HEAD
=======
passwd = "passwd"
>>>>>>> 51d0c3c (Init Project)
ip = var.domains[count.index].ip
}
}
data "template_file" "network_config" {
count = length(var.domains)
template = file("${path.module}/network_config.yml")
vars = {
ip = var.domains[count.index].ip
}
}
resource "libvirt_pool" "pool" {
name = "${var.prefix}pool"
type = "dir"
target {
path = "${var.pool_path}/${var.prefix}pool"
}
}
resource "libvirt_volume" "image" {
name = var.image.name
format = "qcow2"
pool = libvirt_pool.pool.name
source = var.image.url
}
resource "libvirt_volume" "root" {
count = length(var.domains)
name = "${var.prefix}${var.domains[count.index].name}-root.qcow2"
pool = libvirt_pool.pool.name
base_volume_id = libvirt_volume.image.id
size = var.domains[count.index].disk
format = "qcow2"
}
# resource "libvirt_network" "br0" {
# bridge = "br0"
# name = "br0"
# mode = "bridge"
# autostart = true
# }
resource "libvirt_cloudinit_disk" "commoninit" {
count = length(var.domains)
name = "commoninit-${var.domains[count.index].name}.iso"
pool = libvirt_pool.pool.name
user_data = data.template_file.user_data[count.index].rendered
network_config = data.template_file.network_config[count.index].rendered
<<<<<<< HEAD
=======
>>>>>>> 51d0c3c (Init Project)
}
resource "libvirt_domain" "vm" {
count = length(var.domains)
name = var.domains[count.index].name
memory = var.domains[count.index].ram
vcpu = var.domains[count.index].cpu
machine = "q35"
arch = "x86_64"
xml {
xslt = file("/home/filimonov/t1/cdrom-model.xsl")
}
cloudinit = libvirt_cloudinit_disk.commoninit[count.index].id
cpu {
mode = "custom"
}
network_interface {
network_name = "br0"
}
disk {
volume_id = libvirt_volume.root[count.index].id
scsi = "true"
}
}