Repository: ansible/ansible

Issue:
[2.0] Jinja2 comment included in the template output
##### Issue Type:

Bug Report
##### Ansible Version:

```
$ ansible --version
ansible 2.0.0.1
  config file = /Users/bbo/.ansible.cfg
  configured module search path = Default w/o overrides
```
##### Ansible Configuration:

Blank `ansible.cfg` with default configuration.
##### Environment:

MacOS X 10.11.2 (El Capitan)
##### Summary:

[Jinja2 comments](http://jinja.pocoo.org/docs/dev/templates/#comments) syntax doesn't work in templates.
##### Steps To Reproduce:

playbook:

```

---
- hosts: all
  tasks:
    - template:
        src: template-with-comment.j2
        dest: /tmp/test-jinja2-comment
```

template `template-with-comment.j2`:

```
It should be present on rendered template
{# Jinja2 Comment #}
```
##### Expected Results:

```
$ cat /tmp/test-jinja2-comment
It should be present on rendered template
```
##### Actual Results:

```
$ cat /tmp/test-jinja2-comment
It should be present on rendered template
{# Jinja2 Comment #}
```



Discussion:
I should add that with Ansible 1.9.4 rendered file doesn't contain Jinja2 comments.

Just tested with 2.0.1.0-0.1.rc, and issue still is present. Is there any chance to be fixed with next RC release?

so i cannot reproduce the issue, in my tests the comment is removed.

I hit the same bug migrating from 1.9 to 2.0. This script reproduces the bug:

```
cat > test.conf.j2 << HERE
    # hello


{# vim: set filetype=jinja: #}
HERE

cat > test_conf.yml << HERE
- hosts: localhost
  tasks:
    - template: src=test.conf.j2 dest=/tmp/test.conf
HERE


ansible-playbook test_conf.yml -i 127.0.0.1

cat /tmp/test.conf
```

The comment is included in the output file. Including a variable in the script (e.g. `# hello {{ansible_user_id}}` in `test.conf.j2`) the comment is removed.

If useful, testing with ansible 2.0.0.2 installed by ppa on ubuntu 14.04, jinja2 reported as 2.7.2 (also tested with a pre-existing pip-installed 2.7.3).

so the issue is that when ONLY a comment is in the template it does not get removed? seems like we might be skipping jinja2 process if there are no jinja vars directives in it.

Yes, in my tests only this file got the comment through. Could you reproduce the issue now?

We have the same problem, with basic Jinja Templates that do not contain anything but text and comments.
If I add "{# {{ ansible_default_ipv4.address }} #}" to that template, everything works as expected, the comment takes precedence and all other comments are also seen as comments.


Resolving patch:
diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py
--- a/lib/ansible/template/__init__.py
+++ b/lib/ansible/template/__init__.py
@@ -370,7 +370,10 @@ def _contains_vars(self, data):
         '''
         returns True if the data contains a variable pattern
         '''
-        return self.environment.block_start_string in data or self.environment.variable_start_string in data
+        for marker in  [self.environment.block_start_string, self.environment.variable_start_string, self.environment.comment_start_string]:
+            if marker in data:
+                return True
+        return False
 
     def _convert_bare_variable(self, variable, bare_deprecated):
         '''


Test patch: