Home Packer
Post
Cancel

Packer

a little intro

So… It has been quite a while since I made a post for my website and you must be thinking.. BUT WHY JEFFREY! I NEED MY STUPID CONTENT FROM YOU… Said no one ever. Well I have been busy with a renovation from my appartment so that took quite allot of time and also your mind gets really chaotic if your environment is chaotic. So I was mostly busy with that and I also started a new job. Perfect timing… Iknow

But now I am back! And I thought this can possibly be a awesome subject to write a tiny blog about.

The environment and most of all.. WHY!?

So think about it. Have you ever deployed a VM and thought: Well this is allot of clicking… Why can’t I just press a button and be done with it? Well you can. If you have a template. And here comes the thing… I already had templates and they where working great with cloud init! But there it came… A new ubuntu release. CRAP I NEED TO REMAKE MY CLOUD INIT IMAGE AGAIN!!

Well here comes packer. With packer you can describe how you want your image. The download URL and even let it run a ansible playbook. Perfect! So here it comes.. The packer code, it is writen in HCL. The “Language” that Hashicorp developed for terraform and packer and so on.

By the way, GIANT shoutout too: Christian Lempa https://twitter.com/christian_tdl That guy provided me with the basis and I just used it further for my use case.


variable "proxmox_api_url" {
    type = string
}

variable "proxmox_api_token_id" {
    type = string
}

variable "proxmox_api_token_secret" {
    type = string
    sensitive = true

}

source "proxmox" "ub-srv-jam" {

# Connection Settings
proxmox_url = "${var.proxmox_api_url}"
username    = "${var.proxmox_api_token_id}"
token       = "${var.proxmox_api_token_secret}"
insecure_skip_tls_verify = true

# General VM
node = "odin"
vm_id = "9005"
vm_name = "ub-srv-jam"

iso_file = "mead-hall:iso/ubuntu-22.04.1-live-server-amd64.iso"

# you can also download the ISO
#iso_url = "https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-live-server-amd64.iso"
# Yes you need the checksum. You wil thank your self if it ever goes wrong
iso_checksum = "10f19c5b2b8d6db711582e0e27f5116296c34fe4b313ba45f9b201a5007056cb"
iso_storage_pool = "mead-hall"
unmount_iso = true

qemu_agent = true  
scsi_controller = "virtio-scsi-pci"

disks {
    disk_size = "20G"
    format = "raw" # This can be different for you. I use RAW since I use ZFS
    storage_pool = "hel"
    storage_pool_type = "lvm"
    type = "virtio"
}
cores = "2"
memory = "2048" # Memory in MB
network_adapters {
    model = "virtio"
    bridge = "vmbr0"
    firewall = "false"
}
cloud_init = true
cloud_init_storage_pool = "hel"

boot_command = [
    "<esc><wait>",
    "e<wait>",
    "<down><down><down><end>",
    "<bs><bs><bs><bs><wait>",
    "autoinstall ds=nocloud-net\\;s=http://:/ ---<wait>",
    "<f10><wait>"
]

boot = "c"
boot_wait = "5s"

# so these settings are for your host specific
http_directory = "http"
http_bind_address = "192.168.0.84" # Fill this in if you have multiple nics for example.
http_port_min = 8802
http_port_max = 8802

ssh_username = "jeffrey"
#ssh_password = ""
# I for now build from my Mac so I just point to my private key 
ssh_private_key_file = "~/.ssh/id_rsa"

ssh_timeout = "20m"

}

build {
name = "ub-srv-jam"
sources = ["source.proxmox.ub-srv-jam"]

provisioner "shell" {
    inline = [
        "while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
        "sudo rm /etc/ssh/ssh_host_*",
        "sudo truncate -s 0 /etc/machine-id",
        "sudo apt -y autoremove --purge",
        "sudo apt -y clean",
        "sudo apt -y autoclean",
        "sudo cloud-init clean",
        "sudo rm -f /etc/cloud/cloud.cfg.d/subiquity-disable-cloudinit-networking.cfg",
        "sudo sync"
    ]

}

provisioner "file" {
    source = "files/99-pve.cfg"
    destination = "/tmp/99-pve.cfg"
}

provisioner "shell" {
    inline = [ "sudo cp /tmp/99-pve.cfg /etc/cloud/cloud.cfg.d/99-pve.cfg" ]
}
provisioner "ansible" {
    playbook_file = "files/ansible/playbook.yml"
    }
}

Well most of it speaks for it self but you can go through it if you want. Most interesting for me was mostly that I can have cloud init already installed and a playbook also running afterwards with some basic config that I like to have more in ansible then cloud init or any shell scripts.

short post iknow

Well this was it for now. I will later on place a link to my gitlab as soon as I have everything ready. For now you can follow me on twitter or send me a message on twitter or linkedin!

This post is licensed under CC BY 4.0 by the author.