Skip to main content

Tạo VM với Terraform và Proxmox

Step 1: Tạo API Token

  • Login vào proxmox ve server 
  • Chọn Datacenter ở cột trái
  • Nhìn qua phải thì section Permissions > chọn API Token
  • Chọn Add để tạo Api Token
image.png
  • Add Token theo user root
  • Privilege Seperation: Unchecked (bỏ check)
  • Token Id: Điền tên terraform
  • Nhấn Add Button

image.png

Lưu lại các thông tin sau vì nó chỉ hiện một lần:

  • Token ID: root@pam!teraform
  • Secret: 8ac5928a-9f2f-49e0-90d4-7542f87b9361

image.png

Sau khi lưu xong token id và secret thì bạn mở webstorm hay vscode lên, tạo project trống:

  • Tạo file provider.tf
  • Install plugin hổ trợ terraform language

image.png

Tạo 2 file, provider.tf và credentials.auto.tfvars

file provider.tf

terraform {

  required_version = ">= 0.13.0"

  required_providers {
    proxmox = {
      source  = "telmate/proxmox"
      version = ">= 2.9.3"
    }
  }
}

variable "proxmox_api_url" {
  type = string
}

variable "proxmox_api_token_id" {
  type      = string
  sensitive = true
}

variable "proxmox_api_token_secret" {
  type      = string
  sensitive = true
}

provider "proxmox" {
  pm_api_url = var.proxmox_api_url
  pm_token_id = var.proxmox_api_token_id
  pm_token_secret = var.proxmox_api_token_secret
  pm_tls_insecure = true
}

file: credentials.auto.tfvars

proxmox_api_url = "https://gray.kyluat.lan:8006/api2/json"
proxmox_api_token_id = "root@pam!teraform2"
proxmox_api_token_secret = "8ac5928a-9f2f-49e0-90d4-7542f87b9361"

file: srv-demo-1.tf

resource "proxmox_vm_qemu" "srv_demo_1" {
  name        = "srv-demo-1"
  desc        = "Demo Server 1"
  vmid        = "201"
  target_node = "gray"
  agent       = 1

  clone   = "ubuntu-cloud"
  cores   = 2
  sockets = 1
  cpu     = "host"
  memory  = 2048

  network {
    model  = "virtio"
    bridge = "vmbr0"
  }

  disk {
    type = "virtio"
    storage = "local-lvm"
    size    = "2252M"
  }

  os_type = "cloud-init"
  ipconfig0 = "ip=dhcp"
  nameserver = "192.168.1.11"
  ciuser = "USERNAMEABC"
  sshkeys = <<EOF
    ssh-rsa AAAAB3NzaC1yc2EAAABKDJhkashdkahsdahdkhasd.35c2EAAABKDJhkashdkahsdahdkhasd.35c2EAAABKDJhkashdkahsdahdkhasd.35c2EAAABKDJhkashdkahsdahdkhasd.3590Y
  EOF
}