diff --git a/build-packer-templates.yaml b/build-packer-templates.yaml index b912437..1fccd9a 100644 --- a/build-packer-templates.yaml +++ b/build-packer-templates.yaml @@ -27,6 +27,12 @@ iso_url: "https://fedora.cu.be/linux/releases/40/Server/x86_64/iso" iso_name: "Fedora-Server-netinst-x86_64-40-1.14.iso" checksum_filename: "Fedora-Server-40-1.14-x86_64-CHECKSUM" + - type: "debian" + flavor: "debian" + version: "12" + iso_url: "https://cdimage.debian.org/debian-cd/current/amd64/iso-cd" + iso_name: "debian-11.3.0-amd64-netinst.iso" + checksum_filename: "SHA256SUMS" - type: "debian" flavor: "ubuntu" version: "20.04" diff --git a/debian12.pkr.hcl b/debian12.pkr.hcl new file mode 100644 index 0000000..a4da7e7 --- /dev/null +++ b/debian12.pkr.hcl @@ -0,0 +1,118 @@ +#Generated by packer-kvm/build-packer-templates.yaml + +packer { + required_plugins { + qemu = { + version = "~> 1" + source = "github.com/hashicorp/qemu" + } + ansible = { + version = ">= 1.1.2" + source = "github.com/hashicorp/ansible" + } + } +} + +variable "cpu" { + type = string + default = "2" +} + +variable "destination_server" { + type = string + default = "download.goffinet.org" +} + +variable "disk_size" { + type = string + default = "40000" +} + +variable "headless" { + type = string + default = "true" +} + +variable "iso_checksum" { + type = string + default = "file:https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/SHA256SUMS" +} + +variable "iso_url" { + type = string + default = "https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-11.3.0-amd64-netinst.iso" +} + +variable "name" { + type = string + default = "debian" +} + +variable "ram" { + type = string + default = "2048" +} + +variable "ssh_password" { + type = string + default = "testtest" +} + +variable "ssh_username" { + type = string + default = "root" +} + +variable "version" { + type = string + default = "12" +} + +source "qemu" "debian12" { + accelerator = "kvm" + boot_command = ["", "auto ", "console-keymaps-at/keymap=us ", "console-setup/ask_detect=false ", "debconf/frontend=noninteractive ", "debian-installer=en_US ", "fb=false ", "install ", "kbd-chooser/method=us ", "keyboard-configuration/xkb-keymap=us ", "locale=en_US ", "netcfg/get_hostname=${var.name}${var.version} ", "preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/http/${var.config_file} ", ""] + disk_cache = "none" + disk_compression = true + disk_discard = "unmap" + disk_interface = "virtio" + disk_size = var.disk_size + format = "qcow2" + headless = var.headless + http_directory = "." + iso_checksum = var.iso_checksum + iso_url = var.iso_url + net_device = "virtio-net" + output_directory = "artifacts/qemu/${var.name}${var.version}" + qemu_binary = "/usr/bin/qemu-system-x86_64" + qemuargs = [["-m", "${var.ram}M"], ["-smp", "${var.cpu}"], ["-cpu", "host"]] + shutdown_command = "sudo /usr/sbin/shutdown -h now" + ssh_password = var.ssh_password + ssh_username = var.ssh_username + boot_wait = "3s" + ssh_handshake_attempts = 500 + ssh_timeout = "45m" + ssh_wait_timeout = "45m" + host_port_max = 2229 + host_port_min = 2222 + http_port_max = 10089 + http_port_min = 10082 +} + +build { + sources = ["source.qemu.debian12"] + + provisioner "shell" { + execute_command = "{{ .Vars }} sudo -E bash '{{ .Path }}'" + inline = ["apt-get update", "apt -y install python3 python3-pip", "pip3 install ansible"] + } + + provisioner "ansible-local" { + playbook_dir = "ansible" + playbook_file = "ansible/playbook.yml" + } + + post-processor "shell-local" { + environment_vars = ["IMAGE_NAME=${var.name}", "IMAGE_VERSION=${var.version}", "DESTINATION_SERVER=${var.destination_server}"] + script = "scripts/push-image.sh" + } +} \ No newline at end of file diff --git a/http/debian12/meta-data b/http/debian12/meta-data new file mode 100644 index 0000000..e69de29 diff --git a/http/debian12/user-data b/http/debian12/user-data new file mode 100644 index 0000000..b3f3ce2 --- /dev/null +++ b/http/debian12/user-data @@ -0,0 +1,36 @@ +#cloud-config +autoinstall: + version: 1 + identity: + hostname: debian12 + username: ubuntu + password: $6$rounds=4096$mB3ZauOM8/HIycgS$iaaXsE86il1SLeotstYVkkQXG9md.tG0QMyWvszGlc4zye9SxK3W3lnyYT5K9vkgZWZa7QFwIOIqPE8umY7zg1 + user-data: + disable_root: false + keyboard: + layout: us + locale: en_US + network: + ethernets: + eth0: + dhcp4: true + dhcp-identifier: mac + version: 2 + ssh: + allow-pw: true + install-server: true + storage: + layout: + name: lvm + late-commands: + - sed -i -e 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /target/etc/ssh/sshd_config + - sed -i -e 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /target/etc/ssh/sshd_config + - echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/ubuntu + - sed -ie 's/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX="net.ifnames=0 ipv6.disable=1 biosdevname=0"/' /target/etc/default/grub + - curtin in-target --target /target update-grub2 + packages: + - bc + - curl + - wget + - openssh-server +# - qemu-guest-agent \ No newline at end of file diff --git a/http/user-data b/http/user-data new file mode 100644 index 0000000..1350e51 --- /dev/null +++ b/http/user-data @@ -0,0 +1,50 @@ +d-i pkgsel/install-language-support boolean false + +# locale +d-i debian-installer/locale string en_US.UTF-8 + +# keyboard +d-i keyboard-configuration/xkb-keymap select us + +# timezone +d-i time/zone string Europe/Paris + +# Controls whether to use NTP to set the clock during the install +d-i clock-setup/ntp boolean true + +# apt +d-i mirror/country string manual +d-i mirror/http/hostname string ftp.debian.org +d-i mirror/http/directory string /debian +d-i mirror/http/proxy string +d-i apt-setup/use_mirror boolean true + +# users +d-i passwd/root-password password testtest +d-i passwd/root-password-again password testtest +d-i passwd/make-user boolean false + +# partitioning +d-i partman-auto/method string regular +d-i partman-auto/choose_recipe select atomic +d-i partman-partitioning/confirm_write_new_label boolean true +d-i partman/choose_partition select finish +d-i partman/confirm boolean true +d-i partman/confirm_nooverwrite boolean true + +apt-cdrom-setup apt-setup/cdrom/set-first boolean false + +# software +tasksel tasksel/first multiselect standard +d-i pkgsel/include string qemu-guest-agent wget openssh-server vim sudo cloud-init +popularity-contest popularity-contest/participate boolean false + +# grub +d-i grub-installer/only_debian boolean true +d-i grub-installer/bootdev string /dev/vda + +# script +d-i preseed/late_command string in-target sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config + +# finish +d-i finish-install/reboot_in_progress note diff --git a/roles/build_packer_templates/defaults/main.yaml b/roles/build_packer_templates/defaults/main.yaml index 35ca3cc..a20835b 100644 --- a/roles/build_packer_templates/defaults/main.yaml +++ b/roles/build_packer_templates/defaults/main.yaml @@ -25,22 +25,37 @@ boot_command: >- "initrd /casper/initrd", "boot"] {%- endif -%} + {%- if flavor == 'debian' -%} + ["", "auto ", + "console-keymaps-at/keymap=us ", + "console-setup/ask_detect=false ", + "debconf/frontend=noninteractive ", + "debian-installer=en_US ", + "fb=false ", + "install ", + "kbd-chooser/method=us ", + "keyboard-configuration/xkb-keymap=us ", + "locale=en_US ", + "netcfg/get_hostname=${var.name}${var.version} ", + "preseed/url=http://{% raw %}{{ .HTTPIP }}:{{ .HTTPPort }}{% endraw %}/http/${var.config_file} ", + ""] + {%- endif -%} {%- endif -%} ssh_password_salt: >- mB3ZauOM8/HIycgS ssh_password: >- - {%- if type == 'redhat' -%} + {%- if type == 'redhat' or flavor == 'debian' -%} testtest - {%- elif type == 'debian' -%} + {%- elif flavor == 'ubuntu' -%} ubuntu {%- endif -%} ssh_username: >- - {%- if type == 'redhat' -%} + {%- if type == 'redhat' or flavor == 'debian' -%} root - {%- elif type == 'debian' -%} + {%- elif flavor == 'ubuntu' -%} ubuntu {%- endif -%} @@ -63,10 +78,14 @@ shell_provisioner: >- {%- if type == 'redhat' -%} ["dnf -y install python3 python3-pip", "pip3 install ansible"] - {%- elif type == 'debian' -%} + {%- elif flavor == 'ubuntu' -%} ["sudo apt-get update", "sudo apt-get -y install software-properties-common", "sudo apt-add-repository --yes --update ppa:ansible/ansible", "sudo apt update", "sudo apt -y install ansible"] + {% elif flavor == 'debian' %} + ["apt-get update", + "apt -y install python3 python3-pip", + "pip3 install ansible"] {%- endif -%} diff --git a/roles/build_packer_templates/tasks/debian.yaml b/roles/build_packer_templates/tasks/debian.yaml index 186529f..f7083d9 100644 --- a/roles/build_packer_templates/tasks/debian.yaml +++ b/roles/build_packer_templates/tasks/debian.yaml @@ -3,6 +3,13 @@ type: "{{ item.type }}" flavor: "{{ item.flavor }}" +- name: Set debian flavor computed variables + ansible.legacy.set_fact: + label: "{{ item.flavor }}{{ item.version.split('.')[0] }}" + version: "{{ item.version.split('.')[0] }}" + preseed_suffix: "-preseed.cfg" + when: item.flavor == "debian" + - name: Set ubuntu computed variables ansible.legacy.set_fact: label: "{{ item.flavor }}{{ item.version.split('.')[0] }}{{ item.version.split('.')[1] }}" @@ -33,3 +40,14 @@ ansible.legacy.template: src: "{{ metadata_file }}.j2" dest: "{{ playbook_dir }}/http/{{ label }}/{{ metadata_file }}" + +- name: Create debian flavor http directory + ansible.legacy.file: + dest: "{{ playbook_dir }}/http" + state: directory + +- name: Create debian flavor preseed file + ansible.legacy.template: + src: "preseed.cfg.j2" + dest: "{{ playbook_dir }}/http/{{ config_file }}" + diff --git a/roles/build_packer_templates/templates/preseed.cfg.j2 b/roles/build_packer_templates/templates/preseed.cfg.j2 new file mode 100644 index 0000000..1350e51 --- /dev/null +++ b/roles/build_packer_templates/templates/preseed.cfg.j2 @@ -0,0 +1,50 @@ +d-i pkgsel/install-language-support boolean false + +# locale +d-i debian-installer/locale string en_US.UTF-8 + +# keyboard +d-i keyboard-configuration/xkb-keymap select us + +# timezone +d-i time/zone string Europe/Paris + +# Controls whether to use NTP to set the clock during the install +d-i clock-setup/ntp boolean true + +# apt +d-i mirror/country string manual +d-i mirror/http/hostname string ftp.debian.org +d-i mirror/http/directory string /debian +d-i mirror/http/proxy string +d-i apt-setup/use_mirror boolean true + +# users +d-i passwd/root-password password testtest +d-i passwd/root-password-again password testtest +d-i passwd/make-user boolean false + +# partitioning +d-i partman-auto/method string regular +d-i partman-auto/choose_recipe select atomic +d-i partman-partitioning/confirm_write_new_label boolean true +d-i partman/choose_partition select finish +d-i partman/confirm boolean true +d-i partman/confirm_nooverwrite boolean true + +apt-cdrom-setup apt-setup/cdrom/set-first boolean false + +# software +tasksel tasksel/first multiselect standard +d-i pkgsel/include string qemu-guest-agent wget openssh-server vim sudo cloud-init +popularity-contest popularity-contest/participate boolean false + +# grub +d-i grub-installer/only_debian boolean true +d-i grub-installer/bootdev string /dev/vda + +# script +d-i preseed/late_command string in-target sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config + +# finish +d-i finish-install/reboot_in_progress note