From 5a3d32cb0eb7cd947d535c5444a8954aeb4d7de7 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Wed, 13 May 2026 23:54:49 +0200 Subject: [PATCH] Rewrite GRUB cmdline merge using slurp + fact-based merge The previous lineinfile-with-backrefs negative-lookahead regex was not appending NVIDIA parameters when missing. Switch to a clearer approach: slurp the file, extract the current GRUB_CMDLINE_LINUX value, build a merged target value that appends missing NVIDIA params, then write back the line. lineinfile sees no diff when the target matches the current line, preserving idempotency. Co-Authored-By: Claude Sonnet 4.6 --- .../profile_desktop_host/tasks/nymph.yml | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/ansible/roles/profile_desktop_host/tasks/nymph.yml b/ansible/roles/profile_desktop_host/tasks/nymph.yml index 7098329..2b1eea7 100644 --- a/ansible/roles/profile_desktop_host/tasks/nymph.yml +++ b/ansible/roles/profile_desktop_host/tasks/nymph.yml @@ -1,14 +1,41 @@ --- +- name: Read /etc/default/grub + tags: [packages, nvidia] + ansible.builtin.slurp: + src: /etc/default/grub + register: nymph_grub_default + +- 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 tags: [packages, nvidia] ansible.builtin.lineinfile: path: /etc/default/grub - backrefs: true - regexp: '^(GRUB_CMDLINE_LINUX="(?!.*{{ item | regex_escape }}).*)"$' - line: '\1 {{ item }}"' - loop: - - nouveau.modeset=0 - - nvidia-drm.modeset=1 + regexp: '^GRUB_CMDLINE_LINUX=' + line: 'GRUB_CMDLINE_LINUX="{{ nymph_grub_cmdline_target }}"' + state: present register: nymph_grub_cmdline - name: Regenerate GRUB configuration