Repository: ansible/ansible

Issue:
lookup module fail to compute value of a template using regex_replace filter

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
template

##### ANSIBLE VERSION
```
ansible 2.3.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, May 10 2017, 20:04:28) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]
```

##### OS / ENVIRONMENT
N/A

##### SUMMARY
template action and template lookup give different results using regex_replace filter.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->
Playbook
```
- template:
    dest: /tmp/test.xml
    src: test.xml.j2
- copy:
    content: "{{ lookup( 'template', 'test.xml.j2' ) }}"
    dest: /tmp/test2.xml
```

template test.xml.j2
```
<command>ssh {{ command| regex_replace('"(.*)"', '"\\"\1\\""') }} #1</command>
<command>ssh {{ command| regex_replace('"(.*)"', '"\\"\\1\\""') }} #2</command>
{% set workaround_repl = '"\\"\\1\\""' %}
<command>ssh {{ command| regex_replace('"(.*)"', workaround_repl) }} #3</command>
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
I expect to get same /tmp/test2.xml after the two tasks

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
- when escaping once, template module fail to display the reference (#1)
- when escaping twice, lookup module fail to dereference the reference (#2)
- using workaround_repl (escaping twice), it work's normally (#3)



Discussion:
The template lookup does this:
``` python
                    # do the templating
                    res = self._templar.template(template_data, preserve_trailing_newlines=True,                     convert_data=convert_data_p)
```

whereas the template action plugin does this:
``` python
            resultant = self._templar.do_template(template_data, preserve_trailing_newlines=True,                    escape_backslashes=False)
```

The difference in escape_backslashes is the problem here.  escape_backslashes was added to the template action plugin due to #12198.  I believe the right fix is to add the same thing (escape_backslashes=False) to the lookup plugin.

@bcoca, @jimi-c opinions from you?

Resolving patch:
diff --git a/lib/ansible/plugins/lookup/template.py b/lib/ansible/plugins/lookup/template.py
--- a/lib/ansible/plugins/lookup/template.py
+++ b/lib/ansible/plugins/lookup/template.py
@@ -68,7 +68,8 @@ def run(self, terms, variables, **kwargs):
                     self._templar.set_available_variables(temp_vars)
 
                     # do the templating
-                    res = self._templar.template(template_data, preserve_trailing_newlines=True, convert_data=convert_data_p)
+                    res = self._templar.template(template_data, preserve_trailing_newlines=True,
+                                                 convert_data=convert_data_p, escape_backslashes=False)
                     ret.append(res)
             else:
                 raise AnsibleError("the template file %s could not be found for the lookup" % term)


Test patch: