diff --git a/multiple/shared_modules/cloud-init.tf b/multiple/shared_modules/cloud-init.tf new file mode 100644 index 0000000..f4c6097 --- /dev/null +++ b/multiple/shared_modules/cloud-init.tf @@ -0,0 +1,6 @@ +resource "libvirt_cloudinit_disk" "commoninit" { + name = var.cloudinit_filename + user_data = templatefile("${path.module}/../environments/cloud_init.yaml", {}) + pool = var.pool_name + depends_on = [libvirt_pool.tf_tmp_storage] +} \ No newline at end of file diff --git a/multiple/shared_modules/domain.tf b/multiple/shared_modules/domain.tf new file mode 100644 index 0000000..cd1da04 --- /dev/null +++ b/multiple/shared_modules/domain.tf @@ -0,0 +1,33 @@ +resource "libvirt_domain" "domain" { + count = var.instance_count + name = "${var.vm_name}-${count.index}" + memory = var.memory + vcpu = var.vcpu + cloudinit = libvirt_cloudinit_disk.commoninit.id + cpu { + mode = "host-model" + } + + disk { + volume_id = element(libvirt_volume.vm_disk.*.id, count.index) + } + + console { + type = "pty" + target_port = "0" + target_type = "virtio" + } + + video { + type = "virtio" + } + + tpm { + backend_type = "emulator" + backend_version = "2.0" + } + + network_interface { + network_name = var.network_name + } +} \ No newline at end of file diff --git a/multiple/shared_modules/network.tf b/multiple/shared_modules/network.tf new file mode 100644 index 0000000..161edc0 --- /dev/null +++ b/multiple/shared_modules/network.tf @@ -0,0 +1,11 @@ +resource "libvirt_network" "tf_libvirt_tmp_network" { + name = var.network_name + mode = var.network_mode + domain = var.network_domain + addresses = var.network_addresses + + dns { + enabled = var.dns_enabled + local_only = var.dns_local_only + } +} \ No newline at end of file diff --git a/multiple/shared_modules/outputs.tf b/multiple/shared_modules/outputs.tf new file mode 100644 index 0000000..877c03b --- /dev/null +++ b/multiple/shared_modules/outputs.tf @@ -0,0 +1,19 @@ +output "pool_name" { + value = libvirt_pool.tf_tmp_storage.name +} + +output "network_name" { + value = libvirt_network.tf_libvirt_tmp_network.name +} + +output "network_addresses" { + value = libvirt_network.tf_libvirt_tmp_network.addresses +} + +output "cloudinit_disk_id" { + value = libvirt_cloudinit_disk.commoninit.id +} + +output "cloudinit_disk_name" { + value = libvirt_cloudinit_disk.commoninit.name +} \ No newline at end of file diff --git a/multiple/shared_modules/pool.tf b/multiple/shared_modules/pool.tf new file mode 100644 index 0000000..a5fa1b8 --- /dev/null +++ b/multiple/shared_modules/pool.tf @@ -0,0 +1,7 @@ +resource "libvirt_pool" "tf_tmp_storage" { + name = var.pool_name + type = "dir" + target { + path = var.pool_path + } +} \ No newline at end of file diff --git a/multiple/shared_modules/provider.tf b/multiple/shared_modules/provider.tf new file mode 100644 index 0000000..06942fe --- /dev/null +++ b/multiple/shared_modules/provider.tf @@ -0,0 +1,9 @@ +terraform { + required_version = ">= 0.13" + required_providers { + libvirt = { + source = "dmacvicar/libvirt" + version = "0.8.3" + } + } +} \ No newline at end of file diff --git a/multiple/shared_modules/variables.tf b/multiple/shared_modules/variables.tf new file mode 100644 index 0000000..3d723a7 --- /dev/null +++ b/multiple/shared_modules/variables.tf @@ -0,0 +1,113 @@ +variable "libvirt_uri" { + description = "URI for libvirt connection" + type = string + default = "qemu:///system" +} + +variable "pool_name" { + description = "Name of the storage pool" + type = string + default = "tf_tmp_pool" +} + +variable "pool_path" { + description = "Path for the storage pool" + type = string + default = "/tmp/tf_tmp_storage" +} + +variable "instance_count" { + description = "Number of instances to create" + type = number + default = 1 +} + +variable "vm_name" { + description = "Name prefix for VMs" + type = string +} + +variable "image_location" { + description = "Location of the OS image" + type = string + default = "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img" +} +# In order to avoid refetching the cloud ISO each time, it could could be set to a local directory, like : "/var/lib/libvirt/images/noble-server-cloudimg-amd64.img" + +# variable "cloudinit_user_data" { +# description = "User data for cloud-init" +# type = string +# default = <= 512 + error_message = "Memory must be at least 512MB." + } +} + +variable "vcpu" { + description = "Number of virtual CPUs" + type = number + default = 1 +} + +variable "network_name" { + description = "Name of the network" + type = string + default = "tf" +} + +variable "network_mode" { + description = "Network mode (nat, none, route, open, bridge)" + type = string + default = "nat" +} + +variable "network_domain" { + description = "Domain name for the network" + type = string + default = "tf.local" +} + +variable "network_addresses" { + description = "List of network addresses" + type = list(string) + default = ["10.17.3.0/24", "2001:db8:ca2:2::1/64"] +} + +variable "dns_enabled" { + description = "Enable DNS for the network" + type = bool + default = true +} + +variable "dns_local_only" { + description = "DNS requests only resolved by virtual network's DNS server" + type = bool + default = false +} \ No newline at end of file diff --git a/multiple/shared_modules/volume.tf b/multiple/shared_modules/volume.tf new file mode 100644 index 0000000..802a706 --- /dev/null +++ b/multiple/shared_modules/volume.tf @@ -0,0 +1,9 @@ +resource "libvirt_volume" "vm_disk" { + count = var.instance_count + name = "${var.vm_name}-${count.index}" + pool = var.pool_name + source = var.image_location + format = "qcow2" + + depends_on = [libvirt_pool.tf_tmp_storage] +} \ No newline at end of file