terraform-incus/modules/instance/main.tf
Ben Vincent cb67816eee feat: initial commit
- have been working on this for some time now
2025-05-30 22:36:55 +10:00

140 lines
3.2 KiB
HCL

module "storage_volume" {
source = "../../../../../../modules/storage_volume"
for_each = {
for k, v in var.storage_volumes : "${var.name}-${k}" => v
}
name = each.key
pool = each.value.pool
description = lookup(each.value, "description", null)
type = lookup(each.value, "type", "custom")
content_type = lookup(each.value, "content_type", null)
config = lookup(each.value, "config", {})
}
resource "incus_instance" "this" {
name = var.name
image = var.image
description = var.description
type = var.type
ephemeral = var.ephemeral
running = var.running
profiles = var.profiles
dynamic "wait_for" {
for_each = var.wait_for
content {
type = wait_for.value.type
nic = wait_for.value.nic
}
}
dynamic "device" {
for_each = var.disk_devices
content {
name = device.value.name
type = device.value.type
properties = {
path = device.value.properties["path"]
source = device.value.properties["source"]
pool = device.value.properties["pool"]
}
}
}
dynamic "device" {
for_each = {
for k, v in var.storage_volumes : k => {
name = k
path = v.path
pool = v.pool
source = "${var.name}-${k}"
}
}
content {
name = device.value.name
type = "disk"
properties = {
path = device.value.path
source = device.value.source
pool = device.value.pool
}
}
}
depends_on = [
module.storage_volume
]
}
resource "cobbler_system" "this" {
name = "${var.name}.${var.cobbler_domain}"
hostname = "${var.name}.${var.cobbler_domain}"
profile = var.cobbler_profile
status = "testing"
name_servers = var.cobbler_name_servers
mgmt_classes = var.cobbler_mgmt_classes
name_servers_search = ["${var.cobbler_domain}"]
interface {
name = "eth0"
mac_address = incus_instance.this.mac_address
static = true
ip_address = incus_instance.this.ipv4_address
netmask = var.cobbler_netmask
dns_name = "${var.name}.${var.cobbler_domain}"
gateway = local.gateway_ip
}
depends_on = [incus_instance.this]
}
resource "puppetdb_node" "this" {
certname = "${var.name}.${var.cobbler_domain}"
depends_on = [incus_instance.this]
}
resource "null_resource" "wait_for_instance_ready" {
depends_on = [incus_instance.this]
provisioner "local-exec" {
command = "sleep 10"
}
}
resource "puppetca_certificate" "cert" {
name = "${var.name}.${var.cobbler_domain}"
env = "production"
sign = false
depends_on = [
null_resource.wait_for_instance_ready
]
lifecycle {
create_before_destroy = false
}
}
#resource "puppetca_certificate" "cert" {
# count = (
# var.check_on_instance_creation ? 1 :
# (
# can(incus_instance.this.ipv4_address) && incus_instance.this.ipv4_address != "" ? 1 : 0
# )
# )
#
# name = "${var.name}.${var.cobbler_domain}"
# env = "production"
# sign = false
#
# depends_on = [incus_instance.this]
#
# lifecycle {
# create_before_destroy = false
# }
#}