Skip to content

Commit b3f3d9e

Browse files
committed
Remove Jinja from when statement
This change adresses a problem reported by ansible-lint that the when statement contains Jinja expressions. This affects Ansible remediations of rules using the grub2_bootloader_argument template if the rule is parametrized by a variable. For example, rule grub2_l1tf_argument is affected by this problem. Addressing: ``` no-jinja-when: No Jinja2 in when. ```
1 parent da027a3 commit b3f3d9e

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

shared/macros/10-ansible.jinja

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1479,9 +1479,11 @@ Part of the grub2_bootloader_argument template.
14791479
:type arg_name: str
14801480
:param arg_name_value: Kernel command line argument concatenated with the value of this argument using an equal sign, eg. "noexec=off".
14811481
:type arg_name_value: str
1482+
:param arg_variable: Name of the XCCDF Value parametrizing the rule (can be None)
1483+
:type arg_variable: str
14821484

14831485
#}}
1484-
{{%- macro ansible_grub2_bootloader_argument(arg_name, arg_name_value) -%}}
1486+
{{%- macro ansible_grub2_bootloader_argument(arg_name, arg_name_value, arg_variable) -%}}
14851487
{{% if 'ubuntu' in product or 'debian' in product or product in ['ol7', 'sle12', 'sle15', 'slmicro5'] %}}
14861488
- name: Check {{{ arg_name }}} argument exists
14871489
ansible.builtin.command: grep '^\s*GRUB_CMDLINE_LINUX=.*{{{ arg_name }}}=' /etc/default/grub
@@ -1541,7 +1543,11 @@ Part of the grub2_bootloader_argument template.
15411543

15421544
- name: Update grub defaults and the bootloader menu
15431545
ansible.builtin.command: /sbin/grubby --update-kernel=ALL --args="{{{ arg_name_value }}}"
1546+
{{%- if arg_variable %}}
1547+
when: (grubby_info.stdout is not search('{{{ arg_name }}}=' ~ {{{ arg_variable }}})) or ((etc_default_grub['content'] | b64decode) is not search('{{{ arg_name }}}=' ~ {{{ arg_variable }}}))
1548+
{{% else %}}
15441549
when: (grubby_info.stdout is not search('{{{ arg_name_value }}}')) or ((etc_default_grub['content'] | b64decode) is not search('{{{ arg_name_value }}}'))
1550+
{{% endif %}}
15451551
{{% endif -%}}
15461552
{{%- endmacro -%}}
15471553

shared/templates/grub2_bootloader_argument/ansible.template

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@
88
{{{ ansible_instantiate_variables(ARG_VARIABLE) }}}
99
{{% set ARG_NAME_VALUE = ARG_NAME ~ "={{ " ~ ARG_VARIABLE ~ " }}" %}}
1010
{{% endif %}}
11-
12-
{{{ ansible_grub2_bootloader_argument(ARG_NAME, ARG_NAME_VALUE) }}}
11+
{{{ ansible_grub2_bootloader_argument(ARG_NAME, ARG_NAME_VALUE, ARG_VARIABLE) }}}

0 commit comments

Comments
 (0)