Location>code7788 >text

Packer builds openStack images

Popularity:433 ℃/2024-07-21 19:47:13

catalogs
  • Using Packer to automate image builds

Using Packer to automate image builds

openstack plugin installation:OpenStack | Integrations | Packer | HashiCorp Developer
Examples of openstack plugin usage:OpenStack Builder | Integrations | Packer | HashiCorp Developer

Installing the openstack plugin

packer plugins install /hashicorp/openstack

Preparation of template documentsas below

packer {
  required_plugins {
    openstack = {
      version = "~> 1"
      source  = "/hashicorp/openstack"
    }
  }
}

source "openstack" "example" {
  domain_name                 = "default"
  flavor                      = ""
  identity_endpoint           = "http://192.168.200.150:5000/v3"
  image_name                  = "openEuler-22.03-sp4.qcow2"
  insecure                    = true
  password                    = "JBJGuIpffaGMJDtvMXRrmF1qET4KMZseR0Ihyfil"
  region                      = "RegionOne"
  source_image_name           = "openEuler-22.03-LTS-SP4-x86_64.qcow2"
  networks                    = ["289df86a-6780-4563-a806-9f365ec86812"]
  floating_ip_network         = "public1"
  ssh_username                = "root"
  ssh_password	              = "openEuler12#$"
  tenant_name                 = "admin"
  username                    = "admin"
  use_blockstorage_volume     = "true"
  volume_size                 = "40"
  image_disk_format           = "qcow2"
}

build {
  sources = [""]
  provisioner "shell" {
    inline = [
      "echo Build image work is starting",
      "yum install vim bash-comp* -y",
      "echo 123 | passwd --stdin root",
      "echo execute successful"
    ]
  }
}

Execute the build

packer build ./

Description of commonly used configuration items:

  • domain_name: openstack's domain, default is default
  • flavor: Specifies the specifications for creating the virtual machine
  • identity_endpoint: specifies the authentication address of the keystone
  • image_name: name of the image generated using Packer
  • tenant_name: tenant name, which is the project name in openstack
  • insecure: Whether or not you are using an insecure connection, i.e. http and https.
  • password: password of the openstack user
  • username: openstack username
  • networks: Specifies the network on which the virtual machine is created.
  • source_image_name: Use a local image file to create the virtual machine
    • external_source_image_name: use an external image file to create, the address is a url
    • source_image: create a VM using the image in the glance
  • floating_ip_network: Specify the network of the floating IP, i.e., the external network in openstack, you need to configure the virtual machine with a floating IP, otherwise the packer can't use ssh to connect to the virtual machine.
  • ssh_username : the username to connect using ssh after starting the virtual machine
  • ssh_password : optional parameter, specifies the password for ssh connection, without this parameter packer will create a temporary key pair, ssh will use the key pair to connect to the
  • use_blockstorage_volume: whether to use block storage.
  • image_disk_format: specifies the format of the saved image, only if theuse_blockstorage_volumewill only take effect if it is true
  • volume_size :Specify the block storage size

Extra note, I am using the official openEuler provided by the qcow2 file, and the version is 22.03-sp4, can only be sp4, because sp3 default is not allowed to root user to log in, only allows ordinary users openeuelr login, but this user does not have the ability to sudo, and there is no cloud-init, so Can not do a lot of configuration, but sp4 this version of his default open the root password login privileges, so we can do some customized operations, other operating systems do not have this problem!

Waiting for the packer to finish building the image will leave you with an image in the glance

packer will prompt for a mirror ID of 2db14ce1......, and when he's done saving we'll go check the

We can see from the dashboard that the image has been saved and is in the format qcow2, and we only need to start the VM from this image, which will have a default password of 123 and will already have vim installed.