Proxmox and Terraform

Just some notes on the steps I took to use terraform with proxmox in case it helps anyone else.

Create a VM template

I created a dummy VM using CentOS 8 iso and then used the GUI to convert that VM to a template (which I named centos8-template). I gave the VM the id of 901 to keep it well out of the way. So the VM conf is stored at:

/etc/pve/qemu-server/901.conf

And the disk image is stored at:

/var/lib/vz/images/901

Upload the Centos CloudInit Image

CentOS 8 cloudinit images are downloadable here. Since proxmox doesn’t allow uploading of qcow images via the GUI, you can scp the qcow2 file and move to the folder. Or download directly:

cd /var/lib/vz/images/901
wget https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2

Point Template at the CloudInit Image, amending the VM template conf file editing the conf file:

/etc/pve/qemu-server/901.conf

And set the path to the root disk to:

scsi0: local:901/CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2,size=80G

Set the Root Disk Size

The Centos 8 CloudInit image is 10GB by default so to increase that for VMs created from the cloudinit image use qemu-img:

/var/lib/vz/images/901
qemu-img resize CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2 +80G

Add CloudInit Disk to VM Template by choosing: Hardware > Add > CloudInit Drive. Then visit the CloudInit menu option for the VM template and add the details you want used for CloudInit created VMs.

Terraform Provider

The instructions for the Telmate proxmox provider at the time of writing didn’t use instructions for terraform 13. On Mac I cloned the repo within my ./.go/src directory and ran the ‘go install’ instructions per the readme.

go install github.com/Telmate/terraform-provider-proxmox/cmd/terraform-provisioner-proxmox

Then ran make. However I then needed to copy the binaries from the ./bin directory to :

~/.terraform.d/plugins/local/provider/proxmox/1.0.0/darwin_amd64/

Then within my terraform code I added:

terraform {
  required_providers {
    proxmox = {
        source = "local/provider/proxmox"
        version = ">=1.0.0"
    }
  }
}

provider "proxmox" {
    pm_api_url = "https://111.222.111.222:8006/api2/json"
    pm_user = "root@pam"
    pm_password = "ChAnGeMe"
    pm_tls_insecure = "true"
}

And defined a new VM:

resource "proxmox_vm_qemu" "proxmox_vm" {
    count             = 1
    name              = "vm002"
    target_node       = "prox002"
    clone             = "centos8-template"
    os_type           = "cloud-init"
    cores             = 4
    sockets           = "1"
    cpu               = "host"
    memory            = 2048
    scsihw            = "virtio-scsi-pci"
    bootdisk          = "scsi0"
    disk {
        id              = 2
        size            = 2
        type            = "scsi"
        storage         = "local"
        storage_type    = "qcow2"
        iothread        = false
    }
    network {
        id              = 0
        model           = "virtio"
        bridge          = "vmbr0"
        macaddr         = "52:54:00:00:1A:1A"
    }
# Cloud Init Settings
    ipconfig0 = "ip=123.123.123.123/24,gw=123.123.0.1"
    ciuser = "firstuser"
    cipassword = "topsecret"
    sshkeys = <<EOF
${var.ssh_key}
EOF
}

Then I ran the terraform commands i.e.

terraform init
terraform validate
terraform plan
teraform apply

At this point the VM was ready for Ansible.