Skip to content

Commit

Permalink
MAJOR OVERHAUL
Browse files Browse the repository at this point in the history
  • Loading branch information
x86-39 committed Apr 10, 2024
1 parent eb700af commit b6ea5d0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 42 deletions.
29 changes: 17 additions & 12 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
resource "libvirt_volume" "cloudinit_image" {
count = var.cloudinit_image != "" ? 1 : 0
name = "${var.hostname}_cloudinit_image"
name = "${var.name}_cloudinit_image"
pool = var.libvirt_pool
source = var.cloudinit_image
format = "qcow2"
}

resource "libvirt_volume" "disk" {
name = "${var.hostname}_disk"
name = "${var.name}_disk"
pool = var.libvirt_pool
base_volume_id = try(libvirt_volume.cloudinit_image[0].id, "")
size = var.disk_size
Expand All @@ -21,8 +21,12 @@ data "template_file" "cloudinit_user_data" {
# From diademiemi/terraform-libvirt-vm
hostname: ${var.hostname}
fqdn: ${var.hostname}.${var.domain}
hostname: ${var.name}
%{if var.domain != null && var.domain != ""~}
fqdn: ${var.name}.${var.domain}
%{else~}
fqdn: ${var.name}
%{endif~}
prefer_fqdn_over_hostname: true
ssh_pwauth: ${var.password_auth}
Expand Down Expand Up @@ -109,15 +113,15 @@ EOT
}

resource "libvirt_cloudinit_disk" "init_disk" {
name = "${var.hostname}_cloudinit"
name = "${var.name}_cloudinit"
pool = var.libvirt_pool

user_data = data.template_file.cloudinit_user_data.rendered
network_config = data.template_file.cloudinit_network_data.rendered
}

resource "libvirt_domain" "domain" {
name = var.hostname
name = var.domain != null && var.domain != "" ? "${var.name}.${var.domain}" : var.name
memory = var.memory
vcpu = var.vcpu

Expand Down Expand Up @@ -183,18 +187,19 @@ resource "libvirt_domain" "domain" {

graphics {
type = "spice"
listen_type = var.spice_server_enabled ? "address" : "none"
listen_type = var.spice_enabled ? "address" : "none"
}

}

resource "ansible_host" "default" {
name = coalesce(var.ansible_name, var.hostname)
groups = concat(var.ansible_groups, [lower(replace(var.domain, ".", "_"))])
name = coalesce(var.ansible_name, var.name)
groups = var.ansible_groups

variables = {
ansible_host = coalesce(var.ansible_host, try(split("/", var.network_interfaces[0].ip).0, var.domain != "" ? "${var.hostname}.${var.domain}" : var.hostname))
ansible_user = var.ansible_user
ansible_ssh_pass = var.ansible_ssh_pass
ansible_host = coalesce(var.ansible_host, try(split("/", var.network_interfaces[0].ip).0, var.domain != null && var.domain != "" ? "${var.name}.${var.domain}" : var.name))
ansible_user = coalesce(var.ansible_user, "root")
ansible_ssh_pass = coalesce(var.ansible_ssh_pass, var.root_password, "root")
ansible_ssh_private_key_file = try(var.ansible_ssh_private_key_file, "")
}
}
10 changes: 5 additions & 5 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ output "primary_ipv4_address" {
value = try(split("/", element(var.network_interfaces, 0).ip)[0], "")
}

output "server_name" {
value = var.hostname
output "name" {
value = var.name
}

output "server_domain" {
output "domain" {
value = var.domain
}

output "server_id" {
value = var.hostname
output "id" {
value = var.name
}
79 changes: 54 additions & 25 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,51 +1,77 @@
# DEFAULTS ARE *NOT* TO BE USED IN PRODUCTION AND ARE VERY INSECURE
variable "name" {
type = string
}

variable "domain" {
type = string
default = null
nullable = true
}

variable "cloudinit_image" {
type = string
default = "https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-generic-amd64.qcow2"
}

variable "libvirt_pool" {
type = string
default = "default"
nullable = false
}

variable "disk_passthroughs" {
type = list(string)
default = []
nullable = false
}

variable "iso_urls" {
type = list(string)
default = []
nullable = false
}

variable "iso_paths" {
type = list(string)
default = []
nullable = false
}

variable "ssh_keys" {
type = list(string)
default = []
nullable = false
}

variable "password_auth" {
type = bool
default = false
default = true
nullable = false
}

variable "disable_root" {
type = bool
default = true
default = false
nullable = false
}

variable "allow_root_ssh_pwauth" {
type = bool
default = false
default = true
nullable = false
}

variable "root_password" {
type = string
default = ""
default = "root"
nullable = false
}

variable "nameservers" {
type = list(string)
default = []
nullable = false
}

variable "libvirt_external_interface" {
Expand All @@ -56,21 +82,25 @@ variable "libvirt_external_interface" {
variable "cloudinit_use_user_data" {
type = bool
default = true
nullable = false
}

variable "cloudinit_use_network_data" {
type = bool
default = true
nullable = false
}

variable "cloudinit_custom_user_data" {
type = string
default = ""
default = "# No user data\n"
nullable = false
}

variable "cloudinit_custom_network_data" {
type = string
default = ""
default = "# No network user data\n"
nullable = false
}

variable "network_interfaces" {
Expand All @@ -94,56 +124,49 @@ variable "network_interfaces" {
})))
}))
default = []
nullable = false
}

variable "memory" {
type = number
default = 2048
nullable = false
}

variable "vcpu" {
type = number
default = 2
nullable = false
}

variable "disk_size" {
type = number
default = 64424509440 # 60GB
nullable = false
}

variable "cloudinit_image" {
type = string
default = "https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-generic-amd64.qcow2"
}

variable "spice_server_enabled" {
variable "spice_enabled" {
type = bool
default = false
}

variable "hostname" {
type = string
default = "libvirt_server"
}

variable "domain" {
type = string
default = ""
nullable = false
}

variable "autostart" {
type = bool
default = true
nullable = false
}

variable "ansible_name" {
type = string
default = ""
nullable = false
}

variable "ansible_host" {
type = string
default = ""
nullable = false
}

variable "ansible_user" {
Expand All @@ -155,12 +178,18 @@ variable "ansible_user" {
variable "ansible_ssh_pass" {
type = string
default = ""
nullable = false
}

variable "ansible_groups" {
type = list(string)
default = [
"libvirt",
]
default = []
nullable = false
}

variable "ansible_ssh_private_key_file" {
type = string
description = "Defaults to null."
default = ""
nullable = false
}

0 comments on commit b6ea5d0

Please sign in to comment.