363 lines
7.2 KiB
HCL
363 lines
7.2 KiB
HCL
|
|
|
|
|
|
# See:
|
|
# https://github.com/sk4zuzu/vm-pool/blob/master/terraform/redhat/nodes/domain.tf
|
|
# SEE: https://github.com/SUSE/ha-sap-terraform-deployments/blob/master/libvirt/modules/hana_node/main.tf#L21
|
|
|
|
#variable "tenant_networks" {
|
|
# type = any
|
|
# default = {}
|
|
#}
|
|
|
|
|
|
|
|
|
|
# Variables
|
|
# =====================
|
|
|
|
variable "name" {
|
|
description = "Instance name"
|
|
type = string
|
|
}
|
|
variable "domain" {
|
|
description = "Instance domain"
|
|
type = string
|
|
default = ""
|
|
}
|
|
variable "number" {
|
|
description = "Number of instances"
|
|
type = number
|
|
default = 1
|
|
}
|
|
|
|
#
|
|
variable "prefix" {
|
|
description = "String to prefix each instances"
|
|
type = string
|
|
default = ""
|
|
}
|
|
variable "name_fqdn" {
|
|
description = "Define instance name with fqdn"
|
|
type = bool
|
|
default = false
|
|
}
|
|
variable "name_prefix" {
|
|
description = "Define instance name with project prefix"
|
|
type = bool
|
|
default = true
|
|
}
|
|
variable "instance_pool" {
|
|
description = "Volume pool for instance disks"
|
|
type = string
|
|
default = "default"
|
|
}
|
|
variable "images_pool" {
|
|
description = "Default image pool"
|
|
type = string
|
|
default = "default"
|
|
}
|
|
variable "disk_gb" {
|
|
description = "Size in gb of the root disk"
|
|
type = number
|
|
default = 2
|
|
}
|
|
|
|
#
|
|
variable "flavor" {
|
|
description = "Instance flavor"
|
|
type = string
|
|
default = "x-small"
|
|
}
|
|
variable "memory" {
|
|
description = "Instance memory"
|
|
type = string
|
|
default = "2048"
|
|
}
|
|
variable "vcpu" {
|
|
description = "Number of vcpu"
|
|
type = number
|
|
default = 2
|
|
}
|
|
|
|
|
|
|
|
variable "disks" {
|
|
description = "Ephemeral disks list"
|
|
type = list
|
|
default = []
|
|
}
|
|
variable "volumes" {
|
|
description = "Persistant volume list"
|
|
type = list
|
|
default = []
|
|
}
|
|
variable "networks" {
|
|
description = "Network list"
|
|
type = list
|
|
default = []
|
|
}
|
|
|
|
variable "wait_for_lease" {
|
|
description = "Wait for network ip assignment"
|
|
type = bool
|
|
default = false
|
|
}
|
|
|
|
variable "metadata" {
|
|
description = "Metadata do add in state"
|
|
type = any
|
|
default = {}
|
|
}
|
|
|
|
|
|
# Cloud init
|
|
# -----------
|
|
|
|
variable "user" {
|
|
description = "Default user login"
|
|
type = string
|
|
default = "cloud"
|
|
}
|
|
|
|
variable "authorized_key" {
|
|
description = "Default user authorized key"
|
|
type = string
|
|
default = ""
|
|
}
|
|
|
|
# Note: This should only be used for debugging purpose
|
|
variable "password_hash" {
|
|
description = "Default user password hash (ie: $1$SaltSalt$GhE887kYCerthShgxern00)"
|
|
type = string
|
|
default = ""
|
|
# sensitive = true
|
|
}
|
|
|
|
|
|
# Cloud settings
|
|
# =====================
|
|
|
|
module "system_os" {
|
|
source = "../../modules/virt_os"
|
|
count = var.number
|
|
|
|
os_name = "debian"
|
|
os_version = "10"
|
|
domain = var.domain
|
|
hostname = "${var.name}${count.index}"
|
|
user = var.user
|
|
password_hash = var.password_hash
|
|
authorized_key = var.authorized_key
|
|
}
|
|
|
|
module "system_flavor" {
|
|
source = "../../modules/virt_flavor"
|
|
count = var.number
|
|
flavor = var.flavor
|
|
}
|
|
|
|
# Volumes Configuration
|
|
# =====================
|
|
|
|
module "volume_cloudinit" {
|
|
source = "../virt_cloudinit"
|
|
count = var.number
|
|
|
|
name = "inst_${var.prefix}${var.name}${count.index}_cloudinit.iso"
|
|
pool = var.instance_pool
|
|
userdata = module.system_os[count.index].template
|
|
}
|
|
|
|
module "volume_os" {
|
|
source = "../virt_volume"
|
|
count = var.number
|
|
|
|
format = "qcow2"
|
|
name = "inst_${var.prefix}${var.name}${count.index}"
|
|
|
|
pool = var.instance_pool
|
|
size_gb = 42
|
|
base_pool = var.images_pool
|
|
# TOFIX: Hardcoded refrence to debian
|
|
base_file = "debian_latest.qcow2"
|
|
}
|
|
|
|
module "volumes_extra" {
|
|
source = "../virt_volumes"
|
|
count = var.number
|
|
|
|
volumes = var.disks
|
|
volumes_defaults = {
|
|
prefix = "inst_${var.prefix}${var.name}${count.index}_"
|
|
pool = var.instance_pool
|
|
# TOFIX: Hardcoded variable
|
|
pool_dir = "/virt"
|
|
}
|
|
}
|
|
|
|
|
|
# Instance
|
|
# =====================
|
|
|
|
locals {
|
|
hostname = replace( var.name, "_", "-")
|
|
vmname = ( var.name_prefix ?
|
|
"${var.prefix}${local.hostname}":
|
|
"${local.hostname}" )
|
|
domain = var.domain
|
|
|
|
metadata = {
|
|
#cloud_init = one(module.system_os[*].template)
|
|
os_name = "debian"
|
|
os_version = "10"
|
|
domain = var.domain
|
|
user = var.user
|
|
#password_hash = var.password_hash
|
|
authorized_key = var.authorized_key
|
|
}
|
|
}
|
|
|
|
resource "ansible_host" "ansible_def" {
|
|
|
|
count = var.number
|
|
inventory_hostname = ( var.name_fqdn ?
|
|
"${local.hostname}${count.index}.${var.domain}" :
|
|
"${local.hostname}${count.index}" )
|
|
|
|
#groups = [
|
|
# "role_${local.hostname}",
|
|
# var.prefix != "" ? "prj_${var.prefix}" : ""
|
|
# ]
|
|
vars = {
|
|
ansible_user = var.user
|
|
ansible_host = "${local.hostname}${count.index}.${var.domain}"
|
|
ansible_connection = "ssh"
|
|
|
|
instances_count = var.number
|
|
instances_index = count.index
|
|
|
|
instance_domain = var.domain
|
|
instance_hostname = "${local.hostname}${count.index}"
|
|
instance_name = "${local.hostname}"
|
|
|
|
instance_provisionning_key = var.authorized_key
|
|
instance_provisionning_user = var.user
|
|
instance_flavor = var.flavor
|
|
|
|
instance_metadata = jsonencode(merge(local.metadata, var.metadata))
|
|
|
|
# TOFIX: lol
|
|
instance_os_name = "debian"
|
|
instance_os_version = "10"
|
|
|
|
instance_vm = ( var.name_fqdn ?
|
|
"${local.vmname}${count.index}.${var.domain}" :
|
|
"${local.vmname}${count.index}" )
|
|
}
|
|
}
|
|
|
|
|
|
resource "libvirt_domain" "instdef" {
|
|
count = var.number
|
|
|
|
autostart = true
|
|
name = ( var.name_fqdn ?
|
|
"${local.vmname}${count.index}.${var.domain}" :
|
|
"${local.vmname}${count.index}" )
|
|
description = "${var.name}${count.index}.${var.domain}"
|
|
memory = var.memory
|
|
vcpu = var.vcpu
|
|
|
|
metadata = jsonencode(merge(local.metadata, var.metadata))
|
|
|
|
# Root FS
|
|
# -------------------
|
|
cloudinit = module.volume_cloudinit[count.index].volume.id
|
|
disk {
|
|
volume_id = module.volume_os[count.index].volume.id
|
|
}
|
|
|
|
# Instance disks
|
|
dynamic "disk" {
|
|
for_each = module.volumes_extra[count.index].volumes
|
|
content {
|
|
volume_id = disk.value.volume.id
|
|
#volume_id = disk.value.info.id
|
|
#volume_id = coalesce(disk.value.volume.id)
|
|
#file = coalesce(disk.value.info.id)
|
|
}
|
|
}
|
|
|
|
# Networking
|
|
# -------------------
|
|
dynamic "network_interface" {
|
|
for_each = var.networks
|
|
content {
|
|
network_name = network_interface.value.name
|
|
mac = try(network_interface.value.mac, null)
|
|
addresses = try(network_interface.value.addresses, null)
|
|
#hostname = try(network_interface.value.hostname, var.name)
|
|
|
|
wait_for_lease = try(network_interface.value.wait_for_lease, var.wait_for_lease )
|
|
macvtap = try(network_interface.value.macvtap, null)
|
|
vepa = try(network_interface.value.vepa, null)
|
|
}
|
|
}
|
|
|
|
# Other
|
|
# -------------------
|
|
console {
|
|
type = "pty"
|
|
target_port = "0"
|
|
target_type = "serial"
|
|
}
|
|
|
|
console {
|
|
type = "pty"
|
|
target_type = "virtio"
|
|
target_port = "1"
|
|
}
|
|
|
|
graphics {
|
|
type = "spice"
|
|
listen_type = "address"
|
|
autoport = true
|
|
}
|
|
|
|
cpu {
|
|
mode = "host-passthrough"
|
|
}
|
|
|
|
}
|
|
|
|
|
|
# Outputs
|
|
# =====================
|
|
|
|
output "instance" {
|
|
value = try(resource.libvirt_domain.instdef, null)
|
|
}
|
|
|
|
|
|
# output "volume_cloudinit" {
|
|
# value = module.volume_cloudinit
|
|
# }
|
|
output "volume_os" {
|
|
value = module.volume_os
|
|
}
|
|
output "volumes_extra" {
|
|
value = module.volumes_extra
|
|
}
|
|
|
|
# Tag infos
|
|
output "system_os" {
|
|
value = module.system_os
|
|
}
|
|
output "system_flavor" {
|
|
value = module.system_flavor
|
|
}
|
|
|
|
|
|
|