Is there any way to provision bare-metal with Packer?

Can Packer be used to install and provision a bare metal server? Packer provides webserver with repository packages and preseed/kickstart and can run some other provision softwares(ansible,puppet, chef, etc). Could it be used to install bare metal servers? If yes, how should a packer .json look like?


Solution 1:

I had a similar question. I found this issue thread on using Packer with PXE boot.

https://github.com/hashicorp/packer/issues/955

From the thread, Vasiliy Tolstov commented:

[It's] very simple: install all needed stuff inside a VM (for Debian live-boot, for fedora/centos Dracut with the ability to boot from the net). after [building] that [with] packer run the following script (example for Debian):

#!/bin/sh -ex

apt-get -y install squashfs-tools

mkdir -p /mnt/squashfs /squashfs
mount -o bind / /mnt/squashfs

mksquashfs /mnt/squashfs /squashfs/filesystem.squashfs -comp gzip -no-exports -xattrs -noappend -no-recovery -e 
/mnt/squashfs/squashfs/filesystem.squashfs
find /boot -name 'vmlinuz-*' -type f -exec cp {} /squashfs/vmlinuz \;
find /boot -name 'init*' -type f -exec cp {} /squashfs/initrd.img \;

and in packer download artifacts from vm:

{
  "type": "file",
  "direction": "download",
  "sources": [
    "/squashfs/vmlinuz"
  ],
  "destination": "output/{{user `name`}}-squashfs/{{user `name`}}.vmlinuz"
},
{
  "type": "file",
  "direction": "download",
  "sources": [
    "/squashfs/initrd.img"
  ],
  "destination": "output/{{user `name`}}-squashfs/{{user `name`}}.initrd"
},
{
  "type": "file",
  "direction": "download",
  "sources": [
    "/squashfs/filesystem.squashfs"
  ],
  "destination": "output/{{user `name`}}-squashfs/{{user `name`}}.squashfs"
}