From 13a32602ef99c521421c7f1a52d555f934cf9c39 Mon Sep 17 00:00:00 2001 From: Murat Aybars <39916128+aybarsm@users.noreply.github.com> Date: Thu, 27 Jun 2024 12:54:01 +0100 Subject: [PATCH] Empty commit message --- roles/grub/defaults/main.yml | 34 ++++++++++++++------------ roles/grub/handlers/command.yml | 19 ++++++++++++++ roles/grub/handlers/main.yml | 16 ++++-------- roles/grub/tasks/grub.yml | 14 +++++------ roles/grub/templates/grub.cfg.j2 | 6 +++-- roles/grub/vars/.gitkeep | 0 roles/network_interfaces/vars/.gitkeep | 0 7 files changed, 54 insertions(+), 35 deletions(-) create mode 100644 roles/grub/handlers/command.yml delete mode 100644 roles/grub/vars/.gitkeep delete mode 100644 roles/network_interfaces/vars/.gitkeep diff --git a/roles/grub/defaults/main.yml b/roles/grub/defaults/main.yml index a504df8..56eb985 100644 --- a/roles/grub/defaults/main.yml +++ b/roles/grub/defaults/main.yml @@ -1,26 +1,30 @@ --- linux_manage_grub: false -linux_grub_cfg_dir: /etc/default/grub.d -linux_grub_cfg_file: 50-ansible.cfg +linux_grub_dir: /etc/default/grub.d +linux_grub_file: 50-ansible.cfg +linux_grub_destination: "{{ linux_grub_dir + '/' + linux_grub_file }}" -linux_grub_backup_cfg_file: true -linux_grub_cfg_template: grub.cfg.j2 +linux_grub_backup: true +linux_grub_template: grub.cfg.j2 -linux_grub_apply_changes: false -linux_grub_update_commands: - - update-grub +# Currently only command strategy is supported +# linux_grub_change_strategy: 'command' +# Leave empty not to apply changes +linux_grub_change_strategy: '' +linux_grub_change: + - cmd: update-grub -linux_grub_config_name_prefix: GRUB_ +linux_grub_entry_prefix: GRUB_ linux_grub_inherit_cmdline_default: true linux_grub_inherit_cmdline: true -linux_grub_config_default: [] -linux_grub_config_group: [] -linux_grub_config_host: [] -linux_grub_config_all: "{{ [linux_grub_config_default, linux_grub_config_group, linux_grub_config_host] | +linux_grub_default: [] +linux_grub_group: [] +linux_grub_host: [] +# The combination strategy below is highly nested hierarchy compliant and recommended (Example provided below) +# linux_grub_host > linux_grub_group > linux_grub_default +linux_grub_all: "{{ [linux_grub_default, linux_grub_group, linux_grub_host] | community.general.lists_mergeby('name', recursive=true, list_merge='prepend') | - unique_recursive(attribute='name', recurse='value') }}" - -__linux_grub_cfg_destination: "{{ linux_grub_cfg_dir + '/' + linux_grub_cfg_file }}" + aybarsm.helper.unique_recursive(attribute='name', recurse='value') }}" diff --git a/roles/grub/handlers/command.yml b/roles/grub/handlers/command.yml new file mode 100644 index 0000000..669a345 --- /dev/null +++ b/roles/grub/handlers/command.yml @@ -0,0 +1,19 @@ +--- +- name: Apply command(s) for linux_grub changes + become: true + ansible.builtin.command: + argv: "{{ item.argv | default(omit) }}" + chdir: "{{ item.chdir | default(omit) }}" + cmd: "{{ item.cmd }}" + creates: "{{ item.creates | default(omit) }}" + expand_argument_vars: "{{ item.expand_argument_vars | default(omit) }}" + free_form: "{{ item.free_form | default(omit) }}" + removes: "{{ item.removes | default(omit) }}" + stdin: "{{ item.stdin | default(omit) }}" + stdin_add_newline: "{{ item.stdin_add_newline | default(omit) }}" + strip_empty_ends: "{{ item.strip_empty_ends | default(omit) }}" + loop: "{{ linux_grub_change | selectattr('cmd', 'defined') }}" + register: linux_grub_apply_changes_command + when: + - linux_grub_change | type_debug == 'list' + - linux_grub_change | selectattr('cmd', 'defined') | length > 0 diff --git a/roles/grub/handlers/main.yml b/roles/grub/handlers/main.yml index a417049..2c6b9ba 100644 --- a/roles/grub/handlers/main.yml +++ b/roles/grub/handlers/main.yml @@ -1,12 +1,6 @@ --- -- name: Apply grub changes - become: true - ansible.builtin.command: - cmd: "{{ item }}" - loop: "{{ linux_grub_update_commands }}" - changed_when: true - listen: "linux_grub_update_grub" - when: - - linux_grub_apply_changes | bool - - linux_grub_update_commands | type_debug == "list" - - linux_grub_update_commands | length > 0 +- name: Apply linux_grub changes + ansible.builtin.import_tasks: + file: "{{ linux_grub_change_strategy }}.yml" + listen: "linux_grub_apply_changes" + when: linux_grub_change_strategy | default('') == 'command' diff --git a/roles/grub/tasks/grub.yml b/roles/grub/tasks/grub.yml index 1ab8c28..aec773c 100644 --- a/roles/grub/tasks/grub.yml +++ b/roles/grub/tasks/grub.yml @@ -2,12 +2,12 @@ - name: Deploy grub.cfg become: true ansible.builtin.template: - src: "{{ linux_grub_cfg_template }}" - dest: "{{ __linux_grub_cfg_destination }}" - backup: "{{ linux_grub_backup_cfg_file | default(omit) | bool }}" + src: "{{ linux_grub_template }}" + dest: "{{ linux_grub_destination }}" + backup: "{{ linux_grub_backup | default(omit) | bool }}" mode: "0644" when: - - linux_grub_config_all | type_debug == 'list' - - linux_grub_config_all | length > 0 - register: linux_grub_deploy_cfg - notify: linux_grub_update_grub + - linux_grub_all | type_debug == 'list' + - linux_grub_all | length > 0 + register: linux_grub_deploy + notify: linux_grub_apply_changes diff --git a/roles/grub/templates/grub.cfg.j2 b/roles/grub/templates/grub.cfg.j2 index 94eb8f9..7d238af 100644 --- a/roles/grub/templates/grub.cfg.j2 +++ b/roles/grub/templates/grub.cfg.j2 @@ -5,12 +5,14 @@ {% set paramName = (linux_grub_config_name_prefix | upper) ~ (param.name | upper) %} {% if 'value' in param %} {% if (param.value | type_debug) == 'list' %} -{% if ((param.name | lower) == 'cmdline_linux_default' and (linux_grub_inherit_cmdline_default | bool)) or ((param.name | lower) == 'cmdline_linux' and (linux_grub_inherit_cmdline | bool)) %} +{% set inheritCmdlineDefault = param.name | lower == 'cmdline_linux_default' and linux_grub_inherit_cmdline_default | bool %} +{% set inheritCmdline = param.name | lower == 'cmdline_linux' and linux_grub_inherit_cmdline | bool %} +{% if inheritCmdlineDefault or inheritCmdline %} {% set paramValPrefix = '$' ~ paramName ~ ' ' %} {% else %} {% set paramValPrefix = '' %} {% endif %} -{{ paramName }}="{{ paramValPrefix }}{{ param.value | flatten_query('name', 'value', '=', ' ') }}" +{{ paramName }}="{{ paramValPrefix }}{{ param.value | aybarsm.helper.flatten_query('name', 'value', '=', ' ') }}" {% else %} {{ paramName }}="{{ param.value }}" {% endif %} diff --git a/roles/grub/vars/.gitkeep b/roles/grub/vars/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/roles/network_interfaces/vars/.gitkeep b/roles/network_interfaces/vars/.gitkeep deleted file mode 100644 index e69de29..0000000