Skip to content

Instantly share code, notes, and snippets.

@f0rkz
Created May 13, 2019 03:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save f0rkz/392bd5f476790f3ebf1ee981bd0fb55b to your computer and use it in GitHub Desktop.
Save f0rkz/392bd5f476790f3ebf1ee981bd0fb55b to your computer and use it in GitHub Desktop.
Using Terraform and Proxmox

Purpose

A clear how-to with instruction to get proxmox virtual machines provisioned with Terraform.

Pre-requisites

You need go installed to compile the Telmate provider located here.

You additionally need terraform installed.

You need cloud-init installed on proxmox. This wiki page has a decent howto. Go ahead and also create a cloud-init image created.

Confirm the Installation

Once you have the Telmate provider installed, go ahead and ensure the terraform provider works by creating a main.tf file with the following content:

provider "proxmox" {
  pm_tls_insecure = true
}

Go ahead and run terraform init to make sure the proper plugins are installed (this also confirms the plugin works and you installed it correctly).

Flesh out the Provider

Now that you confirmed the plugin worked, get the provider connected to your proxmox instance.

There are a lot of lacking documentation of the features for this plugin. Unfortunately, that means you need to review the code of the provider and yank out the features available to you. Fortunately, I am going to cover enough to get an instance stood up utilizing cloud-init to log in to your instance.

Your provider block looks like this to get a connection to your proxmox instance:

provider "proxmox" {
  pm_tls_insecure = true
  pm_api_url      = "https://proxmox-host-ip-or-hostname:8006/api2/json"
  pm_user         = "root@pam"
  pm_password     = "some-password"
}

It is important to keep your secrets safe whenever checking into version control, so feel free to utilize terraform variables and use a tool like blackbox to gpg encrypt your secrets. I'll leave that decision up to you.

Now that you have your host configured, let's create a vm!

A full example looks like the following:

provider "proxmox" {
  pm_tls_insecure = true
  pm_api_url      = "https://proxmox-host-ip-or-hostname:8006/api2/json"
  pm_user         = "root@pam"
  pm_password     = "some-password"
}

resource "proxmox_vm_qemu" "cloudinit-test" {
  name        = "tf-test"
  desc        = "testing terraform proxmox plugin"
  target_node = "pve"
  clone       = "VM 9000"
  cores       = 2
  sockets     = 1
  memory      = 2048

  ipconfig0 = "gw=x.x.x.x,ip=y.y.y.y/24"
  ssh_user  = "ubuntu"

  sshkeys = <<EOF
ssh-rsa somekey user@host
EOF

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

  disk {
    id           = 0
    type         = "scsi"
    storage      = "local-zfs"
    storage_type = "zfspool"
    size         = "100"
    format       = "raw"
  }
}

Now that you have your values populated, you can run terraform plan and terraform apply.

If everything was followed properly, you now should have a host up and running reachable via ssh with the ubuntu user and your key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment