Зависимость между модулями

This commit is contained in:
admin 2026-03-08 17:17:48 +03:00
parent 8063e5fdcb
commit d75a9b3b64
15 changed files with 261 additions and 242 deletions

View File

@ -1 +1 @@
{"Modules":[{"Key":"","Source":"","Dir":"."},{"Key":"app","Source":"./modules/lxc_container","Dir":"modules/lxc_container"},{"Key":"apps","Source":"./modules/lxc_dynamic","Dir":"modules/lxc_dynamic"}]}
{"Modules":[{"Key":"","Source":"","Dir":"."},{"Key":"app","Source":"./modules/app","Dir":"modules/app"},{"Key":"apps","Source":"./modules/lxc_dynamic","Dir":"modules/lxc_dynamic"},{"Key":"db","Source":"./modules/db","Dir":"modules/db"}]}

38
main.tf
View File

@ -1,12 +1,12 @@
module "app" {
source = "./modules/lxc_container"
module "db" {
source = "./modules/db"
target_node = "px"
vm_hostname = "app-01"
vm_hostname = "db"
ostemplate = "local:vztmpl/ubuntu-22.04-custome.tar.zst"
ssh_public_key = file("./ssh/id_terraform.pub")
private_key = file("./ssh/id_terraform")
ip_address = "10.10.10.3/24"
ip_address = "10.10.10.3"
gateway = "10.10.10.1"
bridge = "vmbr1"
storage = "local"
@ -18,18 +18,22 @@ module "app" {
swap = 512
}
}
module "apps" {
source = "./modules/lxc_dynamic"
}
target_node = "px"
ostemplate = "local:vztmpl/ubuntu-22.04-custome.tar.zst"
ssh_public_key = file("./ssh/id_terraform.pub")
private_key = file("./ssh/id_terraform")
gateway = "10.10.10.1"
bridge = "vmbr1"
storage = "local"
size = "8G"
module "app" {
source = "./modules/app"
target_node = "px"
vm_hostname = "app-01"
ostemplate = "local:vztmpl/ubuntu-22.04-custome.tar.zst"
ssh_public_key = file("./ssh/id_terraform.pub")
private_key = file("./ssh/id_terraform")
db_host = module.db.db_ip
ip_address = "10.10.10.4"
gateway = "10.10.10.1"
bridge = "vmbr1"
storage = "local"
size = "8G"
lxc_resources = {
cores = 2
@ -37,8 +41,4 @@ module "apps" {
swap = 512
}
containers = {
nginx01 = { ip = "10.10.10.5/24" }
nginx02 = { ip = "10.10.10.6/24" }
}
}

View File

@ -1,9 +1,11 @@
resource "proxmox_lxc" "app_container" {
resource "proxmox_lxc" "app" {
target_node = var.target_node
hostname = var.vm_hostname
ostemplate = var.ostemplate
password = "password"
depends_on = [var.db_host]
ssh_public_keys = var.ssh_public_key
cores = var.lxc_resources.cores
@ -18,13 +20,19 @@ resource "proxmox_lxc" "app_container" {
network {
name = "eth0"
bridge = var.bridge
ip = var.ip_address
ip = "${var.ip_address}/24"
gw = var.gateway
firewall = true
}
start = true
provisioner "remote-exec" {
inline = [
"echo Приложение подключится к БД ${var.db_host}"
]
}
connection {
type = "ssh"
host = var.ip_address

3
modules/app/outputs.tf Normal file
View File

@ -0,0 +1,3 @@
output "db_host" {
value = var.db_host
}

View File

@ -1,11 +1,14 @@
variable "target_node" {}
variable "vm_hostname" {}
variable "ostemplate" {}
variable "ssh_public_key" {}
variable "private_key" {}
variable "ip_address" {}
variable "gateway" {}
variable "bridge" {}
variable "storage" {}
variable "size" {}
variable "db_host" {}
variable "lxc_resources" {
type = object({
@ -14,10 +17,3 @@ variable "lxc_resources" {
swap = number
})
}
variable "containers" {
description = "Map of containers to create"
type = map(object({
ip = string
}))
}

View File

@ -1,8 +1,6 @@
resource "proxmox_lxc" "app_container" {
for_each = var.containers
resource "proxmox_lxc" "postgresql" {
target_node = var.target_node
hostname = each.key
hostname = var.vm_hostname
ostemplate = var.ostemplate
password = "password"
@ -20,7 +18,7 @@ resource "proxmox_lxc" "app_container" {
network {
name = "eth0"
bridge = var.bridge
ip = each.value.ip
ip = "${var.ip_address}/24"
gw = var.gateway
firewall = true
}
@ -29,11 +27,18 @@ resource "proxmox_lxc" "app_container" {
connection {
type = "ssh"
host = trimsuffix(each.value.ip, "/24")
host = var.ip_address
user = "root"
private_key = var.private_key
timeout = "2m"
}
provisioner "remote-exec" { # команды внутри
inline = [
"echo 'Контейнер запущен и готов'",
"apt-get update",
"apt-get install -y postgresql"
]
}
}

3
modules/db/outputs.tf Normal file
View File

@ -0,0 +1,3 @@
output "db_ip" {
value = proxmox_lxc.postgresql.network[0].ip
}

View File

@ -1,3 +0,0 @@
output "vm_hostname" {
value = proxmox_lxc.app_container.hostname
}

View File

@ -1,6 +0,0 @@
output "vm_hostnames" {
description = "Hostnames of created containers"
value = {
for name, container in proxmox_lxc.app_container : name => container.hostname
}
}

View File

@ -1,7 +1,7 @@
{
"version": 4,
"terraform_version": "1.14.6",
"serial": 7,
"serial": 21,
"lineage": "f4c291e8-3767-da71-b85c-0fdc56b7a316",
"outputs": {},
"resources": [
@ -9,7 +9,7 @@
"module": "module.app",
"mode": "managed",
"type": "proxmox_lxc",
"name": "app_container",
"name": "app",
"provider": "provider[\"registry.terraform.io/telmate/proxmox\"]",
"instances": [
{
@ -33,6 +33,108 @@
"hastate": "",
"hookscript": "",
"hostname": "app-01",
"id": "px/lxc/101",
"ignore_unpack_errors": false,
"lock": "",
"memory": 2048,
"mountpoint": [],
"nameserver": "",
"network": [
{
"bridge": "vmbr1",
"firewall": true,
"gw": "10.10.10.1",
"gw6": "",
"hwaddr": "BC:24:11:FE:43:63",
"id": 0,
"ip": "10.10.10.4/24",
"ip6": "",
"mtu": 0,
"name": "eth0",
"rate": 0,
"tag": 0,
"trunks": "",
"type": "veth"
}
],
"onboot": false,
"ostemplate": "local:vztmpl/ubuntu-22.04-custome.tar.zst",
"ostype": "ubuntu",
"password": "password",
"pool": null,
"protection": false,
"restore": false,
"rootfs": [
{
"acl": false,
"quota": false,
"replicate": false,
"ro": false,
"shared": false,
"size": "8G",
"storage": "local",
"volume": "local:101/vm-101-disk-0.raw"
}
],
"searchdomain": "",
"ssh_public_keys": "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPiX5bOEXfX3AvwstdAyYYHgSyGDF12NzOBCwfNPQVgo terraform@lxc\n",
"start": true,
"startup": "",
"swap": 512,
"tags": "",
"target_node": "px",
"template": false,
"timeouts": null,
"tty": 2,
"unique": false,
"unprivileged": false,
"unused": [],
"vmid": 101
},
"sensitive_attributes": [
[
{
"type": "get_attr",
"value": "password"
}
]
],
"identity_schema_version": 0,
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxMjAwMDAwMDAwMDAwLCJkZWZhdWx0IjoxMjAwMDAwMDAwMDAwLCJkZWxldGUiOjEyMDAwMDAwMDAwMDAsInJlYWQiOjEyMDAwMDAwMDAwMDAsInVwZGF0ZSI6MTIwMDAwMDAwMDAwMH19",
"dependencies": [
"module.db.proxmox_lxc.postgresql"
]
}
]
},
{
"module": "module.db",
"mode": "managed",
"type": "proxmox_lxc",
"name": "postgresql",
"provider": "provider[\"registry.terraform.io/telmate/proxmox\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"arch": "amd64",
"bwlimit": 0,
"clone": null,
"clone_storage": null,
"cmode": "tty",
"console": true,
"cores": 2,
"cpulimit": 0,
"cpuunits": 1024,
"current_node": "px",
"description": "",
"features": [],
"force": false,
"full": null,
"hagroup": "",
"hastate": "",
"hookscript": "",
"hostname": "db",
"id": "px/lxc/100",
"ignore_unpack_errors": false,
"lock": "",
@ -45,7 +147,7 @@
"firewall": true,
"gw": "10.10.10.1",
"gw6": "",
"hwaddr": "BC:24:11:ED:E9:01",
"hwaddr": "BC:24:11:C0:09:67",
"id": 0,
"ip": "10.10.10.3/24",
"ip6": "",
@ -103,197 +205,6 @@
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxMjAwMDAwMDAwMDAwLCJkZWZhdWx0IjoxMjAwMDAwMDAwMDAwLCJkZWxldGUiOjEyMDAwMDAwMDAwMDAsInJlYWQiOjEyMDAwMDAwMDAwMDAsInVwZGF0ZSI6MTIwMDAwMDAwMDAwMH19"
}
]
},
{
"module": "module.apps",
"mode": "managed",
"type": "proxmox_lxc",
"name": "app_container",
"provider": "provider[\"registry.terraform.io/telmate/proxmox\"]",
"instances": [
{
"index_key": "nginx01",
"schema_version": 0,
"attributes": {
"arch": "amd64",
"bwlimit": 0,
"clone": null,
"clone_storage": null,
"cmode": "tty",
"console": true,
"cores": 2,
"cpulimit": 0,
"cpuunits": 1024,
"current_node": "px",
"description": "",
"features": [],
"force": false,
"full": null,
"hagroup": "",
"hastate": "",
"hookscript": "",
"hostname": "nginx01",
"id": "px/lxc/102",
"ignore_unpack_errors": false,
"lock": "",
"memory": 2048,
"mountpoint": [],
"nameserver": "",
"network": [
{
"bridge": "vmbr1",
"firewall": true,
"gw": "10.10.10.1",
"gw6": "",
"hwaddr": "BC:24:11:C4:95:36",
"id": 0,
"ip": "10.10.10.5/24",
"ip6": "",
"mtu": 0,
"name": "eth0",
"rate": 0,
"tag": 0,
"trunks": "",
"type": "veth"
}
],
"onboot": false,
"ostemplate": "local:vztmpl/ubuntu-22.04-custome.tar.zst",
"ostype": "ubuntu",
"password": "password",
"pool": null,
"protection": false,
"restore": false,
"rootfs": [
{
"acl": false,
"quota": false,
"replicate": false,
"ro": false,
"shared": false,
"size": "8G",
"storage": "local",
"volume": "local:102/vm-102-disk-0.raw"
}
],
"searchdomain": "",
"ssh_public_keys": "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPiX5bOEXfX3AvwstdAyYYHgSyGDF12NzOBCwfNPQVgo terraform@lxc\n",
"start": true,
"startup": "",
"swap": 512,
"tags": "",
"target_node": "px",
"template": false,
"timeouts": null,
"tty": 2,
"unique": false,
"unprivileged": false,
"unused": [],
"vmid": 102
},
"sensitive_attributes": [
[
{
"type": "get_attr",
"value": "password"
}
]
],
"identity_schema_version": 0,
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxMjAwMDAwMDAwMDAwLCJkZWZhdWx0IjoxMjAwMDAwMDAwMDAwLCJkZWxldGUiOjEyMDAwMDAwMDAwMDAsInJlYWQiOjEyMDAwMDAwMDAwMDAsInVwZGF0ZSI6MTIwMDAwMDAwMDAwMH19"
},
{
"index_key": "nginx02",
"schema_version": 0,
"attributes": {
"arch": "amd64",
"bwlimit": 0,
"clone": null,
"clone_storage": null,
"cmode": "tty",
"console": true,
"cores": 2,
"cpulimit": 0,
"cpuunits": 1024,
"current_node": "px",
"description": "",
"features": [],
"force": false,
"full": null,
"hagroup": "",
"hastate": "",
"hookscript": "",
"hostname": "nginx02",
"id": "px/lxc/101",
"ignore_unpack_errors": false,
"lock": "",
"memory": 2048,
"mountpoint": [],
"nameserver": "",
"network": [
{
"bridge": "vmbr1",
"firewall": true,
"gw": "10.10.10.1",
"gw6": "",
"hwaddr": "BC:24:11:49:21:DB",
"id": 0,
"ip": "10.10.10.6/24",
"ip6": "",
"mtu": 0,
"name": "eth0",
"rate": 0,
"tag": 0,
"trunks": "",
"type": "veth"
}
],
"onboot": false,
"ostemplate": "local:vztmpl/ubuntu-22.04-custome.tar.zst",
"ostype": "ubuntu",
"password": "password",
"pool": null,
"protection": false,
"restore": false,
"rootfs": [
{
"acl": false,
"quota": false,
"replicate": false,
"ro": false,
"shared": false,
"size": "8G",
"storage": "local",
"volume": "local:101/vm-101-disk-0.raw"
}
],
"searchdomain": "",
"ssh_public_keys": "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPiX5bOEXfX3AvwstdAyYYHgSyGDF12NzOBCwfNPQVgo terraform@lxc\n",
"start": true,
"startup": "",
"swap": 512,
"tags": "",
"target_node": "px",
"template": false,
"timeouts": null,
"tty": 2,
"unique": false,
"unprivileged": false,
"unused": [],
"vmid": 101
},
"sensitive_attributes": [
[
{
"type": "get_attr",
"value": "password"
}
]
],
"identity_schema_version": 0,
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxMjAwMDAwMDAwMDAwLCJkZWZhdWx0IjoxMjAwMDAwMDAwMDAwLCJkZWxldGUiOjEyMDAwMDAwMDAwMDAsInJlYWQiOjEyMDAwMDAwMDAwMDAsInVwZGF0ZSI6MTIwMDAwMDAwMDAwMH19"
}
]
}
],
"check_results": null

View File

@ -1,7 +1,7 @@
{
"version": 4,
"terraform_version": "1.14.6",
"serial": 3,
"serial": 18,
"lineage": "f4c291e8-3767-da71-b85c-0fdc56b7a316",
"outputs": {},
"resources": [
@ -9,7 +9,7 @@
"module": "module.app",
"mode": "managed",
"type": "proxmox_lxc",
"name": "app_container",
"name": "app",
"provider": "provider[\"registry.terraform.io/telmate/proxmox\"]",
"instances": [
{
@ -33,6 +33,108 @@
"hastate": "",
"hookscript": "",
"hostname": "app-01",
"id": "px/lxc/101",
"ignore_unpack_errors": false,
"lock": "",
"memory": 2048,
"mountpoint": [],
"nameserver": "",
"network": [
{
"bridge": "vmbr1",
"firewall": true,
"gw": "10.10.10.1",
"gw6": "",
"hwaddr": "BC:24:11:D1:7E:AF",
"id": 0,
"ip": "10.10.10.4/24",
"ip6": "",
"mtu": 0,
"name": "eth0",
"rate": 0,
"tag": 0,
"trunks": "",
"type": "veth"
}
],
"onboot": false,
"ostemplate": "local:vztmpl/ubuntu-22.04-custome.tar.zst",
"ostype": "ubuntu",
"password": "password",
"pool": null,
"protection": false,
"restore": false,
"rootfs": [
{
"acl": false,
"quota": false,
"replicate": false,
"ro": false,
"shared": false,
"size": "8G",
"storage": "local",
"volume": "local:101/vm-101-disk-0.raw"
}
],
"searchdomain": "",
"ssh_public_keys": "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPiX5bOEXfX3AvwstdAyYYHgSyGDF12NzOBCwfNPQVgo terraform@lxc\n",
"start": true,
"startup": "",
"swap": 512,
"tags": "",
"target_node": "px",
"template": false,
"timeouts": null,
"tty": 2,
"unique": false,
"unprivileged": false,
"unused": [],
"vmid": 101
},
"sensitive_attributes": [
[
{
"type": "get_attr",
"value": "password"
}
]
],
"identity_schema_version": 0,
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxMjAwMDAwMDAwMDAwLCJkZWZhdWx0IjoxMjAwMDAwMDAwMDAwLCJkZWxldGUiOjEyMDAwMDAwMDAwMDAsInJlYWQiOjEyMDAwMDAwMDAwMDAsInVwZGF0ZSI6MTIwMDAwMDAwMDAwMH19",
"dependencies": [
"module.db.proxmox_lxc.postgresql"
]
}
]
},
{
"module": "module.db",
"mode": "managed",
"type": "proxmox_lxc",
"name": "postgresql",
"provider": "provider[\"registry.terraform.io/telmate/proxmox\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"arch": "amd64",
"bwlimit": 0,
"clone": null,
"clone_storage": null,
"cmode": "tty",
"console": true,
"cores": 2,
"cpulimit": 0,
"cpuunits": 1024,
"current_node": "px",
"description": "",
"features": [],
"force": false,
"full": null,
"hagroup": "",
"hastate": "",
"hookscript": "",
"hostname": "db",
"id": "px/lxc/100",
"ignore_unpack_errors": false,
"lock": "",
@ -45,7 +147,7 @@
"firewall": true,
"gw": "10.10.10.1",
"gw6": "",
"hwaddr": "BC:24:11:7F:7E:1B",
"hwaddr": "BC:24:11:80:08:AB",
"id": 0,
"ip": "10.10.10.3/24",
"ip6": "",

View File

@ -1 +1 @@
pm_token_secret = "e71efc34-8635-41fd-b8e9-019858b451a7"
pm_token_secret = "db7041aa-ced2-40d8-a5d5-7883f064210d"