Using Packer to modify source images – DevOps Automation Tools – Terraform, Ansible, Packer, and More

Share this post on:

Using Packer to modify source images
Packer is a tool used for automating the creation of machine images. In this recipe, we’re going to use Packer to reference an Oracle Linux 8 platform image as its source, install something using a provisioner, and push up a new OCID image with these changes onto OCI.
NOTE
Although OCI features a free tier that contains “Always Free” resources, this does not include the ability to store images. If you wish to follow along with this recipe, you will need to use a paid account.
Getting ready
For this recipe, you will need the following:
• Oracle Linux
• Packer
Refer to the Technical requirements section at the beginning of this chapter if you need help installing Packer.
How to do it…
Packer is often overlooked because it seems so simple on the surface; however, don’t let this simplicity fool you – Packer is incredibly powerful and useful. In this recipe, we’re going to use Packer to reference some release of Oracle Linux 8 as its base image, install something using a provisioner, and push up a new OCID image with these changes onto the OCI:

  1. First, we will specify a Packer plugin that provides a builder called oracle-oci, which enables Packer to create machine images for OCI. We can do that in a file named oracle-oci.pkr.hcl. Input the following at the beginning of the file:

Figure 8.26 – Packer OCI builder plugin

  1. Next, we will configure our authentication to OCI using the configuration options specified by the oracle-oci builder. Details on configuring the oracle-oci builder can be reviewed at https://developer.hashicorp.com/packer/plugins/builders/oracle/oci:
    • There are numerous ways to do this. One way is to reference variables for the authentication details directly in your *.pkr.hcl file, like this:

Figure 8.27 – Referencing variables in Packer
• Then, we need to declare all of those variables in a file named variables.pkr.hcl. In this file, we declare the variable and specify the type, as well as providing a description to explain the purpose of the variable. Input the following into your variables.pkr.hcl file:

Figure 8.28 – Declaration of variables in Packer
• Finally, we want to assign values to the variables. We place them in a file named variables.auto.pkrvars.hcl. It should look something like this:

Figure 8.29 – Assigning variables in Packer
• Another way is to configure your OCI CLI in ~/.oci/config. That would look something like this:

[DEFAULT]
user=ocid1.user.oc1..
fingerprint=
key_file=~/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.oc1..
region=us-ashburn-1
[ADMIN_USER]
user=ocid1.user.oc1..
fingerprint=
key_file=keys/admin_key.pem
pass_phrase=


NOTE
Full details on configuring your OCI CLI can be found here: https://docs.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm

  1. Once you have the authentication configured, you can start by defining the base image. This should look something like the following:

Figure 8.30 – Defining the base image in Packer
NOTE
To get the OCID for Oracle Linux 8, visit the following URL: https://docs.oracle.com/en-us/iaas/images/oracle-linux-8x/. This is region specific as well as architecture specific, so you’ll want to find the latest Oracle Linux 8 image for x86 (or in other words, look for one without aarch64 mentioned in the image name).

  1. Once you’ve defined the source, you can move on to instructing Packer on what to do during the build process. In this case, we’ll call a provisioner so we can run a shell command to install Git:

Figure 8.31 – Packer provisioners

  1. At this point, we’ve told Packer about what we want to use for our source and described what we want to do for our build. Your entire file should look something like this:

Figure 8.32 – Complete Packer file for modifying base images


NOTE
In the final file, I replaced the word example with ol8u8 throughout to be a bit more descriptive about what I’m working with. This is simply a good form of self-documentation.

  1. Next, we’re going to run packer init to download the external plugin:

$
packer init .

  1. Finally, let’s go ahead and build the OCID image:

$ packer build .
Packer will spin up a new Oracle Linux 8.8 instance in the Oracle Cloud. Once the machine is running, it will run sudo dnf install -y git to install Git on the instance.

Figure 8.33 – Initial output after running packer build
Once Git is installed, it will take a snapshot of the instance and export this as a new OCID image, giving us a new starting point for all future instances.

Figure 8.34 – Output as Packer completes the build process
When Packer is finished, you can view the newly published image by navigating to the Compute | Custom Images on the OCI Console. Here’s an example of what that looks like:

Figure 8.35 – Viewing the recently published custom image on OCI
The source code for this recipe can be found at https://github.com/PacktPublishing/Oracle-Linux-Cookbook/tree/main/ch8/packer-cloud.

Share this post on:

Author: Stacy Atkins Prince

View all posts by Stacy Atkins Prince >

Leave a Reply

Your email address will not be published. Required fields are marked *