Using Packer to Create Openstack Images
This tutorial will help a cloud administrator setup a machine to use Packer, from Hashicorp, to create VM operating system images for an Openstack environment. Packer uses three steps to create an image: * Build * Provision * Post-Process
We recommend you setup a VM that can access the openstack environment or use the deployment server. You will also need to be able to SSH to the network selected for the image build during the Provisioning step.
For this tutorial you will need a server that can connect to the Openstack Environment that has Packer installed Install Packer, and an Openstack cloud with Base images installed.
Many of the commands to gather the required information will be done from the Openstack CLI
- First step is to discover the UUID of the base image you want to start with. In this example we will be using
ba8d9391-eb7f-44b2-94b7-5ee38fc31ace | Ubuntu 22.04
- Next is to grab the UUID of the network that will be attached to the VM
ecdb8ecf-e91b-42d4-ac52-f0120b937273 | ext-net
- Finally get the flavor name, you will not need a large flavor as we are just spinning up a vm and customising the image. In our example we will use
tiny-2
with 1 vCPU core and 2GB of RAM. - Create the packer JSON file with the Openstack Builder components
-
Optional items added are
ssh_timeout
to extend the default 5 minute timeout for the network to be reachable, and block storage related options.{ "builders":[{ "type": "openstack", "ssh_username": "ubuntu", "image_name": "Ubuntu 22.04 Ansible Docker Base", "source_image": "ba8d9391-eb7f-44b2-94b7-5ee38fc31ace", "flavor": "tiny-2", "networks": "ecdb8ecf-e91b-42d4-ac52-f0120b937273", "ssh_timeout": "10m", "use_blockstorage_volume": "true", "volume_type": "Breqwatr", "volume_size": "30" }] }
6. Now we can add provisioners to customize our image further, in our example we will be running OS updates and installing custom packages for Docker, Python3, pip and installing Ansible.{ "builders":[{ "type": "openstack", "ssh_username": "ubuntu", "image_name": "Ubuntu 22.04 Ansible Docker Base", "source_image": "ba8d9391-eb7f-44b2-94b7-5ee38fc31ace", "flavor": "tiny-2", "networks": "ecdb8ecf-e91b-42d4-ac52-f0120b937273", "ssh_timeout": "10m", "use_blockstorage_volume": "true", "volume_type": "Breqwatr", "volume_size": "30" }], "provisioners":[{ "type": "shell", "inline": [ "sleep 10", "sudo apt-get update", "sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin", "sudo apt-get install -y python3 python3-pip", "sudo pip install ansible" ] }] }
-
Our Packer file is now complete, once you have sourced into your Openstack openrc file you can initiate the image build with the packer build command
packer build ubuntu22.04_docker_ansible.json
-
Once the build is complete it will clean up all the temporary resources used to create the image and your new image should be available to use.