I’ve been looking at automating as much of my home lab that’s running on Proxmox as possible, not only to get things up and running quickly should a failure happen but also to keep my skills current with the various automation tools out there at the moment.

From my previous posts, you can see that I use Ansible a fair bit for my personal projects, but at work, being a C# developer and working for an organisation that is predominantly Microsoft based, we use Terraform by HashiCorp for infrastructure automation.

I’ve been keeping a close eye on a project by Telmate on Github. They have been working on the Terraform Proxmox Provider and a few days ago a commit was made to allow the creation of Linux Containers (LXC) in Proxmox as opposed to just the creation of Virtual Machines (VMs), so I was keen to give it a go.

Requirements

I’m using macOS Catalina as my development machine, so the instructions will be based on this platform, but the tools will be the same and installation may differ.

Go

Download and install Go from Go’s website. Or by a direct link to MacOS Go 1.13.3

Terraform Provider and Provisioner

Using Go, get the latest Terraform provider and provisioner for Proxmox.

These instructions differ slightly from those on the Terraform Proxmox Provider Github page, but they are what worked for me.

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

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

Copy them to the correct locations:

which terraform-provider-proxmox
~/go-workspace/bin/terraform-provider-proxmox
which terraform-provisioner-proxmox
~/go-workspace/bin/terraform-provisioner-proxmox 

cd ~/.terraform.d
mkdir plugins
cd plugins
cp ~/go-workspace/bin/terraform-provider-proxmox .
cp ~/go-workspace/bin/terraform-provisioner-proxmox .

Example of container creation

Using the example below, after updating your own values, a new container will be created, ready to be started.

I found it simple enough to create a new container using the Proxmox GUI and comparing the values with the resources in the example below and a little trial and error.

For a list of all the resources available that you can set, it’s worth having a read through of the resource_lxc.go code on Github.

provider "proxmox" {
    pm_tls_insecure = true
    pm_api_url = "https://proxmox.home.lan:8006/api2/json"
    pm_user = "terraform@pam"
    pm_password = "<secure password>"
}

resource "proxmox_lxc" "lxc-test" { 
    hostname = "lxc-test-host"
    cores = 1
    memory = "1024"
    swap = "2048"
    network {
        name = "eth0"
        bridge = "vmbr0"
        ip = "192.168.1.69/24"  
        firewall = true
    }
    ostemplate = "local:vztmpl/ubuntu-18.04-custom.tar.gz" 
    password = "<secure password>"
    pool = "terraform"
    rootfs = "local-lvm2:8" 
    storage = "local-lvm2"
    target_node = "proxmox"
    unprivileged = true
}

Run

terraform init
terraform plan
terraform apply

Related Posts