refactor(configuration): add platform_config dict and replace is_rhel/is_debian with os_family lookups

This commit is contained in:
2026-02-22 02:26:54 +01:00
parent 9c0f00f1ec
commit a460584c5d
11 changed files with 101 additions and 44 deletions

View File

@@ -6,11 +6,10 @@
"redhat" if os == "rhel" "redhat" if os == "rhel"
else ("ubuntu" if os in ["ubuntu", "ubuntu-lts"] else os) else ("ubuntu" if os in ["ubuntu", "ubuntu-lts"] else os)
}} }}
_efi_loader: >- _efi_loader: "{{ _configuration_platform.efi_loader }}"
{{ "shimx64.efi" if is_rhel | bool else "grubx64.efi" }}
block: block:
- name: Install GRUB EFI binary - name: Install GRUB EFI binary
when: not (is_rhel | bool) when: _configuration_platform.grub_install
ansible.builtin.command: >- ansible.builtin.command: >-
{{ chroot_command }} /usr/sbin/grub-install --target=x86_64-efi {{ chroot_command }} /usr/sbin/grub-install --target=x86_64-efi
--efi-directory={{ partitioning_efi_mountpoint }} --efi-directory={{ partitioning_efi_mountpoint }}
@@ -44,20 +43,8 @@
backrefs: true backrefs: true
- name: Regenerate initramfs - name: Regenerate initramfs
when: os not in ["alpine", "void"] when: _configuration_platform.initramfs_cmd | length > 0
vars: ansible.builtin.command: "{{ chroot_command }} {{ _configuration_platform.initramfs_cmd }}"
configuration_initramfs_cmd: >-
{{
'/usr/sbin/mkinitcpio -P'
if os == "archlinux"
else (
'/usr/bin/env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin '
+ '/usr/sbin/update-initramfs -u -k all'
if is_debian | bool
else '/usr/bin/dracut --regenerate-all --force'
)
}}
ansible.builtin.command: "{{ chroot_command }} {{ configuration_initramfs_cmd }}"
register: configuration_initramfs_result register: configuration_initramfs_result
changed_when: configuration_initramfs_result.rc == 0 changed_when: configuration_initramfs_result.rc == 0
@@ -65,10 +52,10 @@
vars: vars:
configuration_grub_cfg_cmd: >- configuration_grub_cfg_cmd: >-
{{ {{
'/usr/sbin/grub2-mkconfig -o ' '/usr/sbin/' + _configuration_platform.grub_mkconfig_prefix + ' -o '
+ partitioning_efi_mountpoint + partitioning_efi_mountpoint
+ '/EFI/' + _efi_vendor + '/grub.cfg' + '/EFI/' + _efi_vendor + '/grub.cfg'
if is_rhel | bool if os_family == 'RedHat'
else '/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg' else '/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg'
}} }}
ansible.builtin.command: "{{ chroot_command }} {{ configuration_grub_cfg_cmd }}" ansible.builtin.command: "{{ chroot_command }} {{ configuration_grub_cfg_cmd }}"

View File

