feat(module): adding Nebula module for OpenNebula virtual machine creation with a 'variable'

This commit is contained in:
Philippe Caseiro 2023-02-02 14:43:16 +01:00
parent b209942b3d
commit 5059720757
3 changed files with 136 additions and 0 deletions

116
modules/nebula/main.tf Normal file
View File

@ -0,0 +1,116 @@
variable "vms" {
type = map
description = "Virtual Machines description"
}
variable "image_driver" {
default = "qcow2"
}
variable "image_format" {
default = "qcow2"
}
variable "dev_prefix" {
default = "vd"
}
variable "permissions" {
default = "600"
}
variable "graphics" {
default = {
keymap = "fr"
listen = "0.0.0.0"
type = "VNC"
}
}
variable "one_prod_ds" {
default = "101"
}
// system image
resource "opennebula_image" "system" {
for_each = var.vms
name = "system-${each.key}.${each.value.domain}"
description = "System disk for ${each.key}"
datastore_id = var.one_prod_ds
path = "https://vulcain.cadoles.com/empty_20G"
persistent = each.value.system_image_persistent
driver = var.image_driver
format = var.image_format
dev_prefix = var.dev_prefix
permissions = var.permissions
}
// data image
resource "opennebula_image" "data" {
for_each = var.vms
name = "data-${each.key}.${each.value.domain}"
path = "https://vulcain.cadoles.com/empty_40G"
datastore_id = var.one_prod_ds
description = "data disk for ${each.key}"
persistent = each.value.data_image_persistent
driver = var.image_driver
format = var.image_format
dev_prefix = var.dev_prefix
permissions = var.permissions
}
resource "opennebula_template" "vm_template" {
for_each = var.vms
name = each.key
cpu = each.value.cpu
vcpu = each.value.vcpu
memory = each.value.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[each.key].id
}
disk {
image_id = opennebula_image.data[each.key].id
}
os {
arch = "x86_64"
boot = "disk0,nic0"
}
//labels = var.tag_labels
dynamic "nic" {
for_each = each.value.network_interfaces
content {
network_id = nic.value.network_id
model = "virtio"
ip = nic.value.ip
}
}
permissions = var.permissions
depends_on = [ opennebula_image.system,
opennebula_image.data ]
}
resource "opennebula_virtual_machine" "vm" {
for_each = var.vms
name = "${each.key}.${each.value.domain}"
template_id = opennebula_template.vm_template[each.key].id
depends_on = [ opennebula_template.vm_template ]
}

11
modules/nebula/output.tf Normal file
View File

@ -0,0 +1,11 @@
output "shortnames" {
value = keys(var.vms)
}
output "first_macs" {
value = opennebula_virtual_machine.vm //FIXME .template_nic[0].computed_mac
}
output "virtual_machines" {
value = opennebula_virtual_machine.vm //template_nic[0].computed_mac)
}

View File

@ -0,0 +1,9 @@
terraform {
required_providers {
opennebula = {
source = "OpenNebula/opennebula"
version = "~> 1.0"
}
}
}