From d3e78a37b6d907ecbfc817db6ba4c7a71f8a96c3 Mon Sep 17 00:00:00 2001 From: Ahmed Abdelhalim Date: Mon, 11 Aug 2025 00:33:33 +0200 Subject: Refactor bootstrap to have ubuntu specific tasks --- roles/bootstrap/tasks/install-debian.yml | 118 ------------------------------- roles/bootstrap/tasks/install-ubuntu.yml | 117 ++++++++++++++++++++++++++++++ roles/bootstrap/tasks/main.yml | 6 +- roles/bootstrap/vars/ubuntu.yml | 4 +- 4 files changed, 123 insertions(+), 122 deletions(-) delete mode 100644 roles/bootstrap/tasks/install-debian.yml create mode 100644 roles/bootstrap/tasks/install-ubuntu.yml (limited to 'roles') diff --git a/roles/bootstrap/tasks/install-debian.yml b/roles/bootstrap/tasks/install-debian.yml deleted file mode 100644 index 05d84e9..0000000 --- a/roles/bootstrap/tasks/install-debian.yml +++ /dev/null @@ -1,118 +0,0 @@ ---- -- name: "Include distribution vars" - ansible.builtin.include_vars: "{{ ansible_distribution | lower }}.yml" - -- name: "Ensure install dependencies are installed on live environment" - ansible.builtin.apt: - state: "present" - update_cache: true - pkg: - - "debootstrap" - - "arch-install-scripts" - - "bash" - -- name: "Run debootstrap" - ansible.builtin.command: - cmd: "debootstrap - {{ bootstrap_opts }} - {{ bootstrap_distro.name }} - {{ mnt_root_path }} - {{ bootstrap_distro.mirror_url }}" - register: bootstrap_result - changed_when: true - -- name: "Display bootstrap output" - ansible.builtin.debug: - var: bootstrap_result.stdout_lines - verbosity: 1 - -- name: "Generate fstab" - ansible.builtin.shell: - cmd: "genfstab -t PARTLABEL {{ mnt_root_path }} > {{ mnt_root_path }}/etc/fstab" - changed_when: true - -- name: "Configure apt in chroot" - ansible.builtin.copy: - dest: "{{ item.dest }}" - mode: "0640" - content: "{{ item.content }}" - with_items: - - dest: "{{ mnt_root_path }}/etc/apt/preferences.d/ignored-package" - content: "{{ bootstrap_apt_ignored_preferences }}" - - dest: "{{ mnt_root_path }}/etc/apt/sources.list" - content: "{{ bootstrap_apt_sources }}" - -- name: "Configure system in chroot" - ansible.builtin.command: - cmd: "arch-chroot {{ mnt_root_path }} {{ item }}" - changed_when: true - with_items: - - "apt-get update" - - "apt-get install -y {{ bootstrap_distro.kernel_packages | join(' ') }}" - - "apt-get install -y systemd-boot systemd-boot-efi" - -- name: "Run bootclt" - ansible.builtin.command: - cmd: "arch-chroot {{ mnt_root_path }} bootctl install" - changed_when: true - -- name: "Get machine-id" - ansible.builtin.command: - cmd: "cat {{ mnt_root_path }}/etc/machine-id" - changed_when: false - register: bootstrap_machine_id - -- name: "Get Kernel version" - ansible.builtin.shell: | - set -eo pipefail - ls {{ mnt_root_path }}/boot/vmlinuz-* | sed 's/.*vmlinuz-//' - args: - executable: /usr/bin/bash - changed_when: false - register: bootstrap_kernel_version - -- name: "Create distro kernel directory" - ansible.builtin.file: - path: "{{ mnt_boot_path }}/{{ ansible_distribution | lower }}" - state: "directory" - mode: "0700" - -- name: "Copy kernel to efi partition" - ansible.builtin.copy: - src: "{{ item }}" - dest: "{{ mnt_boot_path }}/{{ ansible_distribution | lower }}" - remote_src: true - mode: "0644" - with_items: - - "{{ mnt_root_path }}/boot/vmlinuz-{{ bootstrap_kernel_version.stdout }}" - - "{{ mnt_root_path }}/boot/initrd.img-{{ bootstrap_kernel_version.stdout }}" - -- name: "Template systemd-boot files" - ansible.builtin.template: - src: "{{ item.src }}" - dest: "{{ item.dest }}" - mode: "0644" - with_items: - - src: "loader.conf.j2" - dest: "{{ mnt_boot_path }}/loader/loader.conf" - - src: "{{ ansible_distribution | lower }}.conf.j2" - dest: "{{ mnt_boot_path }}/loader/entries/{{ ansible_distribution | lower }}.conf" - -- name: "Clean up debian kernel install" - ansible.builtin.file: - path: "{{ item }}" - state: "absent" - with_items: - - "{{ mnt_boot_path }}/{{ bootstrap_machine_id.stdout }}" - - "{{ mnt_boot_path }}/loader/entries/{{ bootstrap_machine_id.stdout }}-{{ bootstrap_kernel_version.stdout }}.conf" - -- name: "Configure hostname in chroot" - ansible.builtin.copy: - dest: "{{ mnt_root_path }}/etc/hostname" - mode: "0644" - content: "{{ hostname }}" - -- name: "Ensure ansible dependencies are installed in chroot" - ansible.builtin.command: - cmd: "arch-chroot {{ mnt_root_path }} apt-get install -y python3" - changed_when: true diff --git a/roles/bootstrap/tasks/install-ubuntu.yml b/roles/bootstrap/tasks/install-ubuntu.yml new file mode 100644 index 0000000..9e6801f --- /dev/null +++ b/roles/bootstrap/tasks/install-ubuntu.yml @@ -0,0 +1,117 @@ +--- +- name: "Include distribution vars" + ansible.builtin.include_vars: "{{ ansible_distribution | lower }}.yml" + +- name: "Ensure install dependencies are installed on live environment" + ansible.builtin.apt: + state: "present" + update_cache: true + pkg: + - "debootstrap" + - "arch-install-scripts" + - "bash" + +- name: "Run debootstrap" + ansible.builtin.command: + cmd: "debootstrap + {{ bootstrap_opts }} + {{ bootstrap_distro.name }} + {{ mnt_root_path }} + {{ bootstrap_distro.mirror_url }}" + register: bootstrap_result + changed_when: true + +- name: "Display bootstrap output" + ansible.builtin.debug: + var: bootstrap_result.stdout_lines + verbosity: 1 + +- name: "Generate fstab" + ansible.builtin.shell: + cmd: "genfstab -t PARTLABEL {{ mnt_root_path }} > {{ mnt_root_path }}/etc/fstab" + changed_when: true + +- name: "Configure apt in chroot" + ansible.builtin.copy: + dest: "{{ item.dest }}" + mode: "0640" + content: "{{ item.content }}" + with_items: + - dest: "{{ mnt_root_path }}/etc/apt/preferences.d/ignored-package" + content: "{{ bootstrap_apt_ignored_preferences }}" + - dest: "{{ mnt_root_path }}/etc/apt/sources.list" + content: "{{ bootstrap_apt_sources }}" + +- name: "Configure system in chroot" + ansible.builtin.command: + cmd: "arch-chroot {{ mnt_root_path }} {{ item }}" + changed_when: true + with_items: + - "apt-get update" + - "apt-get install -y {{ bootstrap_distro.packages | join(' ') }}" + +- name: "Run bootclt" + ansible.builtin.command: + cmd: "arch-chroot {{ mnt_root_path }} bootctl install" + changed_when: true + +- name: "Get machine-id" + ansible.builtin.command: + cmd: "cat {{ mnt_root_path }}/etc/machine-id" + changed_when: false + register: bootstrap_machine_id + +- name: "Get Kernel version" + ansible.builtin.shell: | + set -eo pipefail + ls {{ mnt_root_path }}/boot/vmlinuz-* | sed 's/.*vmlinuz-//' + args: + executable: /usr/bin/bash + changed_when: false + register: bootstrap_kernel_version + +- name: "Create distro kernel directory" + ansible.builtin.file: + path: "{{ mnt_boot_path }}/{{ ansible_distribution | lower }}" + state: "directory" + mode: "0700" + +- name: "Copy kernel to efi partition" + ansible.builtin.copy: + src: "{{ item }}" + dest: "{{ mnt_boot_path }}/{{ ansible_distribution | lower }}" + remote_src: true + mode: "0644" + with_items: + - "{{ mnt_root_path }}/boot/vmlinuz-{{ bootstrap_kernel_version.stdout }}" + - "{{ mnt_root_path }}/boot/initrd.img-{{ bootstrap_kernel_version.stdout }}" + +- name: "Template systemd-boot files" + ansible.builtin.template: + src: "{{ item.src }}" + dest: "{{ item.dest }}" + mode: "0644" + with_items: + - src: "loader.conf.j2" + dest: "{{ mnt_boot_path }}/loader/loader.conf" + - src: "{{ ansible_distribution | lower }}.conf.j2" + dest: "{{ mnt_boot_path }}/loader/entries/{{ ansible_distribution | lower }}.conf" + +- name: "Clean up debian kernel install" + ansible.builtin.file: + path: "{{ item }}" + state: "absent" + with_items: + - "{{ mnt_boot_path }}/{{ bootstrap_machine_id.stdout }}" + - "{{ mnt_boot_path }}/loader/entries/{{ bootstrap_machine_id.stdout }}-{{ bootstrap_kernel_version.stdout }}.conf" + +- name: "Configure hostname in chroot" + ansible.builtin.copy: + dest: "{{ mnt_root_path }}/etc/hostname" + mode: "0644" + content: "{{ hostname }}" + +- name: "Ensure ansible dependencies are installed in chroot" + ansible.builtin.command: + cmd: "arch-chroot {{ mnt_root_path }} apt-get install -y python3" + changed_when: true diff --git a/roles/bootstrap/tasks/main.yml b/roles/bootstrap/tasks/main.yml index af5dc49..723561d 100644 --- a/roles/bootstrap/tasks/main.yml +++ b/roles/bootstrap/tasks/main.yml @@ -9,6 +9,6 @@ ansible.builtin.include_tasks: "install-archlinux.yml" when: ansible_os_family == "Archlinux" -- name: "Install debian" - ansible.builtin.include_tasks: "install-debian.yml" - when: ansible_os_family == "Debian" +- name: "Install ubuntu" + ansible.builtin.include_tasks: "install-ubuntu.yml" + when: ansible_os_distribution == "Ubuntu" diff --git a/roles/bootstrap/vars/ubuntu.yml b/roles/bootstrap/vars/ubuntu.yml index 829ecae..1116087 100644 --- a/roles/bootstrap/vars/ubuntu.yml +++ b/roles/bootstrap/vars/ubuntu.yml @@ -2,8 +2,10 @@ bootstrap_distro: name: "noble" # => Ubuntu 24.04 mirror_url: "http://archive.ubuntu.com/ubuntu/" - kernel_packages: + install_packages: - "linux-generic-hwe-24.04" + - "systemd-boot" + - "systemd-boot-efi" bootstrap_opts: "" -- cgit v1.2.3