Giter VIP home page Giter VIP logo

terraform-module-proxmox-lxc's Introduction

Hi there ๐Ÿ‘‹

terraform-module-proxmox-lxc's People

Contributors

dacodev avatar deepsourcebot avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

john-schreiber

terraform-module-proxmox-lxc's Issues

using for_each instead of this module?

Hi,
I tried your module out and it helped me understand Terraform modules. So thank you.
As I got further along I realized I could just use for_each and locals instead.
Have you considered doing that? Perhaps I was using this module for a different purpose than you intended.
https://developer.hashicorp.com/terraform/language/meta-arguments/for_each

Here's my example of creating 2 containers using for_each with the out of the box bpg/proxmox provider:

terraform {
  required_providers {
    proxmox = {
      source = "bpg/proxmox"
    }
  }
}

locals {
  container_template_file     = "http://download.proxmox.com/images/system/debian-12-standard_12.2-1_amd64.tar.zst"
  container_template_checksum = "761b3b52ba296c3e396b79f89bdb30fed121d2353a641f84d633a87897ed6d19"
  container_distro            = "debian"
  network_bridge              = "vmbr0"
  node_name                   = "proxmox1"
  containers = {
    "test1" = {
      ram_MiB                 = 4096
      swap_MiB                = 512
      vm_id                   = 1000
      cpu_cores               = 2
      container_template_id   = proxmox_virtual_environment_file.container_template.id
    }
    "test2" = {
      ram_MiB                 = 8192
      swap_MiB                = 0
      vm_id                   = 1001
      cpu_cores               = 4
      container_template_id   = proxmox_virtual_environment_file.container_template.id
    }
  }
}

resource "proxmox_virtual_environment_file" "container_template" {
  content_type = "vztmpl"
  datastore_id = "local"
  node_name    = "proxmox1"
  source_file {
    path     = local.container_template_file
    insecure = true
    checksum = local.container_template_checksum
  }
}

resource "proxmox_virtual_environment_container" "proxmox_lxc" {
  description = "Managed by Terraform"
  for_each = local.containers

  node_name   = local.node_name
  vm_id       = each.value.vm_id
  unprivileged  = true

  initialization {
    hostname = each.key
    ip_config {
      ipv4 {
        address = "dhcp"
      }
    }

    user_account {
      keys = [
        trimspace(tls_private_key.lxc_container_key.public_key_openssh)
      ]
      password = random_password.lxc_password.result
    }
  }

  cpu {
    architecture = "amd64"
    cores        = each.value.cpu_cores
  }

  memory {
    dedicated = each.value.ram_MiB
    swap      = each.value.swap_MiB
  }

  disk {
    datastore_id = "local-zfs"
    size         = "10"
  }

  network_interface {
    name   = "veth0"
    bridge = local.network_bridge
  }

  operating_system {
    template_file_id = proxmox_virtual_environment_file.container_template.id
    type             = local.container_distro
  }
}

resource "random_password" "lxc_password" {
  length           = 16
  override_special = "_%@"
  special          = true
}

resource "tls_private_key" "lxc_container_key" {
  algorithm = "RSA"
  rsa_bits  = 2048
}

output "container_password" {
  value     = random_password.lxc_password.result
  sensitive = true
}

output "container_private_key" {
  value     = tls_private_key.lxc_container_key.private_key_pem
  sensitive = true
}

output "container_public_key" {
  value = tls_private_key.lxc_container_key.public_key_openssh
}

Multiple calls of the module with the same 'container_template_file' results in conflict

module "proxmox-lxc-debian-remote" {
  source                  = "DacoDev/proxmox-lxc/module"
  container_name          = "debian"
  container_template_file = "http://download.proxmox.com/images/system/debian-12-standard_12.2-1_amd64.tar.zst"
  cpu_cores               = 2
  distro                  = "debian"
  node_name               = "test"
  memory_dedicated        = 512
  vm_id                   = 1001
  disk_size               = 20
}

module "proxmox-lxc-debian-remote2" {
  source                  = "DacoDev/proxmox-lxc/module"
  container_name          = "debian"
  container_template_file = "http://download.proxmox.com/images/system/debian-12-standard_12.2-1_amd64.tar.zst"
  cpu_cores               = 2
  distro                  = "debian"
  node_name               = "test"
  memory_dedicated        = 512
  vm_id                   = 1002
  disk_size               = 20
}

results in:

Error: File already exists in a datastore, it was created outside of Terraform or is managed by another resource.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.