Repository: ansible/ansible

Issue:
Not using `{{` or `}}` in template file skips Jinja2 even when markers are overriden
<!---
Please do not report issues/requests related to Ansible modules here !!

Report them to the appropriate modules-core or modules-extras project:
  - https://github.com/ansible/ansible-modules-core/issues
  - https://github.com/ansible/ansible-modules-extras/issues

Also verify first that your issue/request is not already reported in GitHub
-->

Although this is an issue exhibited in a module, I suspect the code that causes this exists in this repository
##### ISSUE TYPE

<!--- Pick one below and delete the rest: -->
- Bug Report
##### COMPONENT NAME

<!--- Name of the plugin/task/feature -->

Jinja2 templating
##### ANSIBLE VERSION

<!--- Paste verbatim output from “ansible --version” between quotes below -->

```
ansible 2.1.2.0
  config file = 
  configured module search path = Default w/o overrides
```
##### CONFIGURATION

<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

```
[defaults]
inventory = ./hosts
```
##### OS / ENVIRONMENT

<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->

**N/A** but Arch Linux

```
Linux [...] 4.8.4-1-ARCH #1 SMP PREEMPT Sat Oct 22 18:26:57 CEST 2016 x86_64 GNU/Linux
```
##### SUMMARY

<!--- Explain the problem briefly -->

Not using `{{` or `}}` at any point in a Jinja2 template causes the file not to be run through Jinja2, even when `variable_start_string` and `variable_end_string` is provided (and used throughout the file)
##### STEPS TO REPRODUCE

<!---
For bugs, show exactly how to reproduce the problem.
For new features, show how the feature would be used.
-->
- Create a Jinja2 file with the following content
- Use the `template` directive

<!--- Paste example playbooks or commands between quotes below -->

```
#jinja2: variable_start_string: '[%', variable_end_string: '%]'
" [% ansible_managed %]
```

Since neither `{{` nor `}}` occurs in the file, it is not run through Jinja2. Adding either will cause the expected results to appear (with the `{{` or `}}` in the rendered output)

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

<!--- What did you expect to happen when running the steps above? -->

```
" Ansible managed: [...]
```
##### ACTUAL RESULTS

<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->

```
#jinja2: variable_start_string: '[%', variable_end_string: '%]'
" [% ansible_managed %]
```



Discussion:
I found the same bug (ansible 2.2.0.0)

My example was:

```
cat >/tmp/sample.j2 <<EOF
#jinja2:variable_start_string:'<<' , variable_end_string:'>>', block_start_string:'<%', block_end_string:'%>'
<< ansible_managed >>
EOF
ansible --connection local -m template -a "src=/tmp/sample.j2 dest=/tmp/sample.txt" localhost,
cat /tmp/sample.txt
cat >/tmp/sample.j2 <<EOF
#jinja2:variable_start_string:'<<' , variable_end_string:'>>', block_start_string:'<%', block_end_string:'%>'
<< ansible_managed >>
{{
EOF
ansible --connection local -m template -a "src=/tmp/sample.j2 dest=/tmp/sample.txt" localhost,
cat /tmp/sample.txt
```

I confirm https://github.com/ryanpineo/ansible/commit/2ec3338d4d8ac3ebcfd919e19a6a0342f1fb58b7?diff=unified fixes this.


Resolving patch:
diff --git a/lib/ansible/plugins/action/template.py b/lib/ansible/plugins/action/template.py
--- a/lib/ansible/plugins/action/template.py
+++ b/lib/ansible/plugins/action/template.py
@@ -127,7 +127,7 @@ def run(self, tmp=None, task_vars=None):
 
             old_vars = self._templar._available_variables
             self._templar.set_available_variables(temp_vars)
-            resultant = self._templar.template(template_data, preserve_trailing_newlines=True, escape_backslashes=False, convert_data=False)
+            resultant = self._templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)
             self._templar.set_available_variables(old_vars)
         except Exception as e:
             result['failed'] = True
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
@@ -328,7 +328,7 @@ def template(self, variable, convert_bare=False, preserve_trailing_newlines=True
                     if cache and sha1_hash in self._cached_result:
                         result = self._cached_result[sha1_hash]
                     else:
-                        result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, escape_backslashes=escape_backslashes, fail_on_undefined=fail_on_undefined, overrides=overrides)
+                        result = self.do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, escape_backslashes=escape_backslashes, fail_on_undefined=fail_on_undefined, overrides=overrides)
                         if convert_data and not self._no_type_regex.match(variable):
                             # if this looks like a dictionary or list, convert it to such using the safe_eval method
                             if (result.startswith("{") and not result.startswith(self.environment.variable_start_string)) or \
@@ -451,7 +451,7 @@ def _lookup(self, name, *args, **kwargs):
         else:
             raise AnsibleError("lookup plugin (%s) not found" % name)
 
-    def _do_template(self, data, preserve_trailing_newlines=True, escape_backslashes=True, fail_on_undefined=None, overrides=None):
+    def do_template(self, data, preserve_trailing_newlines=True, escape_backslashes=True, fail_on_undefined=None, overrides=None):
         # For preserving the number of input newlines in the output (used
         # later in this method)
         data_newlines = _count_newlines_from_end(data)
@@ -537,3 +537,6 @@ def _do_template(self, data, preserve_trailing_newlines=True, escape_backslashes
             else:
                 #TODO: return warning about undefined var
                 return data
+
+    # for backwards compatibility in case anyone is using old private method directly
+    _do_template = do_template


Test patch: