Use IBM Packer Plugin to Create Custom Images on IBM Cloud - VPC Infrastructure

3 min read

What is the IBM Packer Plugin, and how can you create a custom image on IBM Cloud - VPC Infrastructure?

Packer is HashiCorp's open-source tool for creating custom machine images from a single source configuration. This means users can customize an image with a pre-configured operating system and installed software for a specific use-case and then use it to quickly launch completely provisioned and configured instances.

What is the IBM Packer Plugin?

On the other hand, a Packer Plugin allows new functionality to be added to Packer without modifying the Packer core. The IBM Packer Plugin is a separate, standalone application; the core of Packer starts and communicates with it, and the plugin enables Packer to connect to and work with IBM Cloud resources. 

IBM Packer Plugin adds on two Builders — one for Classic and one for VPC. A Builder is a Packer component responsible for creating a machine image. First, the Builder reads in a Packer Template — a configuration file that defines the image you want to build and how to build it. From this configuration file, the Builder takes a source OS (Linux- or Windows-based) and provisions a Virtual Server Instance (VSI) on IBM Cloud. Then, the Builder installs software for a specific use-case and generates an image out of the VSI. This generated image can be reused to launch new completely provisioned and configured VSIs instances within IBM Cloud. 

Therefore, by using the IBM Packer Plugin, you can quickly create identical machine images from a single source configuration on both Classic and VPC infrastructure.

Builders

  • Classic: The classic builder supports the creation of custom images (.VHD) on IBM Cloud - Classic Infrastructure.
  • VPC: The vpc builder supports the creation of custom images on IBM Cloud - VPC Infrastructure.

IBM Packer Plugin workflow

IBM Packer Plugin workflow

Prerequisites 

Prepare your end to run the plugin:

  • Install Packer >= 1.7
  • Install Ansible >= 2.10, if Ansible is your preferred Provisioner (recommended)
  • Install Go >= 1.17, if you want to use manual installation
    • Environment variables for golang setup:
      export GOPATH=$HOME/go
      export GOROOT=/usr/local/go
      export PATH=$PATH:$GOPATH/bin:$GOROOT/bin
      export PACKERPATH=/usr/local/packer
      export PATH=$PATH:$PACKERPATH
    • For Windows image, install the Python package for winrm:
       pip3 install --ignore-installed "pywinrm>=0.2.2" --user

Usage

Create configuration files and folders

From the IBM Packer Plugin repo:

  • Create your preferred folder (i.e., mkdir $HOME/packer-plugin-ibmcloud/).
  • Create the .env file and set IBM Cloud Credentials. Also, set Packer and Ansible environment variables:
    # VPC
    export IBM_API_KEY=""
    # Classic
    export SL_USERNAME=""
    export SL_API_KEY=""
    
    export ANSIBLE_INVENTORY_FILE="provisioner/hosts"
    export ANSIBLE_HOST_KEY_CHECKING=False
    export PACKER_LOG=1
    export PACKER_LOG_PATH="packerlog/packerlog.txt"
    export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
  • Copy the Packer Templates examples folder: cp -r examples $HOME/packer-plugin-ibmcloud/. This folder will store Packer Template examples for different OS and Provisioners.
  • Copy the Windows-based VSI config scripts folder: cp -r scripts $HOME/packer-plugin-ibmcloud/. This folder will store required config scripts to setup WinRM communication with Windows VSIs.
  • Copy the Ansible playbooks folder: cp -r provisioner $HOME/packer-plugin-ibmcloud/. This folder will store Provisioner's scripts.
  • Create the Packer log folder (recall env variable PACKER_LOG_PATH) cp -r packerlog $HOME/packer-plugin-ibmcloud/.

Use the packer init command

Starting with version 1.7, Packer supports third-party plugin installation using the packer init command (read the Packer documentation for more information). packer init downloads the Packer Plugin binaries required in your Packer Template. To install a Packer Plugin, just copy and paste the required_plugins block inside your Packer Template:

packer {
    required_plugins {
        ibmcloud = {
            version = ">=v3.0.0"
            source = "github.com/IBM/ibmcloud"
        }
    }
}

Then, run packer init -upgrade <packer-template-name>.

Note: Be aware that packer init does not work with legacy JSON templates. Upgrade your JSON config files to HCL. The IBM Packer plugin will be installed on $HOME/.packer.d/plugins.

Run the Packer Plugin

  • Run the source command to read and execute commands from the .env file: source .env.
  • Finally, run the Packer plugin commands:
    • packer validate examples/build.vpc.centos.pkr.hcl
    • packer build examples/build.vpc.centos.pkr.hcl

Conclusion

Packer Plugins allow new functionality to be added to Packer without modifying the Packer core. The IBM Packer Plugin is a completely separate, standalone application that can be used to create custom images on IBM Cloud - VPC Infrastructure and then use those images to quickly launch completely provisioned and configured instances. Moreover, the automation to use this tool will help you reduce the amount of time spent on manual processes and configuration and let you use IBM Packer Plugin right away.

If you have feedback, suggestions or questions about this post, please reach out to me here

Be the first to hear about news, product updates, and innovation from IBM Cloud