From f812fefba54372cf9eb87506b7551c8505a1597a Mon Sep 17 00:00:00 2001 From: AJ Schroeder <6432150+ajschroeder@users.noreply.github.com> Date: Sat, 29 Jun 2024 10:36:57 -0500 Subject: [PATCH] feat: reworked cloud-init, UEFI boot, and ansible provisioner --- builds/linux/debian/12/linux-debian.pkr.hcl | 96 +++++++++++++++------ 1 file changed, 71 insertions(+), 25 deletions(-) diff --git a/builds/linux/debian/12/linux-debian.pkr.hcl b/builds/linux/debian/12/linux-debian.pkr.hcl index abda7a0..6c300af 100644 --- a/builds/linux/debian/12/linux-debian.pkr.hcl +++ b/builds/linux/debian/12/linux-debian.pkr.hcl @@ -32,11 +32,53 @@ data "git-repository" "cwd" {} // BLOCK: locals // Defines the local variables. -locals { +locals { + + bios_boot_command = [ + "", + "/install.amd/vmlinuz ", + "initrd=/install.amd/initrd.gz ", + "auto=true ", + "${local.data_source_command} ", +// "hostname=${var.vm_os_name}-${var.vm_os_version} ", + "netcfg/get_hostname=debian netcfg/get_domain=example.com ", + "interface=auto ", + "vga=788 noprompt quiet --" + ] + + uefi_boot_command = [ + // This waits for 3 seconds, sends the "c" key, and then waits for another 3 seconds. In the GRUB boot loader, this is used to enter command line mode. + "c", + // This types a command to load the Linux kernel from the specified path. + "linux /install.amd/vmlinuz", + // This types a string that sets the auto-install/enable option to true. This is used to automate the installation process. + " auto-install/enable=true", + // This types a string that sets the debconf/priority option to critical. This is used to minimize the number of questions asked during the installation process. + " debconf/priority=critical", + // This types the value of the 'data_source_command' local variable. This is used to specify the kickstart data source configured in the common variables. + " ${local.data_source_command}", + // This types a string that sets the noprompt option and then sends the "enter" key. This is used to prevent the installer from pausing for user input. + " noprompt --", + // This types a command to load the initial RAM disk from the specified path and then sends the "enter" key. + "initrd /install.amd/initrd.gz", + // This types the "boot" command and then sends the "enter" key. This starts the boot process using the loaded kernel and initial RAM disk. + "boot", + // This waits for 30 seconds. This is typically used to give the system time to boot before sending more commands. + "", + // This sends the "enter" key and then waits. This is typically used to dismiss any prompts or messages that appear during boot. + "", + // This sends the "enter" key and then waits. This is typically used to dismiss any prompts or messages that appear during boot. + "", + // This types the value of the `mount_cdrom` local variable. This is typically used to mount the installation media. + " ${local.mount_cdrom}", + // This sends four "down arrow" keys and then the "enter" key. This is typically used to select a specific option in a menu. + "" + ] + build_by = "Built by: HashiCorp Packer ${packer.version}" build_date = formatdate("DD-MM-YYYY hh:mm ZZZ", "${timestamp()}" ) build_version = data.git-repository.cwd.head - build_description = "Version: ${local.build_version}\nBuilt on: ${local.build_date}\n${local.build_by}\nCloud-Init: ${var.vm_cloud_init_enable}" + build_description = "Version: ${local.build_version}\nBuilt on: ${local.build_date}\n${local.build_by}\nCloud-Init: ${var.vm_cloudinit}" vm_disk_type = var.vm_disk_type == "virtio" ? "vda" : "sda" manifest_date = formatdate("YYYY-MM-DD hh:mm:ss", timestamp()) manifest_path = "${path.cwd}/manifests/" @@ -51,6 +93,13 @@ locals { vm_os_keyboard = var.vm_os_keyboard vm_os_timezone = var.vm_os_timezone common_data_source = var.common_data_source + network = templatefile("${abspath(path.root)}/data/network.pkrtpl.hcl", { + device = var.vm_network_device + ip = var.vm_ip_address + netmask = var.vm_ip_netmask + gateway = var.vm_ip_gateway + dns = var.vm_dns_list + }) # lvm needs to be here so late commands can access vg names lvm = var.vm_disk_lvm storage = templatefile("${abspath(path.root)}/data/storage.pkrtpl.hcl", { @@ -60,10 +109,14 @@ locals { lvm = var.vm_disk_lvm vm_bios = var.vm_bios }) + additional_packages = join(" ", var.additional_packages) }) } data_source_command = var.common_data_source == "http" ? "url=http://{{.HTTPIP}}:{{.HTTPPort}}/preseed.cfg" : "file=/media/preseed.cfg" + mount_cdrom_command = " mount /dev/sr1 /media " + mount_cdrom = var.common_data_source == "http" ? " " : local.mount_cdrom_command vm_name = "${var.vm_os_family}-${var.vm_os_name}-${var.vm_os_version}" + boot_command = var.vm_bios == "ovmf" ? local.uefi_boot_command : local.bios_boot_command vm_bios = var.vm_bios == "ovmf" ? var.vm_firmware_path : null } @@ -128,17 +181,7 @@ source "proxmox-iso" "debian" { http_port_max = var.common_data_source == "http" ? var.common_http_port_max : null boot = var.vm_boot boot_wait = var.vm_boot_wait - boot_command = [ - "", - "/install.amd/vmlinuz ", - "initrd=/install.amd/initrd.gz ", - "auto=true ", - "${local.data_source_command} ", -// "hostname=${var.vm_os_name}-${var.vm_os_version} ", - "netcfg/get_hostname=debian netcfg/get_domain=example.com ", - "interface=auto ", - "vga=788 noprompt quiet --" - ] + boot_command = local.boot_command dynamic "additional_iso_files" { for_each = var.common_data_source == "disk" ? [1] : [] @@ -153,8 +196,8 @@ source "proxmox-iso" "debian" { template_description = "${local.build_description}" # VM Cloud Init Settings - cloud_init = var.vm_cloud_init_enable - cloud_init_storage_pool = var.vm_cloud_init_enable == true ? var.vm_storage_pool : null + cloud_init = var.vm_cloudinit + cloud_init_storage_pool = var.vm_cloudinit == true ? var.vm_storage_pool : null } @@ -163,19 +206,22 @@ build { sources = ["source.proxmox-iso.debian"] provisioner "ansible" { - user = "${var.build_username}" - playbook_file = "${path.cwd}/ansible/main.yml" - roles_path = "${path.cwd}/ansible/roles" + user = var.build_username + galaxy_file = "${path.cwd}/ansible/linux-requirements.yml" + galaxy_force_with_deps = true + playbook_file = "${path.cwd}/ansible/linux-playbook.yml" + roles_path = "${path.cwd}/ansible/roles" ansible_env_vars = [ - "ANSIBLE_CONFIG=${path.cwd}/ansible/ansible.cfg" + "ANSIBLE_CONFIG=${path.cwd}/ansible/ansible.cfg", + "ANSIBLE_PYTHON_INTERPRETER=/usr/bin/python3" ] extra_arguments = [ "--extra-vars", "display_skipped_hosts=false", - "--extra-vars", "BUILD_USERNAME=${var.build_username}", - "--extra-vars", "BUILD_SECRET='${var.build_key}'", - "--extra-vars", "ANSIBLE_USERNAME=${var.ansible_username}", - "--extra-vars", "ANSIBLE_SECRET='${var.ansible_key}'", - "--extra-vars", "cloud_init='${var.vm_cloud_init_enable}'", + "--extra-vars", "build_username=${var.build_username}", + "--extra-vars", "build_key='${var.build_key}'", + "--extra-vars", "ansible_username=${var.ansible_username}", + "--extra-vars", "ansible_key='${var.ansible_key}'", + "--extra-vars", "enable_cloudinit='${var.vm_cloudinit}'", ] } @@ -196,7 +242,7 @@ build { vm_os_type = "${var.vm_os_type}" vm_mem_size = "${var.vm_mem_size}" vm_network_card_model = "${var.vm_network_card_model}" - vm_cloud_init_enable = "${var.vm_cloud_init_enable}" + vm_cloudinit = "${var.vm_cloudinit}" } } }