From ff20ab7d4439ad6e5ca099c428ac6d3f25a154e7 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Thu, 2 Mar 2017 17:02:07 -0600 Subject: [PATCH] Warn when jinja2 delimiters are found in a when statement (#20312) * Warn when jinja2 delimiters are found in a when statement * Use templar._clean_data to look for templating --- lib/ansible/playbook/conditional.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/ansible/playbook/conditional.py b/lib/ansible/playbook/conditional.py index aa0926eb53..ac342c9089 100644 --- a/lib/ansible/playbook/conditional.py +++ b/lib/ansible/playbook/conditional.py @@ -32,6 +32,12 @@ from ansible.template import Templar from ansible.template.safe_eval import safe_eval from ansible.module_utils._text import to_native +try: + from __main__ import display +except ImportError: + from ansible.utils.display import Display + display = Display() + DEFINED_REGEX = re.compile(r'(hostvars\[.+\]|[\w_]+)\s+(not\s+is|is|is\s+not)\s+(defined|undefined)') LOOKUP_REGEX = re.compile(r'lookup\s*\(') VALID_VAR_REGEX = re.compile("^[_A-Za-z][_a-zA-Z0-9]*$") @@ -132,6 +138,11 @@ class Conditional: if conditional in all_vars and VALID_VAR_REGEX.match(conditional): conditional = all_vars[conditional] + if templar._clean_data(conditional) != conditional: + display.warning('when statements should not include jinja2 ' + 'templating delimiters such as {{ }} or {%% %%}. ' + 'Found: %s' % conditional) + # make sure the templar is using the variables specified with this method templar.set_available_variables(variables=all_vars)