mirror of
https://github.com/fscotto/infra.git
synced 2026-05-30 15:39:58 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
- 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
|
||||||
backrefs: true
|
regexp: '^GRUB_CMDLINE_LINUX='
|
||||||
regexp: '^(GRUB_CMDLINE_LINUX="(?!.*{{ item | regex_escape }}).*)"$'
|
line: 'GRUB_CMDLINE_LINUX="{{ nymph_grub_cmdline_target }}"'
|
||||||
line: '\1 {{ item }}"'
|
state: present
|
||||||
loop:
|
|
||||||
- nouveau.modeset=0
|
|
||||||
- nvidia-drm.modeset=1
|
|
||||||
register: nymph_grub_cmdline
|
register: nymph_grub_cmdline
|
||||||
|
|
||||||
- name: Regenerate GRUB configuration
|
- name: Regenerate GRUB configuration
|
||||||
|
|||||||
Reference in New Issue
Block a user