From f934195c9631be9c9da3d11b2f23d042dc342b7c Mon Sep 17 00:00:00 2001 From: Max Roeleveld Date: Mon, 10 Jun 2024 14:38:28 +0200 Subject: [PATCH 1/2] Use a slightly uglier but more reliable way of determining GRUB_CMDLINE_LINUX_DEFAULT. --- roles/grubcmdline/tasks/main.yml | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/roles/grubcmdline/tasks/main.yml b/roles/grubcmdline/tasks/main.yml index e191d83..a9171b6 100644 --- a/roles/grubcmdline/tasks/main.yml +++ b/roles/grubcmdline/tasks/main.yml @@ -15,16 +15,19 @@ changed_when: old_cmdline != kernel_cmdline | select() | sort | list notify: "{{ kernel_restart_handler }}" -- name: Slurp /etc/default/grub - ansible.builtin.slurp: - path: /etc/default/grub - register: grub_result - become: true - -- name: Set fact containing GRUB_CMDLINE_LINUX_DEFAULT +- name: Look for the last occurance of GRUB_CMDLINE_LINUX_DEFAULT in grub config + # In some environments, the grub config contained multiple lines defining this + # value; the last one would "win", but the change in this play would be based + # on the first one that was found, with interesting consequences. Hence this + # slightly less pure but more reliable way of fetching that line. + ansible.builtin.shell: + cmd: >- + grep -E '^GRUB_CMDLINE_LINUX_DEFAULT' /etc/default/grub | tail -1 + register: grub_cmdline_linux_default +- name: Set fact containing the contents of GRUB_CMDLINE_LINUX_DEFAULT ansible.builtin.set_fact: grub_cmdline_linux_default: >- - {{ grub_result.content | b64decode | regex_search('^GRUB_CMDLINE_LINUX_DEFAULT.*$', multiline=True) | regex_replace('^GRUB_CMDLINE_LINUX_DEFAULT="(.*)"$', '\1') }} + {{ grub_cmdline_linux_default.stdout | regex_search('^GRUB_CMDLINE_LINUX_DEFAULT.*$', multiline=True) | regex_replace('^GRUB_CMDLINE_LINUX_DEFAULT="(.*)"$', '\1') }} - name: Display GRUB_CMDLINE_DEFAULT ansible.builtin.debug: var: grub_cmdline_linux_default From fbb1be4d19c782b2e69ddbd9f277a22b77c4b7bf Mon Sep 17 00:00:00 2001 From: Max Roeleveld Date: Mon, 10 Jun 2024 15:16:38 +0200 Subject: [PATCH 2/2] Appease the linter gods and make the shell command more reliable. --- roles/grubcmdline/tasks/main.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/roles/grubcmdline/tasks/main.yml b/roles/grubcmdline/tasks/main.yml index a9171b6..c42d2f1 100644 --- a/roles/grubcmdline/tasks/main.yml +++ b/roles/grubcmdline/tasks/main.yml @@ -22,8 +22,13 @@ # slightly less pure but more reliable way of fetching that line. ansible.builtin.shell: cmd: >- - grep -E '^GRUB_CMDLINE_LINUX_DEFAULT' /etc/default/grub | tail -1 + set -o pipefail + && grep -E '^GRUB_CMDLINE_LINUX_DEFAULT' /etc/default/grub | tail -1 + args: + executable: /usr/bin/bash register: grub_cmdline_linux_default + changed_when: grub_cmdline_linux_default.rc != 0 + - name: Set fact containing the contents of GRUB_CMDLINE_LINUX_DEFAULT ansible.builtin.set_fact: grub_cmdline_linux_default: >- @@ -36,7 +41,7 @@ # We use a regex here so you can remove parameters regardless of their value, e.g to remove iommu=on you # could use the regex: ^iommu= ansible.builtin.set_fact: - grub_cmdline_linux_remove: "{{ grub_cmdline_linux_default.split() | select('search', kernel_cmdline_remove_regex) | list }}" + wrub_cmdline_linux_remove: "{{ grub_cmdline_linux_default.split() | select('search', kernel_cmdline_remove_regex) | list }}" - name: Display parameters to be removed ansible.builtin.debug: