Packer is an open source tool developed by HashiCorp that lets you create identical images using the same source. It helps in implementing and managing golden images across your organization. I will be using Packer in a vSphere environment only and not be using its multi platform support. The use case I am looking at is managing VM templates applying infrastructure as code concepts.
The workflow I am implementing is using base VM templates made of basic OS installation, VMware tools and networking connectivity. These base templates do not need any management except for periodic updates/patches. The base VMs then are customized into project specific templates using Packer. The process installs any given project customization such as additional users, software packages, devices and creates a new template to be used as the source for prod deployment. Packer will not replace a configuration management tool, but it will reduce the time to deploy and configure the prod (or running) instances. It is faster to have a prepped template than to wait for packages to install on each of your instances during prod deployment. The diagram below exemplifies the intended process:
- variables.pkr.hcl - keeps all variable definitions
- tmpl-linux.auto.pkrvars.hcl - keeps the initialized input variables and it will be loaded during run; this allows to only change this file when moving to another environment
- tmpl-linux.pkr.hcl - main Packer file
variable "vcenter_server" { type = string description = "FQDN or IP address of the vCenter Server instance" } variable "build_user" { type = string description = "user name for build account" } locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
local "linux_user_pass" { expression = vault("/kv/data/linux_workshop", "${var.ssh_user}") sensitive = true }
local "build_user_pass" { expression = vault("/kv/data/build_user", "${var.build_user}") sensitive = true }
vcenter_server = "vcsa.mylab.local" build_user = "build_user@vsphere.local"
- add a new disk to the target image
- install software packages in the target image
packer { required_version = ">= 1.8.5" required_plugins { vsphere = { version = ">= v1.1.1" source = "github.com/hashicorp/vsphere" } } }
source "vsphere-clone" "linux-vm-1" { # vcenter server connection vcenter_server = "${var.vcenter_server}" insecure_connection = "true" username = "${var.build_user}" password = local.build_user_pass # virtual infrastructure where we build the templates datacenter = "${var.datacenter}" host = "${var.vsphere_host}" datastore = "${var.datastore}" folder = "Templates/${var.lab_name}" # source template name template = "${var.src_vm_template}" # build process connectivity communicator = "ssh" ssh_username = "${var.ssh_user}" ssh_password = local.linux_user_pass # target image name and VM notes
vm_name = "tmpl-${var.lab_name}-${var.new_vm_template}" notes = "build with packer \n version ${local.timestamp} " # target image hardware changes disk_controller_type = ["pvscsi"] storage { disk_size = var.extra_disk_size disk_thin_provisioned = true disk_controller_index = 0 } convert_to_template = true }
build { sources = ["source.vsphere-clone.linux-vm-1"] provisioner "shell" { execute_command = "echo '${local.linux_user_pass}' | sudo -S sh -c '{{ .Vars }} {{ .Path }}'" inline = ["yum install tree htop -y"] } }
packer validate . packer build .