diff --git a/proxmox/terraform/modules/node/main.tf b/proxmox/terraform/modules/node/main.tf index 349bd9a..fe308e9 100644 --- a/proxmox/terraform/modules/node/main.tf +++ b/proxmox/terraform/modules/node/main.tf @@ -1,102 +1,109 @@ resource "proxmox_vm_qemu" "node" { name = var.hostname desc = var.description vmid = var.vmid balloon = var.balloon full_clone = false # hypervisor onto which make the vm target_node = var.hypervisor # See init-template.md to see the template vm bootstrap clone = var.template boot = "c" # linux kernel 2.6 qemu_os = "l26" # generic setup sockets = var.sockets cores = var.cores numa = var.numa memory = var.memory # boot machine when hypervirsor starts onboot = true #### cloud-init setup os_type = "cloud-init" # ciuser - User name to change to use when connecting ciuser = var.config["user_admin"] ssh_user = var.config["user_admin"] # sshkeys - public ssh key to use when connecting sshkeys = var.config["user_admin_ssh_public_key"] # searchdomain - Sets DNS search domains for a container. searchdomain = var.config["domain"] # nameserver - Sets DNS server IP address for a container. nameserver = var.config["dns"] # ipconfig0 - [gw =] [,ip=] ipconfig0 = "ip=${var.networks[0]["ip"]}/24,gw=${var.networks[0]["gateway"]}" # Mostly, var.networks holds only one network declaration except for gateways # Try to lookup such value, if it fails (or is undefined), then ipconfig1 # will be empty, thus no secondary ip config ipconfig1 = try(lookup(var.networks[1], "ip"), "") != "" ? "ip=${var.networks[1]["ip"]}/24" : "" #### dynamic disk { for_each = var.storages content { id = disk.value["id"] storage = disk.value["storage"] size = disk.value["size"] type = "virtio" # storage_type: https://pve.proxmox.com/wiki/Storage storage_type = lookup(disk.value, "storage_type", "cephfs") } } dynamic network { for_each = var.networks content { id = lookup(network.value, "id", 0) macaddr = lookup(network.value, "macaddr", "") bridge = lookup(network.value, "bridge", "vmbr443") model = "virtio" } } #### provisioning: (creation time only) connect through ssh + # Let puppet do its install provisioner "remote-exec" { inline = concat( var.pre_provision_steps, [ + # First install facts... + "mkdir -p /etc/facter/facts.d", + "echo deployment=${var.facter_deployment} > /etc/facter/facts.d/deployment.txt", + "echo subnet=${var.facter_subnet} > /etc/facter/facts.d/subnet.txt", "sed -i 's/127.0.1.1/${lookup(var.networks[0], "ip")}/g' /etc/hosts", + # so puppet agent installs the node's role "puppet agent --server ${var.config["puppet_master"]} --environment=${var.config["puppet_environment"]} --waitforcert 60 --test || echo 'Node provisionned!'", - ] - ) + ]) + connection { - type = "ssh" - user = "root" - host = lookup(var.networks[0], "ip") + type = "ssh" + user = "root" + host = lookup(var.networks[0], "ip") + private_key = "${file("~/.ssh/id-rsa-terraform-proxmox")}" # <- something changed } } lifecycle { ignore_changes = [ bootdisk, scsihw, target_node, clone ] } } diff --git a/proxmox/terraform/modules/node/variables.tf b/proxmox/terraform/modules/node/variables.tf index 58bbcec..3fda89f 100644 --- a/proxmox/terraform/modules/node/variables.tf +++ b/proxmox/terraform/modules/node/variables.tf @@ -1,96 +1,108 @@ variable "hostname" { description = "Node's hostname" type = string } +variable "facter_subnet" { + description = "Subnet custom fact (e.g sesi_rocquencourt_staging, ...)" + type = string + default = "sesi_rocquencourt_staging" +} + +variable "facter_deployment" { + description = "Deployment custom fact (e.g staging, production)" + type = string + default = "staging" +} + variable "description" { description = "Node's description" type = string } variable "hypervisor" { description = "Hypervisor to install the vm to (choice: orsay, hypervisor3, beaubourg, branly)" type = string } variable "template" { description = "Template created by packer to use (template-debian-10, debian-buster-...)" type = string default = "debian-buster-2020-11-06" } variable "sockets" { description = "Number of sockets" type = string default = "1" } variable "cores" { description = "Number of cores" type = string default = "1" } variable "memory" { description = "Memory in Mb" type = string default = "1024" } variable "networks" { description = "Default networks configuration (id, ip, gateway, macaddr, bridge)" type = list(object({ id = number ip = string gateway = string macaddr = string bridge = string })) default = [] } variable "vmid" { description = "virtual machine id" type = number default = 0 } variable "balloon" { description = "ballooning option" type = number default = 0 } variable "numa" { type = bool default = false } variable "storages" { description = "Default disks configuration (id, storage, size, storage_type)" type = list(object({ id = number storage = string size = string storage_type = string })) default = [{ id = 0 storage = "proxmox" size = "32G" storage_type = "cephfs" }] } variable "config" { description = "Local config to avoid duplication from the main module" type = map(string) } variable "pre_provision_steps" { description = "List of sequential provisioning steps to apply" type = list(string) default = [] }