summaryrefslogtreecommitdiff
path: root/terraform/main.tf
blob: 9977fb45e88b3057132d1293c281686aa90358b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Provider

terraform {
  required_providers {
    proxmox = {
      source  = "bpg/proxmox"
      version = "~> 0.83"
    }
  }
}

provider "proxmox" {
  endpoint  = var.proxmox_endpoint
  api_token = var.proxmox_api_token

  insecure = true
}


# LXC Container

resource "proxmox_virtual_environment_container" "lxc01" {

  vm_id     = var.lxc_id
  node_name = var.node_name


  started   = true

  wait_for_ip {
      ipv4 = true
    }

  operating_system {
    template_file_id = "local:vztmpl/debian-12-standard_12.12-1_amd64.tar.zst"
  }


  cpu {
    cores = var.lxc_cores
  }

  memory {
    dedicated = var.lxc_memory
    swap      = 512
  }

  disk {
    datastore_id = "local-lvm"
    size         = 8
  }

  network_interface {
    name   = "eth0"
    bridge = "vmbr0"
    
  }

  initialization {
    hostname = var.lxc_name

    ip_config {
      ipv4 {
        address = "dhcp"
      }
    }

    user_account {
      keys = [
        "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHB4jD3PVsg1edlMwBW6Hb/NYLnzEI8dyJQRnQQap45q enrico@cachyos"
      ]
    }
  }
}


resource "local_file" "ansible_inventory" {
  content = <<-EOT
    [lxc]
    ${try(proxmox_virtual_environment_container.lxc01.ipv4["eth0"], "Brak_IP")} ansible_user=root ansible_ssh_common_args='-o StrictHostKeyChecking=no'
  EOT
  
  filename = "${path.module}/../ansible/inventory.ini"
}

output "aktualne_ip_kontenera" {
  description = "Adres IP pobrany przez Terraforma"
  value       = try(proxmox_virtual_environment_container.lxc01.ipv4["eth0"], "Proxmox jeszcze nie zaraportował IP!")
}