terraform {
required_providers {
proxmox = {
source = "Telmate/proxmox"
version = "2.7.0"
}
}
}
provider "proxmox" {
pm_api_url = "https://server-01/api2/json"
}
locals {
containers = [
{ hostname = "unifi", template = "local:vztmpl/debian-9.0-standard_9.7-1_amd64.tar.gz", memory = "2048", grains = { roles = ["unifi", "haproxy", "mongodb"] } },
{ hostname = "icinga", template = "local:vztmpl/debian-10-standard_10.5-1_amd64.tar.gz", grains = { roles = ["icinga-master", "mysql", "redis", "apache"]} },
{ hostname = "pihole", template = "local:vztmpl/debian-10-standard_10.5-1_amd64.tar.gz", ip = "192.168.1.253/24", gw = "192.168.1.1", grains = { roles = ["pihole", "lighttpd"]} },
{ hostname = "prometheus", template = "local:vztmpl/debian-10-standard_10.5-1_amd64.tar.gz", grains = { roles = ["prometheus", "haproxy"]} },
{ hostname = "grafana", template = "local:vztmpl/debian-10-standard_10.5-1_amd64.tar.gz", size = "30G", grains = { roles = ["influxdb", "grafana", "haproxy"]} },
{ hostname = "librenms", template = "local:vztmpl/debian-10-standard_10.5-1_amd64.tar.gz" , grains = { roles = ["librenms", "mysql", "apache"]} },
{ hostname = "smokeping", template = "local:vztmpl/debian-10-standard_10.5-1_amd64.tar.gz" , grains = { roles = ["smokeping", "apache"]} },
{ hostname = "mqttbroker", template = "local:vztmpl/debian-10-standard_10.5-1_amd64.tar.gz", grains = { roles = ["mqttbroker", "haproxy"] } },
{ hostname = "homeassistant", template = "local:vztmpl/ubuntu-20.04-standard_20.04-1_amd64.tar.gz", grains = { roles = ["homeassistant", "haproxy"] } },
{ hostname = "backup", template = "local:vztmpl/debian-10-standard_10.5-1_amd64.tar.gz", grains = { roles = ["backup"] } },
]
}
resource "proxmox_lxc" "create_container" {
count = length(local.containers)
vmid = 100+count.index
hostname = lookup(local.containers[count.index], "hostname")
ostemplate = lookup(local.containers[count.index], "template")
target_node = "server-01"
ostype = "debian"
start = true
memory = lookup(local.containers[count.index], "memory", "512")
rootfs {
storage = lookup(local.containers[count.index], "storage", "local-lvm")
size = lookup(local.containers[count.index], "size", "10G")
}
network {
name = "eth0"
bridge = "vmbr0"
ip = lookup(local.containers[count.index], "ip", "dhcp")
gw = lookup(local.containers[count.index], "gw", null)
}
provisioner "local-exec" {
when = destroy
command = <<EOT
sudo salt-key --force-color --yes -d ${self.hostname}
EOT
}
provisioner "local-exec" {
command = <<EOT
sleep 5s
sudo pct exec ${count.index+100} -- /bin/bash -c "apt update && apt install -y curl sudo"
sudo pct exec ${count.index+100} -- /bin/bash -c "curl -L https://bootstrap.saltproject.io | sudo sh -s -- -X -x python3 stable 3003"
sudo pct exec ${count.index+100} -- /bin/bash -c "systemctl stop salt-minion"
sudo pct exec ${count.index+100} -- /bin/bash -c "echo 'master: server-01 > /etc/salt/minion.d/99-master-address.conf"
sudo lxc-attach ${count.index+100} -- /bin/bash -c "echo '${yamlencode(lookup(local.containers[count.index], "grains"))}' > /etc/salt/grains"
sudo pct exec ${count.index+100} -- /bin/bash -c "systemctl restart salt-minion"
sleep 5s
sudo salt-key --force-color --yes -a ${lookup(local.containers[count.index], "hostname")}
EOT
}
}