Posted on

Building a Template Image using Packer and Ansible

In this guide, we will walk through the process of building template images using Packer and Ansible. We will be using a pre-configured repository that contains all the necessary files and configurations to streamline the build process. By the end of this guide, you will have a solid understanding of how to build images for different providers using Packer and Ansible.

Prerequisites

Before we begin, ensure that you have the following software and requirements met:

sudo apt install zfsutils-linux

Note: For Bhyve Images, This setup could also work for other operating systems that support ZFS, VirtualBox, Packer, and Ansible.

Step 1: Clone the Repository

  1. Open a terminal or command prompt.
  2. Navigate to the directory where you want to clone the repository.
  3. Run the following command to clone the repository with submodules:
    git clone --recursive https://github.com/STARTcloud/vagrant_box_template_creator
  4. Change into the cloned repository directory:
    cd vagrant_box_template_creator/builder

Step 2: Configuration

  1. Navigate to the definitions folder:
    cd definitions
  2. Create a cloud-credentials.json file based on the cloud-credentials-example.json file:
    cp cloud-credentials-example.json cloud-credentials.json
  3. Open the cloud-credentials.json file in a text editor and fill in the necessary secrets for pushing images to cloud repositories. Replace the placeholder values with your actual credentials.
  4. Review the vendor.json file and update it with your organization-specific details, such as product URL, vendor URL, vendor name, and vendor domain.
  5. Choose the desired operating system template from the definitions/templates folder. In this example, we’ll use debian12-server.json for Debian 12.

Step 3: Building the Base Image

  1. Open a terminal and navigate to the root directory of the cloned repo:
    cd vagrant_box_template_creator/builder
  2. Run the following command to build the base image using Packer and Ansible-Local:

    packer build -var-file='definitions/cloud-credentials.json' -var-file='definitions/vendor.json' -var-file='definitions/templates/x64/debian12-server.json' tasks/build-ansible-local.json

    This command uses the build-ansible-local.json file as the main Packer build file and incorporates variables from debian12-server.json, vendor.json, and cloud-credentials.json.
  3. Packer will start the build process and use VirtualBox to create the base image. The build process will take some time, and you can monitor the progress in the terminal.
  4. Once the build is complete, the base image will be stored as an OVA file in the temp directory.

Step 4: Creating Provider-Specific Images and Uploading to Vagrant Cloud

After building the base image, you can convert it to other formats for different providers and upload them to BoxVault (or Vagrant Cloud) using the following commands:

VirtualBox

packer build -var-file='definitions/cloud-credentials.json' -var-file='definitions/vendor.json' -var-file='definitions/templates/x64/debian12-server.json' providers/virtualbox/publish.json

This command will create a VirtualBox-compatible image using the publish.json file in the providers/virtualbox folder. The resulting image will be stored in the providers/virtualbox/boxes folder and uploaded to Vagrant Cloud.

Zone (Bhyve)

packer build -var-file='definitions/cloud-credentials.json' -var-file='definitions/vendor.json' -var-file='definitions/templates/x64/debian12-server.json' providers/zones/publish.json

This command will create a Zone (Bhyve)-compatible image using the publish.json file in the providers/zones folder. The resulting image will be stored in the providers/zones/boxes folder and uploaded to Vagrant Cloud.

AMI (Amazon Machine Image)

packer build -var-file='definitions/cloud-credentials.json' -var-file='definitions/vendor.json' -var-file='definitions/templates/x64/debian12-server.json' providers/ami/publish.json

This command will create an AMI (Amazon Machine Image) using the publish.json file in the providers/ami folder. The resulting image will be stored in the providers/ami/boxes folder and uploaded to Vagrant Cloud.

Docker

packer build -var-file='definitions/cloud-credentials.json' -var-file='definitions/vendor.json' -var-file='definitions/templates/x64/debian12-server.json' providers/docker/publish.json

This command will create a Docker image using the publish.json file in the providers/docker folder. The resulting image will be stored in the providers/docker/boxes folder and uploaded to Vagrant Cloud.

Accessing Images on Vagrant Cloud

Once the images are uploaded, you can find them under your respective organization on Vagrant Cloud. For example, the Debian 12 server image can be accessed at:

https://portal.cloud.hashicorp.com/vagrant/discover/STARTcloud/debian12-server

Each of these commands uses the respective publish.json file located in the providers folder to build, publish, and upload the image for the specific provider. The publish.json files contain the necessary configuration and provisioning steps for each provider, including the upload to Vagrant Cloud.

Step 5: Customizing the Build Process

The build process can be customized and extended to fit your specific requirements. Here are a few key areas you can explore:

Ansible Playbooks

  • The Ansible playbooks used for provisioning the image are located in the provisioners/ansible/playbooks folder.
  • The main playbook for building the image with Ansible-Local is build-ansible-local-playbook.yml.
  • You can customize the playbook and roles to add additional provisioning steps or modify the existing configuration.

Preseed and Shell Scripts

  • Preseed files for different operating systems and types (server/desktop) are located in the provisioners/preseed folder.
  • Shell scripts for various provisioning tasks are located in the provisioners/shell folder.
  • You can modify or add new scripts to perform additional provisioning tasks specific to your needs.

Temporary Files and Output

  • During the build process, temporary files and output images are stored in the temp folder.
  • The final built images for each provider can be found in their respective boxes folders under the providers folder.

Step 6: Cleaning Up

After the build process is complete and you have obtained the desired images, you can clean up the temporary files and artifacts by running the following command:

packer build -var-file='definitions/cloud-credentials.json' -var-file='definitions/vendor.json' -var-file='definitions/templates/x64/debian12-server.json' tasks/cleanup.json

This command uses the cleanup.json file in the tasks folder to remove the temporary files and artifacts generated during the build process.

Conclusion

Congratulations! You have now learned how to build Packer images using Packer and Ansible based on the provided setup. You can use this knowledge to create custom images for different providers and automate the provisioning process.

Remember to review and update the configuration files, credentials, and templates according to your specific needs. Feel free to explore the different folders and files to gain a deeper understanding of the build process and make any necessary modifications.

If you encounter any issues or have further questions, refer to the official documentation of Packer and Ansible for more information and troubleshooting steps.

Happy building!