mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-28 11:10:21 -07:00
improve conditional warnings (#57190)
* Fix order for warning on templated conditionals Fix bare variable warnings when the variable is a boolean * changelog * Add tests for cases that should and should not give warnings If the behavior may change when the default behavior for CONDITIONAL_BARE_VARS becomes False there should be a warning. Boolean type conditionals will not change in behavior so don't warn. * oops, forgot to add files * typo
This commit is contained in:
parent
fecffea370
commit
21cd24a0dd
5 changed files with 54 additions and 6 deletions
|
@ -114,16 +114,17 @@ class Conditional:
|
|||
if isinstance(conditional, bool):
|
||||
return conditional
|
||||
|
||||
if C.CONDITIONAL_BARE_VARS:
|
||||
if conditional in all_vars and VALID_VAR_REGEX.match(conditional):
|
||||
display.deprecated('evaluating %s as a bare variable, this behaviour will go away and you might need to add |bool'
|
||||
' to the expression in the future. Also see CONDITIONAL_BARE_VARS configuration toggle.' % conditional, "2.12")
|
||||
conditional = all_vars[conditional]
|
||||
|
||||
if templar.is_template(conditional):
|
||||
display.warning('conditional statements should not include jinja2 '
|
||||
'templating delimiters such as {{ }} or {%% %%}. '
|
||||
'Found: %s' % conditional)
|
||||
|
||||
bare_vars_warning = False
|
||||
if C.CONDITIONAL_BARE_VARS:
|
||||
if conditional in all_vars and VALID_VAR_REGEX.match(conditional):
|
||||
conditional = all_vars[conditional]
|
||||
bare_vars_warning = True
|
||||
|
||||
# make sure the templar is using the variables specified with this method
|
||||
templar.available_variables = all_vars
|
||||
|
||||
|
@ -131,6 +132,9 @@ class Conditional:
|
|||
# if the conditional is "unsafe", disable lookups
|
||||
disable_lookups = hasattr(conditional, '__UNSAFE__')
|
||||
conditional = templar.template(conditional, disable_lookups=disable_lookups)
|
||||
if bare_vars_warning and not isinstance(conditional, bool):
|
||||
display.deprecated('evaluating %s as a bare variable, this behaviour will go away and you might need to add |bool'
|
||||
' to the expression in the future. Also see CONDITIONAL_BARE_VARS configuration toggle.' % conditional, "2.12")
|
||||
if not isinstance(conditional, text_type) or conditional == "":
|
||||
return conditional
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue