Skip to content

Commit

Permalink
Add parent kernel name list into bootloader_settings
Browse files Browse the repository at this point in the history
  • Loading branch information
spetrosi committed Nov 7, 2023
1 parent ab737af commit 3f0d7a6
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 55 deletions.
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. `<kernel-path>` 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

Expand Down
2 changes: 1 addition & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
bootloader_settings: []
bootloader_settings: {}
bootloader_timeout: 5

bootloader_password: null
Expand Down
30 changes: 3 additions & 27 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 12 additions & 8 deletions tasks/modify_settings.yml
Original file line number Diff line number Diff line change
@@ -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: >-

Check warning on line 6 in tasks/modify_settings.yml

View workflow job for this annotation

GitHub Actions / ansible_lint

jinja[spacing]

Jinja2 spacing could be improved: {{ __bootloader_with_value | ternary( __bootloader_kernel_setting.name | string + '=' + __bootloader_kernel_setting.value | d() | string, __bootloader_kernel_setting.name) }} -> {{ __bootloader_with_value | ternary(__bootloader_kernel_setting.name | string + '=' + __bootloader_kernel_setting.value | d() | string, __bootloader_kernel_setting.name) }}
{{ 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)
Expand Down
33 changes: 33 additions & 0 deletions tasks/rm_mod_settings.yml
Original file line number Diff line number Diff line change
@@ -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
49 changes: 36 additions & 13 deletions tests/tests_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 17 in tests/tests_settings.yml

View workflow job for this annotation

GitHub Actions / ansible_lint

yaml[line-length]

Line too long (84 > 80 characters)
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -85,6 +107,7 @@
- name: Set an existing variable, should report not changed
vars:
bootloader_settings:
ALL:
- name: console

Check failure on line 111 in tests/tests_settings.yml

View workflow job for this annotation

GitHub Actions / ansible_lint

yaml[indentation]

Wrong indentation: expected 12 but found 10
value: tty0
state: present
Expand Down

0 comments on commit 3f0d7a6

Please sign in to comment.