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

{{ "test" | regex_replace('^(.).*', '\\1') }} produces a literal '\1' in v2, when used in a standalone template #12198

Closed
mgedmin opened this issue Sep 2, 2015 · 3 comments
Labels
bug This issue/PR relates to a bug. P2 Priority 2 - Issue Blocks Release
Milestone

Comments

@mgedmin
Copy link
Contributor

mgedmin commented Sep 2, 2015

Issue Type: Bug Report
Ansible Version:

ansible 2.0.0 (devel e8d7fafc52) last updated 2015/09/02 09:36:18 (GMT +300)
  lib/ansible/modules/core: (detached HEAD bbcfb1092a) last updated 2015/09/02 08:52:41 (GMT +300)
  lib/ansible/modules/extras: (detached HEAD b8803306d1) last updated 2015/09/02 08:52:41 (GMT +300)
  config file = /etc/ansible/ansible.cfg
  configured module search path = /usr/share/ansible

Ansible Configuration: none whatsoever
Environment: Ubuntu 15.04
Summary:

Commit 7f5080f introduced a regression: now backslashes in standalone template files are doubled, breaking them.

Steps To Reproduce:

$ cat test.yml
- hosts: localhost
  gather_facts: no
  tasks:
    - template: src=template.j2 dest=/tmp/output

$ cat template.j2
This should expand to the letter 't':
'{{ "test" | regex_replace('^(.).*', '\\1') }}'

$ ansible-playbook test.yml
 [WARNING]: provided hosts list is empty, only localhost is available


PLAY ***************************************************************************

TASK [template dest=/tmp/output src=template.j2] *******************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0   

Expected Results:

(This is what expect, and also what I get with Ansible 1.9.2.)

$ cat /tmp/output
This should expand to the letter 't':
't'

also note that the file ought to have a trailing newline, since there was one in the source template:

$ xxd template.j2 
0000000: 5468 6973 2073 686f 756c 6420 6578 7061  This should expa
0000010: 6e64 2074 6f20 7468 6520 6c65 7474 6572  nd to the letter
0000020: 2027 7427 3a0a 277b 7b20 2274 6573 7422   't':.'{{ "test"
0000030: 207c 2072 6567 6578 5f72 6570 6c61 6365   | regex_replace
0000040: 2827 5e28 2e29 2e2a 272c 2027 5c5c 3127  ('^(.).*', '\\1'
0000050: 2920 7d7d 270a                           ) }}'.

$ xxd /tmp/output
0000000: 5468 6973 2073 686f 756c 6420 6578 7061  This should expa
0000010: 6e64 2074 6f20 7468 6520 6c65 7474 6572  nd to the letter
0000020: 2027 7427 3a0a 2774 270a                  't':.'t'.

Actual Results:

$ cat /tmp/output
This should expand to the letter 't':
'\1'

also note that the file lacks a trailing newline:

$ xxd /tmp/output
0000000: 5468 6973 2073 686f 756c 6420 6578 7061  This should expa
0000010: 6e64 2074 6f20 7468 6520 6c65 7474 6572  nd to the letter
0000020: 2027 7427 3a0a 275c 3127                  't':.'\1'
@mgedmin
Copy link
Contributor Author

mgedmin commented Sep 2, 2015

Reverting 7f5080f fixes this (but regresses #11891 probably).

Perhaps the hack introduced in 7f5080f could be applied to templates coming from YAML files, but not to standalone templates?

@mgedmin
Copy link
Contributor Author

mgedmin commented Sep 2, 2015

Some points about the trailing newline bit:

  • Trailing newlines are important. E.g., cron ignores the last line in a crontab if it lacks a newline. People who generate crontabs from templates might be unpleasantly surprised.
  • Reverting 7f5080f fixes the trailing newline change, which is why I didn't file a separate issue about that. Something about parsing the template and joining it back eats the trailing newline.
  • If the fix is to skip _preserve_backslashes for standalone template files (e.g. when invoked from the 'template' module), somebody ought to investigate if trailing newlines change for things defined directly in YAML files, e.g. when using the literal block syntax.

abadger added a commit that referenced this issue Sep 2, 2015
Fix for one half of hte bug reported in #12198
@abadger abadger closed this as completed in e218bf8 Sep 4, 2015
@abadger abadger added this to the v2 milestone Sep 4, 2015
@abadger abadger added the P2 Priority 2 - Issue Blocks Release label Sep 4, 2015
@abadger
Copy link
Contributor

abadger commented Sep 4, 2015

@mgedmin Thanks for continuing to find problems and make quality bug reports! There were a few changes in this past week that could affect a lot of code so continue to let us know if you find stuff and we'll either fix it or decide that it's a change of behaviour that we want in v2.

Closing This Ticket

Hi!

We believe recent commits (likely detailed above) should resolve this question or problem for you.

This will also be included in the next major release.

If you continue seeing any problems related to this issue, or if you have any further questions, please let us know by stopping by one of the two mailing lists, as appropriate:

Because this project is very active, we're unlikely to see comments made on closed tickets, but the mailing list is a great way to ask questions, or post if you don't think this particular
issue is resolved.

Thank you!

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 6, 2018
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue/PR relates to a bug. P2 Priority 2 - Issue Blocks Release
Projects
None yet
Development

No branches or pull requests

4 participants