Skip to content

Commit

Permalink
Remove vm_ prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
x86-39 committed Jun 8, 2023
1 parent e54c1c4 commit d8f7446
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
66 changes: 33 additions & 33 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
resource "libvirt_volume" "cloudinit_image" {
name = "${var.vm_hostname}_cloudinit_image"
pool = var.vm_libvirt_pool
source = var.vm_cloudinit_image
name = "${var.hostname}_cloudinit_image"
pool = var.libvirt_pool
source = var.cloudinit_image
format = "qcow2"
}

resource "libvirt_volume" "vm_disk" {
name = "${var.vm_hostname}_vm_disk"
pool = var.vm_libvirt_pool
resource "libvirt_volume" "disk" {
name = "${var.hostname}_disk"
pool = var.libvirt_pool
base_volume_id = libvirt_volume.cloudinit_image.id
size = var.vm_disk_size
size = var.disk_size
}

data "template_file" "vm_cloudinit" {
data "template_file" "cloudinit" {
template = <<-EOT
#cloud-config
hostname: ${var.vm_hostname}
fqdn: ${var.vm_hostname}.${var.vm_domain}
hostname: ${var.hostname}
fqdn: ${var.hostname}.${var.domain}
ssh_pwauth: True
ssh_deletekeys: False
%{ if length(var.vm_ssh_keys) > 0 }
%{ if length(var.ssh_keys) > 0 }
ssh_authorized_keys:
%{ for key in var.vm_ssh_keys }
%{ for key in var.ssh_keys }
- "${key}"
%{ endfor }
%{ endif }
network:
ethernets:
eth0:
dhcp4: ${var.vm_dhcp}
dhcp4: ${var.dhcp}
dhcp6: false
%{ if var.vm_dhcp == false ~}
addresses: ["${var.vm_ip}"]
gateway4: ${var.vm_gateway}
%{ if var.dhcp == false ~}
addresses: ["${var.ip}"]
gateway4: ${var.gateway}
nameservers:
%{ if length(var.vm_nameservers) > 0 ~}
%{ for nameserver in var.vm_nameservers ~}
%{ if length(var.nameservers) > 0 ~}
%{ for nameserver in var.nameservers ~}
- ${nameserver}
%{ endfor ~}
%{ endif ~}
Expand All @@ -49,35 +49,35 @@ data "template_file" "vm_cloudinit" {
}

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

user_data = data.template_file.vm_cloudinit.rendered
user_data = data.template_file.cloudinit.rendered
}

resource "libvirt_domain" "domain" {
name = var.vm_hostname
memory = var.vm_memory
vcpu = var.vm_vcpu
name = var.hostname
memory = var.memory
vcpu = var.vcpu

cloudinit = libvirt_cloudinit_disk.init_disk.id // Attach cloud-init disk

disk {
volume_id = libvirt_volume.vm_disk.id
volume_id = libvirt_volume.disk.id
}

dynamic "disk" {
for_each = var.vm_disk_passthroughs
for_each = var.disk_passthroughs
content {
block_device = disk.value
}
}

network_interface {
macvtap = var.vm_libvirt_external_interface
hostname = var.vm_hostname
macvtap = var.libvirt_external_interface
hostname = var.hostname
wait_for_lease = false
mac = var.vm_mac // For some providers, this is required
mac = var.mac // For some providers, this is required
}

console {
Expand All @@ -88,17 +88,17 @@ resource "libvirt_domain" "domain" {

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

}

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

variables = {
ansible_host = coalesce(var.ansible_host, "${var.vm_ip}", "${var.vm_hostname}.${var.vm_domain}")
ansible_host = coalesce(var.ansible_host, "${var.ip}", "${var.hostname}.${var.domain}")
ansible_user = "root"
}
}
34 changes: 17 additions & 17 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
variable "vm_libvirt_pool" {
variable "libvirt_pool" {
type = string
default = "default"
}

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

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

variable "vm_password_auth" {
variable "password_auth" {
type = bool
default = false
}

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

variable "vm_libvirt_external_interface" {
variable "libvirt_external_interface" {
type = string
default = "eth0"
}

variable "vm_dhcp" {
variable "dhcp" {
type = bool
default = true
}

variable "vm_ip" {
variable "ip" {
type = string
default = ""
}

variable "vm_gateway" {
variable "gateway" {
type = string
default = "" # 1.2.3.4/5
}

variable "vm_mac" {
variable "mac" {
type = string
default = null
}

variable "vm_memory" {
variable "memory" {
type = number
default = 2048
}

variable "vm_vcpu" {
variable "vcpu" {
type = number
default = 2
}

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

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

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

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

variable "vm_domain" {
variable "domain" {
type = string
default = "example.com"
}
Expand Down

0 comments on commit d8f7446

Please sign in to comment.