# platform = multi_platform_rhel,multi_platform_fedora,multi_platform_ol,multi_platform_rhv
# reboot = true
# strategy = restrict
# complexity = medium
# disruption = low

{{% if product in ["rhel7", "ol7"] %}}
- name: check {{{ ARG_NAME }}} argument exists
  command: grep 'GRUB_CMDLINE_LINUX.*{{{ ARG_NAME }}}=' /etc/default/grub
  failed_when: False
  register: argcheck

- name: replace existing {{{ ARG_NAME }}} argument
  replace:
      path: /etc/default/grub
      regexp: '{{{ ARG_NAME }}}=\w+'
      replace: '{{{ ARG_NAME_VALUE }}}'
  when: argcheck.rc == 0

- name: add {{{ ARG_NAME }}} argument
  replace:
      path: /etc/default/grub
      regexp: '(GRUB_CMDLINE_LINUX=.*)"'
      replace: '\1 {{{ ARG_NAME_VALUE }}}"'
  when: argcheck.rc != 0

- name: update bootloader menu
  command: /sbin/grubby --update-kernel=ALL --args="{{{ ARG_NAME_VALUE }}}"

{{% else %}}

- name: get current kernel parameters
  command: '/usr/bin/grub2-editenv - list'
  register: kernelopts
  changed_when: False

- name: Update the bootloader menu
  command: /usr/bin/grub2-editenv - set "{{ item }} {{{ ARG_NAME_VALUE }}}"
  with_items: "{{ kernelopts.stdout_lines | select('match', '^kernelopts.*') | list }}"
  when:
    - kernelopts.stdout_lines is defined
    - kernelopts.stdout_lines | length > 0
    - kernelopts.stdout | regex_search('^kernelopts=(?:.*\s)?{{{ ARG_NAME_VALUE }}}(?:\s.*)?$', multiline=True) is none

{{% endif %}}
