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 <noreply@anthropic.com>
This commit is contained in:
Fabio Scotto di Santolo
2026-05-14 00:05:13 +02:00
parent 62e42f90cb
commit 15c6d97671

View File

@@ -1,48 +1,38 @@
--- ---
- name: Read /etc/default/grub - name: Check if GRUB_CMDLINE_LINUX line exists in /etc/default/grub
tags: [packages, nvidia] tags: [packages, nvidia]
ansible.builtin.slurp: ansible.builtin.command:
src: /etc/default/grub cmd: grep -E '^GRUB_CMDLINE_LINUX=' /etc/default/grub
register: nymph_grub_default register: nymph_grub_cmdline_check
changed_when: false
failed_when: false
- name: Extract current GRUB_CMDLINE_LINUX value - name: Initialize empty GRUB_CMDLINE_LINUX line if missing
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] tags: [packages, nvidia]
ansible.builtin.lineinfile: ansible.builtin.lineinfile:
path: /etc/default/grub path: /etc/default/grub
regexp: '^GRUB_CMDLINE_LINUX=' line: 'GRUB_CMDLINE_LINUX=""'
line: 'GRUB_CMDLINE_LINUX="{{ nymph_grub_cmdline_target }}"' insertafter: '^GRUB_CMDLINE_LINUX_DEFAULT='
state: present 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 - name: Regenerate GRUB configuration
tags: [packages, nvidia] tags: [packages, nvidia]
ansible.builtin.command: grub-mkconfig -o /boot/grub/grub.cfg ansible.builtin.command: grub-mkconfig -o /boot/grub/grub.cfg
changed_when: true 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 - name: Configure NVIDIA power management for hybrid graphics
tags: [packages, nvidia] tags: [packages, nvidia]