Nous disposons désormais sur notre machine de déploiement à la fois la clé publique et la clé privée.

main.tf

terraform {
  required_providers {
    proxmox = {
      source = "bpg/proxmox"
      version = "0.42.0"
    }
    ansible = {
      source  = "ansible/ansible"
      version = "~> 1.3.0"
    }
  }
}
 
provider "proxmox" {
  endpoint = "https://192.168.1.7:8006/"
  api_token = var.api_token
  insecure = true
  ssh {
    agent = true
    username = "root"
  }
}
 
resource "ansible_group" "lxc_containers" {
  name = "lxc_containers"
}
 
resource "ansible_host" "lxc_container" {
  count  = 3
  name   = "${var.vm_hostname}-${count.index + 1}"
  groups = [ansible_group.lxc_containers.name]
  variables = {
    ansible_host = "192.168.1.${201 + count.index}"
    ansible_user = "root"  # or the user you've configured in your LXC containers
  }
}
 
resource "proxmox_virtual_environment_container" "lxc_container" {
  count = 3
  description = "Managed by Terraform"
  # node_name = var.target_node
  node_name = "geekr9pve"
  vm_id = 241201 + count.index
 
  initialization {
    # hostname = var.vm_hostname
    hostname = "${var.vm_hostname}-${count.index + 1}"
    
    ip_config {
      ipv4 {
        # address = "dhcp"
        address = "192.168.1.${201 + count.index}/24"
        gateway = var.gateway
      }
    }
    
    user_account {
      # password = var.user_password
      keys = [var.ssh_public_key]
    }
  }
 
  network_interface {
    name = "eth0"
    bridge = "vmbr0"
  }
 
  operating_system {
    template_file_id = var.template_file_id
    type = "ubuntu"
  }
 
  cpu {
    cores = var.cores
  }
 
  memory {
    dedicated = var.memory
  }
 
  disk {
    datastore_id = var.disk.storage
    size         = var.disk.size
  }
 
  features {
    nesting = true
  }
 
  start_on_boot = var.onboot
  unprivileged = true
}

terraform.tfvars

ssh_public_key = ""
# target_node = "geekr9pve"
vm_hostname = "ubuntu-lts"
cores = 2
memory = 2048
disk = {
  storage = "local-lvm"
  size = "20"  # 20GB
}