Init commit
This commit is contained in:
35
modules/node/cloud-config/default.yml
Normal file
35
modules/node/cloud-config/default.yml
Normal file
@@ -0,0 +1,35 @@
|
||||
#cloud-config
|
||||
|
||||
timezone: Europe/Moscow
|
||||
|
||||
users:
|
||||
- default
|
||||
- name: ubuntu
|
||||
groups: [sudo]
|
||||
shell: /bin/bash
|
||||
lock_passwd: true
|
||||
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
|
||||
ssh_authorized_keys:
|
||||
- ${ssh_key}
|
||||
|
||||
ssh_pwauth: false
|
||||
|
||||
package_update: true
|
||||
|
||||
packages:
|
||||
- qemu-guest-agent
|
||||
|
||||
runcmd:
|
||||
- systemctl enable --now qemu-guest-agent
|
||||
- hostnamectl set-hostname ${hostname}
|
||||
- systemctl disable --now packagekit
|
||||
- systemctl disable --now ModemManager
|
||||
- systemctl disable --now multipathd
|
||||
|
||||
write_files:
|
||||
- path: /etc/motd
|
||||
content: |
|
||||
Managed by OpenTofu
|
||||
|
||||
|
||||
final_message: "cloud-init finished"
|
||||
29
modules/node/locals.tf
Normal file
29
modules/node/locals.tf
Normal file
@@ -0,0 +1,29 @@
|
||||
locals {
|
||||
ssh_public_key = var.ssh_key
|
||||
|
||||
nodes = {
|
||||
for name, node in var.nodes :
|
||||
name => node
|
||||
}
|
||||
|
||||
ip_map = {
|
||||
for name, node in local.nodes :
|
||||
name => coalesce(
|
||||
lookup(node, "ip", null),
|
||||
"${var.network_base}.${var.cluster_ip_start + node.ip_offset + node.index}"
|
||||
)
|
||||
}
|
||||
|
||||
vmid_map = {
|
||||
for name, node in local.nodes :
|
||||
name => coalesce(
|
||||
lookup(node, "vmid", null),
|
||||
var.worker_vmid_start + node.index
|
||||
)
|
||||
}
|
||||
|
||||
hostname_map = {
|
||||
for name, node in local.nodes :
|
||||
name => "${name}"
|
||||
}
|
||||
}
|
||||
92
modules/node/main.tf
Normal file
92
modules/node/main.tf
Normal file
@@ -0,0 +1,92 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
proxmox = {
|
||||
source = "registry.opentofu.org/bpg/proxmox"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "proxmox_virtual_environment_file" "cloudinit" {
|
||||
for_each = local.nodes
|
||||
content_type = "snippets"
|
||||
datastore_id = var.cloudinit_datastore
|
||||
node_name = var.proxmox_node
|
||||
|
||||
source_raw {
|
||||
file_name = "${each.key}.yml"
|
||||
data = fileexists("${path.root}/cloud-config/${coalesce(each.value.cloudinit, "default.yml")}") ? templatefile(
|
||||
"${path.root}/cloud-config/${coalesce(each.value.cloudinit, "default.yml")}",
|
||||
{
|
||||
hostname = local.hostname_map[each.key]
|
||||
ssh_key = local.ssh_public_key
|
||||
}
|
||||
) : templatefile(
|
||||
"${path.module}/cloud-config/default.yml",
|
||||
{
|
||||
hostname = local.hostname_map[each.key]
|
||||
ssh_key = local.ssh_public_key
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
resource "proxmox_virtual_environment_vm" "nodes" {
|
||||
for_each = local.nodes
|
||||
|
||||
name = local.hostname_map[each.key]
|
||||
node_name = var.proxmox_node
|
||||
|
||||
# allow vmid override
|
||||
vm_id = coalesce(
|
||||
lookup(local.nodes[each.key], "vmid", null),
|
||||
local.vmid_map[each.key]
|
||||
)
|
||||
|
||||
agent {
|
||||
enabled = true
|
||||
}
|
||||
|
||||
cpu {
|
||||
cores = each.value.cpu
|
||||
}
|
||||
|
||||
memory {
|
||||
dedicated = each.value.memory
|
||||
}
|
||||
|
||||
network_device {
|
||||
bridge = var.node_bridge
|
||||
# vlan_id = try(each.value.vlan_id, null)
|
||||
vlan_id = each.value.vlan_id
|
||||
|
||||
}
|
||||
|
||||
disk {
|
||||
datastore_id = each.value.datastore
|
||||
import_from = "${var.image_datastore}:${var.image_file}"
|
||||
interface = var.disk_interface
|
||||
size = each.value.disk
|
||||
}
|
||||
|
||||
dynamic "disk" {
|
||||
for_each = try([each.value.data_disk], [])
|
||||
|
||||
content {
|
||||
datastore_id = var.data_datastore
|
||||
interface = "scsi1"
|
||||
size = disk.value
|
||||
}
|
||||
}
|
||||
|
||||
initialization {
|
||||
datastore_id = each.value.datastore
|
||||
user_data_file_id = proxmox_virtual_environment_file.cloudinit[each.key].id
|
||||
|
||||
ip_config {
|
||||
ipv4 {
|
||||
address = "${local.ip_map[each.key]}/${var.network_cidr}"
|
||||
gateway = var.cluster_gateway
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
17
modules/node/outputs.tf
Normal file
17
modules/node/outputs.tf
Normal file
@@ -0,0 +1,17 @@
|
||||
output "ip_addresses" {
|
||||
description = "IP addresses of all created nodes"
|
||||
value = {
|
||||
for name, _ in local.nodes :
|
||||
name => proxmox_virtual_environment_vm.nodes[name].ipv4_addresses[1][0]
|
||||
}
|
||||
}
|
||||
|
||||
output "hostnames" {
|
||||
description = "Hostnames of all created nodes"
|
||||
value = local.hostname_map
|
||||
}
|
||||
|
||||
output "vmids" {
|
||||
description = "VMIDs of all created nodes"
|
||||
value = local.vmid_map
|
||||
}
|
||||
71
modules/node/variables.tf
Normal file
71
modules/node/variables.tf
Normal file
@@ -0,0 +1,71 @@
|
||||
variable "ssh_key" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "nodes" {
|
||||
type = map(object({
|
||||
# role = string
|
||||
index = number
|
||||
cpu = number
|
||||
memory = number
|
||||
disk = number
|
||||
datastore = string
|
||||
ip_offset = optional(number)
|
||||
ip = optional(string)
|
||||
vmid = optional(number)
|
||||
vlan_id = optional(number)
|
||||
data_disk = optional(number)
|
||||
cloudinit = optional(string)
|
||||
}))
|
||||
}
|
||||
|
||||
variable "cluster_ip_start" {
|
||||
type = number
|
||||
}
|
||||
|
||||
variable "worker_vmid_start" {
|
||||
type = number
|
||||
}
|
||||
|
||||
variable "cloudinit_datastore" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "proxmox_node" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "node_bridge" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "image_datastore" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "image_file" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "disk_interface" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "network_base" {
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "network_cidr" {
|
||||
type = number
|
||||
}
|
||||
|
||||
variable "cluster_gateway" {
|
||||
type = string
|
||||
}
|
||||
|
||||
|
||||
|
||||
variable "data_datastore" {
|
||||
type = string
|
||||
description = "Datastore for data disk"
|
||||
}
|
||||
Reference in New Issue
Block a user