Blog IndexPosts by TagHome

Packer with proxmox-clone

Posted <2021-08-07 Sat 13:49> by Aaron S. Jackson.

I recently started using packer for work and realised it would be just the tool I want at home for my Proxmox set up. I have a number of VMs running under proxmox for testing things out in slurm or hosting small services, but creating new VMs was not as programmatic as I'd like.

Packer has two builder plugins for proxmox, the so called "clone" plugin, which can clone a template or VM and apply some provisioning, and the so called "iso" plugin, which allows you to perform an installation from an ISO (which sounds annoying to script).

There were a few hoops to jump through to get this to work, but I'll start with the template I ended up with and then discuss some of the requirements later.

variable "PROXMOX_URL" {
  type = string
  default = env("PROXMOX_URL")
}

variable "PROXMOX_USERNAME" {
  type = string
  default = env("PROXMOX_USERNAME")
}

variable "PROXMOX_TOKEN" {
  type = string
  sensitive = true
  default = env("PROXMOX_TOKEN")
}

source "proxmox-clone" "centos" {
  proxmox_url = var.PROXMOX_URL
  username = var.PROXMOX_USERNAME
  token = var.PROXMOX_TOKEN

  insecure_skip_tls_verify = true
  node = "pve"

  vm_name = "packer-test"

  memory = 1024
  cores = 1
  os = "l26"

  clone_vm = "template-centos7"

  ssh_username = "root"

  task_timeout = "5m"
  ssh_timeout = "10m"

  scsi_controller = "virtio-scsi-pci"


  template_name = "packer-test"
  template_description = "I am testing packer"
}

build {
  sources = ["source.proxmox-clone.centos"]
  provisioner "shell" {
    inline = [
      "echo packer-test.nat.rhwyd.co.uk > /etc/hostname"
    ]
  }
}

First, you will need to create a base image - I've called mine "template-centos7", but any unique name should be fine as long as it matches in proxmox. There are two important things which must be installed on this base image:

The next problem was due to a timeout with the clone operation. I've sent a pull request to make this variable with the option task_timeout, but this doesn't exist in the public releases (yet)

Once those problems are out of the way, provisioning a new template is quite easy:

[aaron@carbon rhwyd-packer]$ packer build centos-base.pkr.hcl
proxmox-clone.centos: output will be in this color.

==> proxmox-clone.centos: Creating ephemeral key pair for SSH communicator...
==> proxmox-clone.centos: Created ephemeral SSH key pair for communicator
==> proxmox-clone.centos: Creating VM
==> proxmox-clone.centos: No VM ID given, getting next free from Proxmox
==> proxmox-clone.centos: Starting VM
==> proxmox-clone.centos: Waiting for SSH to become available...
==> proxmox-clone.centos: Connected to SSH!
==> proxmox-clone.centos: Provisioning with shell script: /tmp/packer-shell890782583
==> proxmox-clone.centos: Trying to remove ephemeral keys from authorized_keys files
==> proxmox-clone.centos: Stopping VM
==> proxmox-clone.centos: Converting VM to template
Build 'proxmox-clone.centos' finished after 1 minute 31 seconds.

==> Wait completed after 1 minute 31 seconds

==> Builds finished. The artifacts of successful builds are:
--> proxmox-clone.centos: A template was created: 115

Wanting to leave a comment?

Comments and feedback are welcome by email (aaron@nospam-aaronsplace.co.uk).

Related posts:

Tags: computers

Blog IndexPosts by TagHome

Copyright 2007-2024 Aaron S. Jackson (compiled: Mon 30 Sep 12:34:19 BST 2024)