Skip to content

Instantly share code, notes, and snippets.

@githubfoam
Last active April 20, 2024 21:50
Show Gist options
  • Save githubfoam/5f665eb27439706c28650d380be80b32 to your computer and use it in GitHub Desktop.
Save githubfoam/5f665eb27439706c28650d380be80b32 to your computer and use it in GitHub Desktop.
packer command cheat sheet
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The ".hcl" file extension is used for HashiCorp Configuration Language (HCL) files, which are used to define infrastructure as code using HashiCorp tools such as Vagrant and Packer.
In the context of Vagrant, the "pkr.hcl" file is used to define how Packer builds machine images that can be used as base boxes for Vagrant. The "pkr.hcl" file specifies the builders (e.g. virtualization providers like VirtualBox, VMware), provisioners (e.g. shell scripts, Ansible, Chef), and post-processors (e.g. compressing the image, uploading to a cloud provider) that Packer should use to create the machine image
The "pkr.hcl" file can also include variables, which allow for more dynamic and flexible configuration. These variables can be set via the command line, a separate variables file, or even environment variables.
-----------------------------------------------------------------------------------------------------
#Windows 10, download packer,add to the PATH, set environment variable,restart cmd
setx /m PATH "C:\tmp\packer_1.7.8_windows_amd64;%PATH%" #set the system variable (elevated cmd)
-----------------------------------------------------------------------------------------------------
- echo "=========travis packer version===================================="
- packer version #1.3.3
- echo "============================================="
- sudo apt-get install -qqy unzip jq
- echo "============================================="
- echo "=============Installing packer latest - not travis version=============="
- PACKER_CURRENT_VERSION="$(curl -s https://checkpoint-api.hashicorp.com/v1/check/packer | jq -r -M '.current_version')"
- PACKER_URL="https://releases.hashicorp.com/packer/$PACKER_CURRENT_VERSION/packer_${PACKER_CURRENT_VERSION}_linux_amd64.zip"
- PACKER_SHA256_URL="https://releases.hashicorp.com/packer/$PACKER_CURRENT_VERSION/packer_${PACKER_CURRENT_VERSION}_SHA256SUMS"
- PACKER_SHA256_SIG_URL="https://releases.hashicorp.com/packer/$PACKER_CURRENT_VERSION/packer_${PACKER_CURRENT_VERSION}_SHA256SUMS.sig"
- curl -LO "${PACKER_URL}"
- curl -LO "${PACKER_SHA256_URL}"
- curl -LO "${PACKER_SHA256_SIG_URL}"
- HASHICORP_PUBLIC_KEY_URL="https://keybase.io/hashicorp/pgp_keys.asc" #https://www.hashicorp.com/security
- 'curl -sSL "${HASHICORP_PUBLIC_KEY_URL}" | gpg --import -' # import the public key (PGP key)
- gpg --verify "packer_${PACKER_CURRENT_VERSION}_SHA256SUMS.sig" "packer_${PACKER_CURRENT_VERSION}_SHA256SUMS" 2>/dev/null #Verify the signature file is untampered
- shasum -a 256 -c "packer_${PACKER_CURRENT_VERSION}_SHA256SUMS" | sudo tee output.txt # Verify the SHASUM matches the archive.
- cat output.txt | grep OK # print OK
- unzip "packer_${PACKER_CURRENT_VERSION}_linux_amd64.zip"
# - sudo cp packer /usr/bin #Packer v1.3.3, same as travis packer
- sudo cp packer /usr/local/bin #overriding travis packer
- echo "============================================="
- packer version #Packer v1.5.6
- echo "============================================="
-----------------------------------------------------------------------------------------------------
VirtualBox Builder (from an ISO)
https://www.packer.io/docs/builders/virtualbox-iso.html
Chapter 8. VBoxManage
https://www.virtualbox.org/manual/ch08.html#idm3756
Templating- JSON file
guest_os_type (string) -> View all available values for this run VBoxManage list ostypes
packer validate vagrantexample.json -> Checks the template is valid
packer build vagrantexample.json -> Execute multiple builds in parallel as defined in the template
packer build -only=virtualbox-iso ubuntu-16.04-amd64.json -> build a template only for a list of specific providers
packer build -except=parallels-iso,vmware-iso ubuntu-16.04-amd64.json -> build a template for all providers except a list of specific providers
packer build -var 'mirror=http://ftp.jaist.ac.jp/pub/Linux/debian-cdimage/release' ubuntu-16.04-amd64.json -> use an alternate mirror
-----------------------------------------------------------------------------------------------------
#Creating Vagrant Box
#file_name.pkr.hcl https://gist.github.com/githubfoam/184271fc83075b8cfa9fdd0b44a294e3
$ packer build file_name.pkr.hcl
$ vagrant init dev-env
$ vagrant box add dev-env output-vagrant/package.box
$ vagrant up
$ vagrant ssh
-----------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment