169 lines
4.3 KiB
Terraform
169 lines
4.3 KiB
Terraform
|
terraform {
|
||
|
required_providers {
|
||
|
proxmox = {
|
||
|
source = "bpg/proxmox"
|
||
|
version = "~>0.56.1"
|
||
|
}
|
||
|
}
|
||
|
required_version = ">=1.6.2"
|
||
|
}
|
||
|
|
||
|
|
||
|
locals {
|
||
|
core_user = {
|
||
|
name = "core"
|
||
|
password_hash = "$6$vDMAZf/yOO6mEbcs$6VE7WD8T9/PeotszMFxatOQxB/rFmLDWsNajg4sI0O47OikSuVpqPjkxRbzcueiXn6rBUY1ubCHlp0nnoZ1VI1"
|
||
|
}
|
||
|
|
||
|
hostname_file = {
|
||
|
path = "/etc/hostname"
|
||
|
user = {id = 0}
|
||
|
group = {id = 0}
|
||
|
mode = 420 # 0644
|
||
|
contents = {
|
||
|
source = format(
|
||
|
"data:text/plain;base64,%s",
|
||
|
base64encode(var.instance_name)
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ignition_configuration = jsonencode({
|
||
|
ignition = {
|
||
|
version = "3.4.0"
|
||
|
}
|
||
|
storage = {
|
||
|
files = [
|
||
|
{
|
||
|
path = "/etc/hostname"
|
||
|
user = {id = 0}
|
||
|
group = {id = 0}
|
||
|
mode = 420 # 0644
|
||
|
contents = {
|
||
|
source = format(
|
||
|
"data:text/plain;base64,%s",
|
||
|
base64encode(var.instance_name)
|
||
|
)
|
||
|
}
|
||
|
},
|
||
|
]
|
||
|
}
|
||
|
passwd = {
|
||
|
users = [
|
||
|
local.core_user,
|
||
|
]
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
resource "random_pet" "config_name" {
|
||
|
length = 4
|
||
|
}
|
||
|
|
||
|
locals {
|
||
|
generated_ignition_config_file = "${path.module}/dns_resolver_ignition_config_${random_pet.config_name.id}.ign"
|
||
|
}
|
||
|
|
||
|
resource "local_file" "sftp_script_for_ignition_file" {
|
||
|
content = <<EOT
|
||
|
cd writable
|
||
|
-rm ${var.pve_vm_id}.ign
|
||
|
put ${local.generated_ignition_config_file} ${var.pve_vm_id}.ign
|
||
|
EOT
|
||
|
filename = "${path.module}/dns_resolver_sftp_script_for_ignition_config_${random_pet.config_name.id}"
|
||
|
file_permission = "0644"
|
||
|
}
|
||
|
|
||
|
resource "local_file" "dns_resolver_ignition_config" {
|
||
|
content = local.ignition_configuration
|
||
|
filename = local.generated_ignition_config_file
|
||
|
file_permission = "0644"
|
||
|
|
||
|
provisioner "local-exec" {
|
||
|
command = <<EOT
|
||
|
sftp -P ${var.netboot_server_sftp_port} \
|
||
|
-o ProxyJump=${var.pve_ssh_user}@${var.pve_ssh_host} \
|
||
|
-b "${path.module}/dns_resolver_sftp_script_for_ignition_config_${random_pet.config_name.id}" \
|
||
|
terraform_ignition@${var.netboot_server_ip_address}
|
||
|
EOT
|
||
|
}
|
||
|
lifecycle {
|
||
|
replace_triggered_by = [local_file.sftp_script_for_ignition_file]
|
||
|
}
|
||
|
}
|
||
|
|
||
|
resource "local_file" "sftp_script_for_dhcp_config" {
|
||
|
content = <<EOT
|
||
|
cd writable
|
||
|
-rm ${var.pve_vm_id}.conf
|
||
|
put ${path.module}/dns_resolver_dhcp_config_${random_pet.config_name.id}.conf ${var.pve_vm_id}.conf
|
||
|
EOT
|
||
|
filename = "${path.module}/dns_resolver_sftp_script_for_dhcp_config_${random_pet.config_name.id}"
|
||
|
file_permission = "0644"
|
||
|
}
|
||
|
|
||
|
resource "local_file" "dhcp_config" {
|
||
|
depends_on = [ local_file.sftp_script_for_dhcp_config ]
|
||
|
content = templatefile(
|
||
|
"${path.module}/files/dhcp_config.conf.tftpl",
|
||
|
{
|
||
|
vm_id = var.pve_vm_id
|
||
|
host_ip = cidrhost(var.prod_network.prefix, var.pve_vm_id)
|
||
|
mac_address = var.prod_network.mac_address
|
||
|
}
|
||
|
)
|
||
|
filename = "${path.module}/dns_resolver_dhcp_config_${random_pet.config_name.id}.conf"
|
||
|
file_permission = "0644"
|
||
|
|
||
|
provisioner "local-exec" {
|
||
|
command = <<EOT
|
||
|
sftp -P ${var.netboot_server_sftp_port} \
|
||
|
-o ProxyJump=${var.pve_ssh_user}@${var.pve_ssh_host} \
|
||
|
-b "${path.module}/dns_resolver_sftp_script_for_dhcp_config_${random_pet.config_name.id}" \
|
||
|
terraform_dhcp@${var.netboot_server_ip_address}
|
||
|
EOT
|
||
|
}
|
||
|
|
||
|
lifecycle {
|
||
|
replace_triggered_by = [local_file.sftp_script_for_dhcp_config ]
|
||
|
}
|
||
|
}
|
||
|
|
||
|
resource "proxmox_virtual_environment_vm" "netboot_server" {
|
||
|
name = var.instance_name
|
||
|
node_name = var.pve_node_name
|
||
|
vm_id = var.pve_vm_id
|
||
|
|
||
|
cpu {
|
||
|
architecture = "x86_64"
|
||
|
type = "host"
|
||
|
sockets = 1
|
||
|
cores = 4
|
||
|
}
|
||
|
|
||
|
memory {
|
||
|
dedicated = 4096
|
||
|
}
|
||
|
|
||
|
disk {
|
||
|
datastore_id = var.pve_storage_id
|
||
|
interface = "virtio0"
|
||
|
size = 10
|
||
|
}
|
||
|
|
||
|
network_device {
|
||
|
bridge = "prod"
|
||
|
model = "virtio"
|
||
|
mac_address = var.prod_network.mac_address
|
||
|
}
|
||
|
|
||
|
boot_order = ["net0"]
|
||
|
|
||
|
operating_system {
|
||
|
type = "l26"
|
||
|
}
|
||
|
|
||
|
vga {}
|
||
|
serial_device{}
|
||
|
}
|