fabrica/.terraform/modules/virtualMachine/main.tf

116 lines
2.5 KiB
Terraform
Raw Permalink Normal View History

2021-03-22 19:52:11 +01:00
// Dependencies management
resource "null_resource" "depends_on" {
triggers = {
depends_on = join("", var.depends)
}
}
// OpenNebula disk image !
resource "opennebula_image" "system" {
name = var.system_image_name
description = "System disk image"
datastore_id = var.one_prod_ds
//persistent = false
persistent = true
//lock = "MANAGE"
path = var.system_image_source
dev_prefix = "vd"
driver = "qcow2"
format = "qcow2"
permissions = "660"
group = "production"
}
resource "opennebula_image" "data" {
name = var.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 = "production"
count = var.data_image_name != "" ? 1 : 0
}
// OpenNebula (VM) Template definition
resource "opennebula_template" "vm_template" {
name = var.vm_shortname
cpu = var.cpu
vcpu = var.vcpu
memory = var.ram
graphics {
keymap = "fr"
listen = "0.0.0.0"
type = "VNC"
}
context = {
DNS_HOSTNAME = "yes"
NETWORK = "YES"
SSH_PUBLIC_KEY = "$USER[SSH_PUBLIC_KEY]"
USERNAME = "root"
}
disk {
image_id = opennebula_image.system.id
}
dynamic "disk" {
for_each = opennebula_image.data
content {
image_id = disk.id
}
}
//labels = var.tag_labels
dynamic "nic" {
for_each = var.network_interfaces
content {
network_id = var.vnets[nic.value.network_name].id
model = "virtio"
ip = nic.value.ip
}
}
permissions = "600"
group = "production"
depends_on = [ opennebula_image.system,
opennebula_image.data ]
}
// OpenNebula Virtual Machine
resource "opennebula_virtual_machine" "vm" {
name = var.vm_fqdn
group = "production"
timeout = 10
template_id = opennebula_template.vm_template.id
depends_on = [ opennebula_template.vm_template,
null_resource.depends_on ]
}
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 = "${var.vm_fqdn}."
ttl = 300
depends_on = [ opennebula_virtual_machine.vm ]
}