From 23324bdda015256f90737ae93351d0ce71da01b2 Mon Sep 17 00:00:00 2001 From: Will Thames Date: Wed, 31 May 2017 05:48:45 +1000 Subject: [PATCH] Ensure `when` warning is checked before expanding (#25092) The `when` condition templating warning should only happen if the condition itself contains templating, not if variables in the condition are themselves composed through templating Before ``` vars: x: hello y: "{{ x }}" tasks: - debug: msg=hello when: y ``` would fire a warning because `y` would get expanded to `{{ x }}`. This checks whether a warning is required prior to expansion. --- lib/ansible/playbook/conditional.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/ansible/playbook/conditional.py b/lib/ansible/playbook/conditional.py index 9ce0c5de1e1..b6836144624 100644 --- a/lib/ansible/playbook/conditional.py +++ b/lib/ansible/playbook/conditional.py @@ -127,6 +127,11 @@ class Conditional: if conditional is None or conditional == '': return True + if templar.is_template(conditional): + display.warning('when statements should not include jinja2 ' + 'templating delimiters such as {{ }} or {%% %%}. ' + 'Found: %s' % conditional) + # pull the "bare" var out, which allows for nested conditionals # and things like: # - assert: @@ -137,11 +142,6 @@ class Conditional: if conditional in all_vars and VALID_VAR_REGEX.match(conditional): conditional = all_vars[conditional] - if templar.is_template(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)