summaryrefslogtreecommitdiff
path: root/libvirt.tf
diff options
context:
space:
mode:
Diffstat (limited to 'libvirt.tf')
-rw-r--r--libvirt.tf79
1 files changed, 79 insertions, 0 deletions
diff --git a/libvirt.tf b/libvirt.tf
new file mode 100644
index 0000000..64bf161
--- /dev/null
+++ b/libvirt.tf
@@ -0,0 +1,79 @@
+# Defining VM Volume
+resource "libvirt_volume" "base" {
+ name = "${var.nom}-base"
+ pool = "default"
+ source = var.image
+ format = "qcow2"
+}
+
+resource "libvirt_volume" "image" {
+ name = "${var.nom}.qcow2"
+ base_volume_id = libvirt_volume.base.id
+ pool = "default"
+ size = 10 * 1024 * 1024 * 1024
+}
+
+# get user data info
+data "template_file" "user_data" {
+ template = file("${path.module}/cloud_init.cfg")
+ vars = {
+ nom = var.nom
+ package = var.package
+ }
+}
+
+# Use CloudInit to add the instance
+resource "libvirt_cloudinit_disk" "commoninit" {
+ name = "commoninit.iso"
+ pool = "default" # List storage pools using virsh pool-list
+ user_data = data.template_file.user_data.rendered
+}
+
+# Define KVM domain to create
+resource "libvirt_domain" "vm_domain" {
+ name = var.nom
+ memory = "2048"
+ vcpu = 2
+ qemu_agent = true
+
+
+ cpu {
+ mode = "host-passthrough"
+ }
+
+ network_interface {
+ network_name = "default"
+ #bridge = "bridge0"
+ wait_for_lease = true
+ }
+
+ disk {
+ volume_id = libvirt_volume.image.id
+ }
+
+ cloudinit = libvirt_cloudinit_disk.commoninit.id
+
+ console {
+ type = "pty"
+ target_type = "serial"
+ target_port = "0"
+ }
+
+ graphics {
+ type = "spice"
+ listen_type = "address"
+ autoport = true
+ }
+
+ provisioner "local-exec" {
+ command = "./script_local ${self.network_interface.0.addresses[0]}"
+ }
+}
+
+# Output Server IP
+# Nécessite qemu-guest-agent sinon bloque la fin de l'installation !!!
+output "ip" {
+ value = libvirt_domain.vm_domain.network_interface.0.addresses
+}
+
+