@@ -134,7 +134,7 @@
- name: Ensure keyfile pattern for initramfs-tools - name: Ensure keyfile pattern for initramfs-tools
when: when:
- is_debian | bool - os_family == 'Debian'
- configuration_luks_keyfile_in_use - configuration_luks_keyfile_in_use
ansible.builtin.lineinfile: ansible.builtin.lineinfile:
path: /mnt/etc/cryptsetup-initramfs/conf-hook path: /mnt/etc/cryptsetup-initramfs/conf-hook
@@ -198,14 +198,14 @@
}}) }})
- name: Ensure dracut config directory exists - name: Ensure dracut config directory exists
when: is_rhel | bool when: os_family == 'RedHat'
ansible.builtin.file: ansible.builtin.file:
path: /mnt/etc/dracut.conf.d path: /mnt/etc/dracut.conf.d
state: directory state: directory
mode: "0755" mode: "0755"
- name: Configure dracut for LUKS - name: Configure dracut for LUKS
when: is_rhel | bool when: os_family == 'RedHat'
ansible.builtin.copy: ansible.builtin.copy:
dest: /mnt/etc/dracut.conf.d/crypt.conf dest: /mnt/etc/dracut.conf.d/crypt.conf
content: | content: |
@@ -216,13 +216,13 @@
mode: "0644" mode: "0644"
- name: Read kernel cmdline defaults - name: Read kernel cmdline defaults
when: is_rhel | bool when: os_family == 'RedHat'
ansible.builtin.slurp: ansible.builtin.slurp:
src: /mnt/etc/kernel/cmdline src: /mnt/etc/kernel/cmdline
register: configuration_kernel_cmdline_slurp register: configuration_kernel_cmdline_slurp
- name: Build kernel cmdline with LUKS args - name: Build kernel cmdline with LUKS args
when: is_rhel | bool when: os_family == 'RedHat'
vars: vars:
kernel_cmdline_current: >- kernel_cmdline_current: >-
{{ configuration_kernel_cmdline_slurp.content | b64decode | trim }} {{ configuration_kernel_cmdline_slurp.content | b64decode | trim }}
@@ -247,14 +247,14 @@
configuration_kernel_cmdline_new: "{{ kernel_cmdline_new }}" configuration_kernel_cmdline_new: "{{ kernel_cmdline_new }}"
- name: Write kernel cmdline with LUKS args - name: Write kernel cmdline with LUKS args
when: is_rhel | bool when: os_family == 'RedHat'
ansible.builtin.copy: ansible.builtin.copy:
dest: /mnt/etc/kernel/cmdline dest: /mnt/etc/kernel/cmdline
mode: "0644" mode: "0644"
content: "{{ configuration_kernel_cmdline_new }}\n" content: "{{ configuration_kernel_cmdline_new }}\n"
- name: Find BLS entries for encryption kernel cmdline - name: Find BLS entries for encryption kernel cmdline
when: is_rhel | bool when: os_family == 'RedHat'
ansible.builtin.find: ansible.builtin.find:
paths: /mnt/boot/loader/entries paths: /mnt/boot/loader/entries
patterns: "*.conf" patterns: "*.conf"
@@ -263,7 +263,7 @@
- name: Update BLS options with LUKS args - name: Update BLS options with LUKS args
when: when:
- is_rhel | bool - os_family == 'RedHat'
- configuration_kernel_bls_entries.files | length > 0 - configuration_kernel_bls_entries.files | length > 0
ansible.builtin.lineinfile: ansible.builtin.lineinfile:
path: "{{ item.path }}" path: "{{ item.path }}"
@@ -274,13 +274,13 @@
label: "{{ item.path }}" label: "{{ item.path }}"
- name: Read grub defaults - name: Read grub defaults
when: not is_rhel | bool when: not os_family == 'RedHat'
ansible.builtin.slurp: ansible.builtin.slurp:
src: /mnt/etc/default/grub src: /mnt/etc/default/grub
register: configuration_grub_slurp register: configuration_grub_slurp
- name: Build grub command lines with LUKS args - name: Build grub command lines with LUKS args
when: not is_rhel | bool when: not os_family == 'RedHat'
vars: vars:
grub_content: "{{ configuration_grub_slurp.content | b64decode }}" grub_content: "{{ configuration_grub_slurp.content | b64decode }}"
grub_cmdline_linux: >- grub_cmdline_linux: >-
@@ -344,7 +344,7 @@
configuration_grub_cmdline_default_new: "{{ grub_cmdline_default_new }}" configuration_grub_cmdline_default_new: "{{ grub_cmdline_default_new }}"
- name: Update GRUB_CMDLINE_LINUX_DEFAULT for LUKS - name: Update GRUB_CMDLINE_LINUX_DEFAULT for LUKS
when: not is_rhel | bool when: not os_family == 'RedHat'
ansible.builtin.lineinfile: ansible.builtin.lineinfile:
path: /mnt/etc/default/grub path: /mnt/etc/default/grub
regexp: "^GRUB_CMDLINE_LINUX_DEFAULT=" regexp: "^GRUB_CMDLINE_LINUX_DEFAULT="

View File

@@ -1,7 +1,7 @@
--- ---
- name: Append vim configurations to vimrc - name: Append vim configurations to vimrc
ansible.builtin.blockinfile: ansible.builtin.blockinfile:
path: "{{ '/mnt/etc/vim/vimrc' if is_debian | bool else '/mnt/etc/vimrc' }}" path: "{{ '/mnt/etc/vim/vimrc' if os_family == 'Debian' else '/mnt/etc/vimrc' }}"
block: | block: |
set encoding=utf-8 set encoding=utf-8
set number set number

View File

