Skip to content

Commit

Permalink
Ensure when warning is checked before expanding (#25092)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
willthames authored and jctanner committed May 30, 2017
1 parent 69efb61 commit 23324bd
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/ansible/playbook/conditional.py
Expand Up @@ -127,6 +127,11 @@ def _check_conditional(self, conditional, templar, all_vars):
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:
Expand All @@ -137,11 +142,6 @@ def _check_conditional(self, conditional, templar, all_vars):
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)

Expand Down

0 comments on commit 23324bd

Please sign in to comment.