terracadoles/modules/virtualMachine/main.tf

109 lines
2.3 KiB
HCL

resource "opennebula_group" "main" {
name = var.group
}
// OpenNebula disk image !
resource "opennebula_image" "system" {
name = local.system_image_name
description = "System disk image"
datastore_id = var.one_prod_ds
persistent = false
//lock = "MANAGE"
path = var.system_image_source
dev_prefix = "vd"
driver = "qcow2"
format = "qcow2"
permissions = var.imagemode
group = var.group
}
resource "opennebula_image" "data" {
name = local.data_image_name
description = "Data disk image"
datastore_id = var.one_prod_ds
persistent = true
//lock = "MANAGE"
path = var.data_image_source
dev_prefix = "vd"
driver = "qcow2"
format = "qcow2"
permissions = "660"
group = var.group
}
// OpenNebula (VM) Template definition
resource "opennebula_template" "vm_template" {
name = var.vm_shortname
cpu = var.cpu
vcpu = var.vcpu
memory = var.ram
graphics {
keymap = var.graphics.keymap
listen = var.graphics.listen
type = var.graphics.type
}
context = {
SET_HOSTNAME = "$NAME"
DNS_HOSTNAME = "YES"
NETWORK = "YES"
SSH_PUBLIC_KEY = "$USER[SSH_PUBLIC_KEY]"
USERNAME = "root"
}
disk {
image_id = opennebula_image.system.id
}
disk {
image_id = opennebula_image.data.id
}
os {
arch = "x86_64"
boot = "disk0,nic0"
}
//labels = var.tag_labels
dynamic "nic" {
for_each = var.network_interfaces
content {
network_id = nic.value.network_id
model = "virtio"
ip = nic.value.ip
}
}
permissions = "600"
group = var.group
depends_on = [ opennebula_image.system,
opennebula_image.data ]
}
// OpenNebula Virtual Machine
resource "opennebula_virtual_machine" "vm" {
name = local.fqdn
group = var.group
template_id = opennebula_template.vm_template.id
depends_on = [ opennebula_template.vm_template ]
}
resource "dns_a_record_set" "vm_dns_a_record" {
count = var.dns_a_record == "yes" ? 1 : 0
zone = var.vm_domain
name = var.vm_shortname
ttl = 300
addresses = [ var.network_interfaces[0].ip ]
depends_on = [ opennebula_virtual_machine.vm ]
}
resource "dns_cname_record" "vm_dns_cname_record" {
for_each = var.dns_cname_record
zone = var.vm_domain
name = each.value
cname = "${local.fqdn}."
ttl = 300
depends_on = [ opennebula_virtual_machine.vm ]
}