From 15c6d9767192ba9066368f959bece073a6e28042 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Thu, 14 May 2026 00:05:13 +0200 Subject: [PATCH] Rewrite GRUB cmdline NVIDIA param injection with grep+replace Replace the slurp/set_fact Jinja-heavy approach (which was clobbering pre-existing GRUB_CMDLINE_LINUX content) with a straightforward sequence: grep to detect whether the line exists, lineinfile to initialize an empty one when missing, then two replace tasks using a tempered-greedy-token regex that appends each NVIDIA parameter only when not already present. Idempotent and preserves any existing content in GRUB_CMDLINE_LINUX. Co-Authored-By: Claude Sonnet 4.6 --- .../profile_desktop_host/tasks/nymph.yml | 56 ++++++++----------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/ansible/roles/profile_desktop_host/tasks/nymph.yml b/ansible/roles/profile_desktop_host/tasks/nymph.yml index 2b1eea7..5433af5 100644 --- a/ansible/roles/profile_desktop_host/tasks/nymph.yml +++ b/ansible/roles/profile_desktop_host/tasks/nymph.yml @@ -1,48 +1,38 @@ --- -- name: Read /etc/default/grub +- name: Check if GRUB_CMDLINE_LINUX line exists in /etc/default/grub tags: [packages, nvidia] - ansible.builtin.slurp: - src: /etc/default/grub - register: nymph_grub_default + ansible.builtin.command: + cmd: grep -E '^GRUB_CMDLINE_LINUX=' /etc/default/grub + register: nymph_grub_cmdline_check + changed_when: false + failed_when: false -- name: Extract current GRUB_CMDLINE_LINUX value - tags: [packages, nvidia] - ansible.builtin.set_fact: - nymph_grub_cmdline_current: >- - {{ - (nymph_grub_default.content | b64decode).split('\n') - | select('match', '^GRUB_CMDLINE_LINUX="') - | first | default('GRUB_CMDLINE_LINUX=""') - | regex_replace('^GRUB_CMDLINE_LINUX="', '') - | regex_replace('"$', '') - }} - -- name: Build merged GRUB cmdline with NVIDIA parameters - tags: [packages, nvidia] - ansible.builtin.set_fact: - nymph_grub_cmdline_target: >- - {{ - ( - nymph_grub_cmdline_current - ~ (' nouveau.modeset=0' if 'nouveau.modeset=0' not in nymph_grub_cmdline_current else '') - ~ (' nvidia-drm.modeset=1' if 'nvidia-drm.modeset=1' not in nymph_grub_cmdline_current else '') - ) | trim - }} - -- name: Ensure NVIDIA kernel parameters are present in GRUB cmdline +- name: Initialize empty GRUB_CMDLINE_LINUX line if missing tags: [packages, nvidia] ansible.builtin.lineinfile: path: /etc/default/grub - regexp: '^GRUB_CMDLINE_LINUX=' - line: 'GRUB_CMDLINE_LINUX="{{ nymph_grub_cmdline_target }}"' + line: 'GRUB_CMDLINE_LINUX=""' + insertafter: '^GRUB_CMDLINE_LINUX_DEFAULT=' state: present - register: nymph_grub_cmdline + when: nymph_grub_cmdline_check.rc != 0 + register: nymph_grub_cmdline_init + +- name: Append NVIDIA kernel parameters to GRUB_CMDLINE_LINUX if missing + tags: [packages, nvidia] + ansible.builtin.replace: + path: /etc/default/grub + regexp: '^(GRUB_CMDLINE_LINUX="(?:(?!{{ item | regex_escape }})[^"])*?)"$' + replace: '\1 {{ item }}"' + loop: + - nouveau.modeset=0 + - nvidia-drm.modeset=1 + register: nymph_grub_cmdline_append - name: Regenerate GRUB configuration tags: [packages, nvidia] ansible.builtin.command: grub-mkconfig -o /boot/grub/grub.cfg changed_when: true - when: nymph_grub_cmdline is changed + when: nymph_grub_cmdline_init is changed or nymph_grub_cmdline_append is changed - name: Configure NVIDIA power management for hybrid graphics tags: [packages, nvidia]