Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lookup module fail to compute value of a template using regex_replace filter #26397

Closed
Id2ndR opened this issue Jul 4, 2017 · 1 comment · Fixed by #26465
Closed

lookup module fail to compute value of a template using regex_replace filter #26397

Id2ndR opened this issue Jul 4, 2017 · 1 comment · Fixed by #26465
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@Id2ndR
Copy link

Id2ndR commented Jul 4, 2017

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

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>
EXPECTED RESULTS

I expect to get same /tmp/test2.xml after the two tasks

ACTUAL RESULTS
@ansibot ansibot added affects_2.3 This issue/PR affects Ansible v2.3 bug_report module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Jul 4, 2017
@abadger
Copy link
Contributor

abadger commented Jul 5, 2017

The template lookup does this:

                    # 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:

            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?

@abadger abadger removed the needs_triage Needs a first human triage before being processed. label Jul 5, 2017
abadger added a commit to abadger/ansible that referenced this issue Jul 5, 2017
This brings the lookup plugin inline with what the template module does.

Fixes ansible#26397
abadger added a commit that referenced this issue Jul 5, 2017
This brings the lookup plugin inline with what the template module does.

Fixes #26397

(cherry picked from commit cf5fb0a)
abadger added a commit that referenced this issue Jul 5, 2017
This brings the lookup plugin inline with what the template module does.

Fixes #26397
abadger added a commit that referenced this issue Aug 8, 2017
This brings the lookup plugin inline with what the template module does.

Fixes #26397

(cherry picked from commit cf5fb0a)
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants