Repository: ansible/ansible

Issue:
when condition checks complain about templated variables
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
when templating checking

##### ANSIBLE VERSION
```
ansible 2.4.0 (devel 2af5556901) last updated 2017/05/26 11:55:34 (GMT +1000)
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/will/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/will/src/ansible/lib/ansible
  executable location = /home/will/src/ansible/bin/ansible
  python version = 2.7.13 (default, Jan 12 2017, 17:59:37) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]
```

##### CONFIGURATION


##### OS / ENVIRONMENT
N/A

##### SUMMARY
ansible-playbook is warning about `when` conditions that are variables that are composed of other variables (e.g. anything of the form `x: "{{ y }}"` and then doing `when: x`

##### STEPS TO REPRODUCE
ansible-playbook playbook.yml

```yaml
$ cat playbook.yml 
- hosts: localhost
  connection: local
  gather_facts: False

  vars:
    x: hello
    y: world
    a_list:
      - "{{ x }}"
      - "{{ y }}"

  tasks:
    - debug:
        var: a_list
      when: a_list
```

##### EXPECTED RESULTS
As below without the WARNING

##### ACTUAL RESULTS
```

$ ansible-playbook playbook.yml -v
Using /etc/ansible/ansible.cfg as config file
 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [localhost] *********************************************************************************************************************************************

TASK [debug] *************************************************************************************************************************************************
 [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: [u'{{ x }}', u'{{ y }}']

ok: [localhost] => {
    "a_list": [
        "hello", 
        "world"
    ]
}

PLAY RECAP ***************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0   

```



Discussion:
I think we can resolve this by just moving the `is_template` check a little higher:

```diff
diff --git a/lib/ansible/playbook/conditional.py b/lib/ansible/playbook/conditional.py
index 9ce0c5d..b683614 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)
 ```

That get's rid of your warning, but still works for the case where you have explicitly done:

```
    - debug:
        var: a_list
      when: "{{ a_list }}"
```
@sivel sounds good to me. I can make a PR unless you have one ready to go?
I reverted it while working on something else. Feel free to submit the PR with this change.
Sure, will do, thanks

Resolving patch:
diff --git a/lib/ansible/playbook/conditional.py b/lib/ansible/playbook/conditional.py
--- a/lib/ansible/playbook/conditional.py
+++ b/lib/ansible/playbook/conditional.py
@@ -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:
@@ -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)
 


Test patch: