From 3f0d7a670c87f810b07c4582c83419d18face2fe Mon Sep 17 00:00:00 2001 From: Sergei Petrosian Date: Tue, 7 Nov 2023 16:48:43 +0100 Subject: [PATCH] Add parent kernel name list into bootloader_settings --- README.md | 37 ++++++++++++++++++++++++----- defaults/main.yml | 2 +- tasks/main.yml | 30 +++--------------------- tasks/modify_settings.yml | 20 +++++++++------- tasks/rm_mod_settings.yml | 33 ++++++++++++++++++++++++++ tests/tests_settings.yml | 49 ++++++++++++++++++++++++++++----------- 6 files changed, 116 insertions(+), 55 deletions(-) create mode 100644 tasks/rm_mod_settings.yml diff --git a/README.md b/README.md index ce305b2..74cdfbc 100644 --- a/README.md +++ b/README.md @@ -20,21 +20,46 @@ Type: `bool` ### bootloader_settings -A `list` of kernel command line parameters. +With this variable, list kernels and their command line parameters that you want to set. -The role updates `ALL` kernels by default. +You can specify one or more of the following kernels: +1. `` a specific kernel path to create or update with the configuration provided. +2. `DEFAULT` to update the default entry. +3. `ALL` to update all of the entries. +Specifying or `ALL` updates the default entry and all of the entries, respectively. -Each `dict` might has one or more of the following keys: +Each kernel `dict` might has one or more of the following keys: * `name` - The name of the setting. `name` is omitted when using `replaced`. * `value` - The value for the setting. You must omit `value` if the setting has no value, e.g. `quiet`. * `state` - `present` (default) or `absent`. The value `absent` means to remove a setting with `name` name - name must be provided. * `previous` - Optional - the only value is `replaced` - this is used to specify that the previous settings should be replaced with the given settings. -The role replaces parameters on the `DEFAULT` kernel to avoid overwriting all kernels. -Default: `[]` +Example: -Type: `list` +```yaml +bootloader_settings: + /boot/vmlinuz-0-rescue-1: + - name: console + value: tty0 + - name: print-fatal-signals + value: 1 + - name: no_timer_check + state: present + - name: quiet + - name: debug + - previous: replaced + ALL: + - name: debug + state: present + DEFAULT: + - name: quiet + state: present +``` + +Default: `{}` + +Type: `dict` ### bootloader_timeout diff --git a/defaults/main.yml b/defaults/main.yml index d07a753..5399e83 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,5 +1,5 @@ --- -bootloader_settings: [] +bootloader_settings: {} bootloader_timeout: 5 bootloader_password: null diff --git a/tasks/main.yml b/tasks/main.yml index 9c46961..d59b624 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -11,34 +11,10 @@ - name: Collect bootloader facts bootloader_facts: when: bootloader_gather_facts | bool -- name: Remove the existing boot settings - when: bootloader_settings | - selectattr('previous', 'defined') | - selectattr('previous', 'match', '^replaced$') | - list | length > 0 - block: - - name: Get existing boot settings - shell: >- - set -o pipefail; - grubby --info=DEFAULT | grep '^args="' | sed 's/^args=//' - changed_when: false - register: __bootloader_args - - - name: Remove all boot settings - command: >- - grubby - --update-kernel=DEFAULT - --remove-args={{ __bootloader_args.stdout }} - changed_when: true - # length > 2 for two quotes - when: __bootloader_args.stdout | length > 2 - notify: - - Fix default kernel boot parameters - - Reboot system -- name: Modify boot settings - include_tasks: modify_settings.yml - loop: "{{ bootloader_settings }}" +- name: Remove for previous replaced then modify boot settings + include_tasks: rm_mod_settings.yml + with_dict: "{{ bootloader_settings }}" - name: Update boot loader timeout configuration lineinfile: diff --git a/tasks/modify_settings.yml b/tasks/modify_settings.yml index ad6fcff..ab60962 100644 --- a/tasks/modify_settings.yml +++ b/tasks/modify_settings.yml @@ -1,29 +1,33 @@ # SPDX-License-Identifier: MIT --- - name: Configure setting with value - when: item.previous | d() != 'replaced' + when: __bootloader_kernel_setting.previous | d() != 'replaced' vars: __bootloader_with_value: >- - {{ item.name is defined and item.value is defined }} + {{ __bootloader_kernel_setting.name is defined and + __bootloader_kernel_setting.value is defined }} __bootloader_setting: >- {{ __bootloader_with_value | - ternary(item.name | string + '=' + item.value | d() | string, - item.name) }} - __bootloader_absent: "{{ item.state | d('present') == 'absent' }}" + ternary( + __bootloader_kernel_setting.name | string + '=' + + __bootloader_kernel_setting.value | d() | string, + __bootloader_kernel_setting.name) }} + __bootloader_absent: >- + {{ __bootloader_kernel_setting.state | d('present') == 'absent' }} block: - name: Check boot setting {{ __bootloader_setting }} shell: >- set -euo pipefail; - grubby --info=ALL | grep '^args="' | sed 's/^args=//' + grubby --info={{ item.key }} | grep '^args="' | sed 's/^args=//' register: __bootloader_check_setting changed_when: false - name: Configure boot setting {{ __bootloader_setting }} command: >- grubby - --update-kernel=ALL + --update-kernel={{ item.key }} {{ __bootloader_absent | ternary('--remove-args=', '--args=') - }}{{ __bootloader_setting | quote }} + }}{{ __bootloader_setting }} changed_when: true when: >- ((__bootloader_setting not in __bootloader_check_setting.stdout) | bool) diff --git a/tasks/rm_mod_settings.yml b/tasks/rm_mod_settings.yml new file mode 100644 index 0000000..0bcc2fc --- /dev/null +++ b/tasks/rm_mod_settings.yml @@ -0,0 +1,33 @@ +# SPDX-License-Identifier: MIT +--- +- name: Remove the existing boot settings + when: item.value | + selectattr('previous', 'defined') | + selectattr('previous', 'match', '^replaced$') | + list | length > 0 + block: + - name: Get existing boot settings for {{ item.key }} + shell: >- + set -o pipefail; + grubby --info={{ item.key }} | grep '^args="' | sed 's/^args=//' + changed_when: false + register: __bootloader_args + + - name: Remove all boot settings for {{ item.key }} + command: >- + grubby + --update-kernel={{ item.key }} + --remove-args={{ __bootloader_args.stdout }} + changed_when: true + # length > 2 for two quotes + when: __bootloader_args.stdout | length > 2 + notify: + - Fix default kernel boot parameters + - Reboot system + +- name: Configure setting with value + when: item.value.previous | d() != 'replaced' + include_tasks: modify_settings.yml + loop: "{{ item.value }}" + loop_control: + loop_var: __bootloader_kernel_setting diff --git a/tests/tests_settings.yml b/tests/tests_settings.yml index c7b6d39..cca2437 100644 --- a/tests/tests_settings.yml +++ b/tests/tests_settings.yml @@ -8,18 +8,39 @@ vars: bootloader_reboot_ok: true tasks: + - name: Get bootloader_facts + vars: + bootloader_gather_facts: true + include_role: + name: linux-system-roles.bootloader + + - name: Verify that the default bootloader is correct in bootloader_gather_facts + vars: + default_bootloader: >- + {{ bootloader_facts | selectattr('index', 'search', '0') | first }} + shell: >- + set -euo pipefail; + grubby --info=DEFAULT; + grubby --info=DEFAULT | + grep -P + 'kernel="{{ default_bootloader }}"' + changed_when: false + - name: Replace configuration with settings vars: bootloader_settings: - - name: console - value: tty0 - - name: print-fatal-signals - value: 1 - - name: no_timer_check - state: present - - name: quiet - - name: debug - - previous: replaced + DEFAULT: + - name: console + value: tty0 + - name: print-fatal-signals + value: 1 + - name: no_timer_check + state: present + - name: quiet + - previous: replaced + ALL: + - name: debug + state: present bootloader_timeout: 6 include_role: name: linux-system-roles.bootloader @@ -51,10 +72,11 @@ - name: Change some settings vars: bootloader_settings: - - name: quiet - state: absent - - name: debug - state: absent + ALL: + - name: quiet + state: absent + - name: debug + state: absent bootloader_timeout: 4 include_role: name: linux-system-roles.bootloader @@ -85,6 +107,7 @@ - name: Set an existing variable, should report not changed vars: bootloader_settings: + ALL: - name: console value: tty0 state: present