infra_code

infrastructure-as-code
git clone git://git.pyratebeard.net/infra_code.git
Log | Files | Refs | README

commit 4a00e360b9f7e1a2153f6e91868c425638bbb271
parent 25a38d98be0580f20a3e44b6e8628f240e666b3f
Author: pyratebeard <root@pyratebeard.net>
Date:   Thu,  2 May 2024 23:33:24 +0100

libvirt debian12 vm creation

Diffstat:
Alibvirt/debian12/cloud_init.cfg | 14++++++++++++++
Alibvirt/debian12/infra.tf | 43+++++++++++++++++++++++++++++++++++++++++++
Alibvirt/debian12/output.tf | 3+++
Alibvirt/debian12/provider.tf | 12++++++++++++
Alibvirt/debian12/variables.tf | 31+++++++++++++++++++++++++++++++
5 files changed, 103 insertions(+), 0 deletions(-)

diff --git a/libvirt/debian12/cloud_init.cfg b/libvirt/debian12/cloud_init.cfg @@ -0,0 +1,14 @@ +#cloud-config +ssh_pwauth: false + +preserve_hostname: false +fqdn: ${hostname}.tilde.gdn +hostname: ${hostname} + +users: + - name: dwarf + ssh_authorized_keys: + - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICSluiY54h5FlGxnnXqifWPnfvKNIh1/f0xf0yCThdqV openpgp:0x65A514D0 + sudo: ['ALL=(ALL) NOPASSWD:ALL'] + shell: /bin/bash + groups: wheel diff --git a/libvirt/debian12/infra.tf b/libvirt/debian12/infra.tf @@ -0,0 +1,43 @@ +resource "libvirt_volume" "debian12-qcow2" { + name = "${var.vm_name}.qcow2" + pool = "labfs" + source = "http://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2" + format = "qcow2" + #size = var.vm_size +} + +data "template_file" "user_data" { + template = "${file("${path.module}/cloud_init.cfg")}" + vars = { + hostname = var.vm_name + } +} + +resource "libvirt_cloudinit_disk" "commoninit" { + name = "commoninit.iso" + pool = "labfs" + user_data = "${data.template_file.user_data.rendered}" +} + +resource "libvirt_domain" "debian12" { + name = var.vm_name + memory = var.vm_mem + vcpu = var.vm_vcpus + + network_interface { + network_name = "default" + wait_for_lease = true + } + + disk { + volume_id = "${libvirt_volume.debian12-qcow2.id}" + } + + cloudinit = "${libvirt_cloudinit_disk.commoninit.id}" + + console { + type = "pty" + target_type = "serial" + target_port = "0" + } +} diff --git a/libvirt/debian12/output.tf b/libvirt/debian12/output.tf @@ -0,0 +1,3 @@ +output "ip" { + value = "${libvirt_domain.debian12.network_interface.0.addresses.0}" +} diff --git a/libvirt/debian12/provider.tf b/libvirt/debian12/provider.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + libvirt = { + source = "dmacvicar/libvirt" + version = "0.7.1" + } + } +} + +provider "libvirt" { + uri = "qemu:///system" +} diff --git a/libvirt/debian12/variables.tf b/libvirt/debian12/variables.tf @@ -0,0 +1,31 @@ +# vars for homelab + +variable "vm_name" { + type = string + description = "hostname" + default = "testvm" +} + +variable "vm_vcpus" { + type = string + description = "number of vcpus" + default = 2 +} + +variable "vm_mem" { + type = string + description = "amount of memory" + default = "2048" +} + +variable "vm_size" { + type = string + description = "capacity of disk" + default = "20480" +} + +#variable "instance_count" { +# type = string +# description = "Number of Vault nodes to create" +# default = "1" +#}