summaryrefslogtreecommitdiff
path: root/lib/ansible/playbook
diff options
context:
space:
mode:
authorSloane Hertel <shertel@redhat.com>2019-06-03 06:12:33 -0400
committerToshio Kuratomi <a.badger@gmail.com>2019-06-11 19:44:01 -0700
commit95b1b42d4f1b2fcb8280208eedcb4b8faa6b9359 (patch)
tree89eeae22625020d1449a69d3a22779ba7785ccbd /lib/ansible/playbook
parent86cb4089ca369618714333b35915a4b14082fdfe (diff)
downloadansible-95b1b42d4f1b2fcb8280208eedcb4b8faa6b9359.tar.gz
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 (cherry picked from commit 21cd24a0dddb9e2f12c431e65f44df538654213d)
Diffstat (limited to 'lib/ansible/playbook')
-rw-r--r--lib/ansible/playbook/conditional.py16
1 files changed, 10 insertions, 6 deletions
diff --git a/lib/ansible/playbook/conditional.py b/lib/ansible/playbook/conditional.py
index 04b49d7167..016a71a4e2 100644
--- a/lib/ansible/playbook/conditional.py
+++ b/lib/ansible/playbook/conditional.py
@@ -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.set_available_variables(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