@@ -1,6 +1,6 @@
--- ---
- name: Configure grub defaults - name: Configure grub defaults
when: not is_rhel | bool when: os_family != 'RedHat'
ansible.builtin.lineinfile: ansible.builtin.lineinfile:
dest: /mnt/etc/default/grub dest: /mnt/etc/default/grub
regexp: "{{ item.regexp }}" regexp: "{{ item.regexp }}"
@@ -14,7 +14,7 @@
label: "{{ item.line }}" label: "{{ item.line }}"
- name: Ensure grub defaults file exists for RHEL-based systems - name: Ensure grub defaults file exists for RHEL-based systems
when: is_rhel | bool when: os_family == 'RedHat'
block: block:
- name: Build RHEL kernel command line defaults - name: Build RHEL kernel command line defaults
vars: vars:

View File

@@ -14,7 +14,7 @@
- name: Setup locales - name: Setup locales
block: block:
- name: Configure locale.gen - name: Configure locale.gen
when: not is_rhel | bool when: _configuration_platform.locale_gen
ansible.builtin.lineinfile: ansible.builtin.lineinfile:
dest: /mnt/etc/locale.gen dest: /mnt/etc/locale.gen
regexp: "{{ item.regex }}" regexp: "{{ item.regex }}"
@@ -25,7 +25,7 @@
label: "{{ item.line }}" label: "{{ item.line }}"
- name: Generate locales - name: Generate locales
when: not is_rhel | bool when: _configuration_platform.locale_gen
ansible.builtin.command: "{{ chroot_command }} /usr/sbin/locale-gen" ansible.builtin.command: "{{ chroot_command }} /usr/sbin/locale-gen"
register: configuration_locale_result register: configuration_locale_result
changed_when: configuration_locale_result.rc == 0 changed_when: configuration_locale_result.rc == 0

View File

@@ -1,4 +1,8 @@
--- ---
- name: Resolve platform-specific configuration
ansible.builtin.set_fact:
_configuration_platform: "{{ configuration_platform_config[os_family] }}"
- name: Include configuration tasks - name: Include configuration tasks
when: configuration_task.when | default(true) when: configuration_task.when | default(true)
ansible.builtin.include_tasks: "{{ configuration_task.file }}" ansible.builtin.include_tasks: "{{ configuration_task.file }}"
@@ -17,7 +21,7 @@
- file: users.yml - file: users.yml
- file: sudo.yml - file: sudo.yml
- file: selinux.yml - file: selinux.yml
when: "{{ is_rhel | bool }}" when: "{{ os_family == 'RedHat' }}"
loop_control: loop_control:
loop_var: configuration_task loop_var: configuration_task
label: "{{ configuration_task.file }}" label: "{{ configuration_task.file }}"

View File

@@ -1,6 +1,6 @@
--- ---
- name: Fix SELinux - name: Fix SELinux
when: is_rhel | bool when: os_family == 'RedHat'
block: block:
- name: Fix SELinux by pre-labeling the filesystem before first boot - name: Fix SELinux by pre-labeling the filesystem before first boot
when: os in ['almalinux', 'rocky', 'rhel'] and system_cfg.features.selinux.enabled | bool when: os in ['almalinux', 'rocky', 'rhel'] and system_cfg.features.selinux.enabled | bool

View File

@@ -1,13 +1,13 @@
--- ---
- name: Enable systemd services - name: Enable systemd services
when: os not in ['alpine', 'void'] when: _configuration_platform.init_system == 'systemd'
vars: vars:
configuration_systemd_services: >- configuration_systemd_services: >-
{{ {{
['NetworkManager'] ['NetworkManager']
+ (['firewalld'] if system_cfg.features.firewall.backend == 'firewalld' and system_cfg.features.firewall.enabled | bool else []) + (['firewalld'] if system_cfg.features.firewall.backend == 'firewalld' and system_cfg.features.firewall.enabled | bool else [])
+ (['ufw'] if system_cfg.features.firewall.backend == 'ufw' and system_cfg.features.firewall.enabled | bool else []) + (['ufw'] if system_cfg.features.firewall.backend == 'ufw' and system_cfg.features.firewall.enabled | bool else [])
+ ([('ssh' if is_debian | bool else 'sshd')] if system_cfg.features.ssh.enabled | bool else []) + ([_configuration_platform.ssh_service] if system_cfg.features.ssh.enabled | bool else [])
+ (['logrotate', 'systemd-timesyncd'] if os == 'archlinux' else []) + (['logrotate', 'systemd-timesyncd'] if os == 'archlinux' else [])
}} }}
ansible.builtin.command: "{{ chroot_command }} systemctl enable {{ item }}" ansible.builtin.command: "{{ chroot_command }} systemctl enable {{ item }}"
@@ -16,7 +16,7 @@
changed_when: configuration_enable_service_result.rc == 0 changed_when: configuration_enable_service_result.rc == 0
- name: Enable OpenRC services - name: Enable OpenRC services
when: os == 'alpine' when: _configuration_platform.init_system == 'openrc'
vars: vars:
configuration_openrc_services: >- configuration_openrc_services: >-
{{ {{
@@ -48,7 +48,7 @@
when: item.stat.exists when: item.stat.exists
- name: Enable runit services - name: Enable runit services
when: os == 'void' when: _configuration_platform.init_system == 'runit'
vars: vars:
configuration_runit_services: >- configuration_runit_services: >-
{{ {{

View File

@@ -9,7 +9,7 @@
- name: Give sudo access to wheel group - name: Give sudo access to wheel group
ansible.builtin.copy: ansible.builtin.copy:
content: "{{ '%sudo ALL=(ALL) ALL\n' if is_debian | bool else '%wheel ALL=(ALL) ALL\n' }}" content: "{{ _configuration_platform.sudo_group }} ALL=(ALL) ALL\n"
dest: /mnt/etc/sudoers.d/01-wheel dest: /mnt/etc/sudoers.d/01-wheel
mode: "0440" mode: "0440"
validate: /usr/sbin/visudo --check --file=%s validate: /usr/sbin/visudo --check --file=%s

View File

@@ -17,8 +17,7 @@
- name: Create user accounts - name: Create user accounts
vars: vars:
configuration_user_group: >- configuration_user_group: "{{ _configuration_platform.user_group }}"
{{ "sudo" if is_debian | bool else "wheel" }}
# UID starts at 1000; safe for fresh installs only # UID starts at 1000; safe for fresh installs only
configuration_useradd_cmd: >- configuration_useradd_cmd: >-
{{ chroot_command }} /usr/sbin/useradd --create-home --user-group {{ chroot_command }} /usr/sbin/useradd --create-home --user-group

View File

@@ -0,0 +1,67 @@
---
# Platform-specific configuration values keyed by os_family.
# Consumed as _configuration_platform in tasks via:
# configuration_platform_config[os_family]
configuration_platform_config:
RedHat:
user_group: wheel
sudo_group: "%wheel"
ssh_service: sshd
efi_loader: shimx64.efi
grub_install: false
initramfs_cmd: "/usr/bin/dracut --regenerate-all --force"
grub_mkconfig_prefix: grub2-mkconfig
locale_gen: false
init_system: systemd
Debian:
user_group: sudo
sudo_group: "%sudo"
ssh_service: ssh
efi_loader: grubx64.efi
grub_install: true
initramfs_cmd: >-
/usr/bin/env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/usr/sbin/update-initramfs -u -k all
grub_mkconfig_prefix: grub-mkconfig
locale_gen: true
init_system: systemd
Archlinux:
user_group: wheel
sudo_group: "%wheel"
ssh_service: sshd
efi_loader: grubx64.efi
grub_install: true
initramfs_cmd: "/usr/sbin/mkinitcpio -P"
grub_mkconfig_prefix: grub-mkconfig
locale_gen: true
init_system: systemd
Suse:
user_group: wheel
sudo_group: "%wheel"
ssh_service: sshd
efi_loader: grubx64.efi
grub_install: true
initramfs_cmd: "/usr/bin/dracut --regenerate-all --force"
grub_mkconfig_prefix: grub-mkconfig
locale_gen: true
init_system: systemd
Alpine:
user_group: wheel
sudo_group: "%wheel"
ssh_service: sshd
efi_loader: grubx64.efi
grub_install: true
initramfs_cmd: ""
grub_mkconfig_prefix: grub-mkconfig
locale_gen: false
init_system: openrc
Void:
user_group: wheel
sudo_group: "%wheel"
ssh_service: sshd
efi_loader: grubx64.efi
grub_install: true
initramfs_cmd: ""
grub_mkconfig_prefix: grub-mkconfig
locale_gen: false
init_system: runit