Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

lineinfile does not expand "\t" when some conditions are met #174

Closed
h2suzuki opened this issue Oct 12, 2014 · 12 comments
Closed

lineinfile does not expand "\t" when some conditions are met #174

h2suzuki opened this issue Oct 12, 2014 · 12 comments

Comments

@h2suzuki
Copy link

Issue Type:

Bug Report

Ansible Version:

1.7.2
(1.7.1 does not have this issue)

Environment:

CentOS7 x86_64
pip install ansible for 1.7.2
yum install ansible for 1.7.1

Summary:

lineinfile fails to expand "\t" when the inserted line has "...'...'..." type of quoting.

Steps To Reproduce:

prepare the following playbook and the test data file.

--- # file: lineinfile.yml

- hosts: all

  gather_facts: False

  vars:
    original_file: ./test.txt.org
    target_file: ./test.txt
    fresh_start: yes

  tasks:
    - name: copy the data file
      copy: src={{ original_file }} dest={{ target_file }} force={{ fresh_start }}

    - name: insert the line
      lineinfile: dest={{ target_file }} state=present
        insertafter="\t.*this line"
        line="\ta line with 'tab'"
        regexp="a line with 'tab'"

Here is test.txt.org. Note: the indent is by a tab.

# a sample text

        Insert after this line
        the next line

Then, run the playbook:

$ ansible-playbook -i hosts lineinfile.yml
Expected Results:

The target machine should have test.txt in the home directory.

# a sample text

        Insert after this line
        a line with 'tab'
        the next line
Actual Results:

The two character string "\t" is inserted instead of a tab.

# a sample text

        Insert after this line
\ta line with 'tab'
        the next line
@h2suzuki
Copy link
Author

Here is the additional information about quoting. I have tried other mixes of quoting.

The new test data file:

# a sample text

        Insert after this line: single-quoted-nomix '...'
        the next line

        Insert after this line: double-quoted-nomix "..."
        the next line

        Insert after this line: single-quoted-mix '"..."'
        the next line

        Insert after this line: double-quoted-mix "'...'"
        the next line

        Insert after this line: single-quoted-jinja2-mix '{{xx}} "..."'
        the next line

        Insert after this line: double-quoted-jinja2-mix "{{xx}} '...'"
        the next line

And the playbook:

--- # file: lineinfile.yml

- hosts: all

  gather_facts: False

  vars:
    original_file: ./test.txt.org
    target_file: ./test.txt
    fresh_start: yes

  tasks:
    - name: copy the data file
      copy: src={{ original_file }} dest={{ target_file }}
        force={{ fresh_start }}

    - name: case1 insert the line '...'
      lineinfile: dest={{ target_file }} state=present
        insertafter='\t.*single-quoted-nomix'
        line='\ta line with tab 1'
        regexp='a line with tab 1'

    - name: case2 insert the line "..."
      lineinfile: dest={{ target_file }} state=present
        insertafter="\t.*double-quoted-nomix"
        line="\ta line with tab 2"
        regexp="a line with tab 2"

    - name: case3 insert the line with '"..."'
      lineinfile: dest={{ target_file }} state=present
        insertafter='\t.*single-quoted-mix'
        line='\ta line with "tab" 3'
        regexp='a line with "tab" 3'

    - name: case4 insert the line "'...'"
      lineinfile: dest={{ target_file }} state=present
        insertafter="\t.*double-quoted-mix"
        line="\ta line with 'tab' 4"
        regexp="a line with 'tab' 4"

    - name: case5 insert the line with '"..."' with jinja2
      lineinfile: dest={{ target_file }} state=present
        insertafter='\t.*single-quoted-jinja2-mix'
        line='{{ "\t" }}a line with "tab" 5'
        regexp='a line with "tab" 5'

    - name: case6 insert the line "'...'" with jinja2
      lineinfile: dest={{ target_file }} state=present
        insertafter="\t.*double-quoted-jinja2-mix"
        line="{{ '\t' }}a line with 'tab' 6"
        regexp="a line with 'tab' 6"

The actual resulted file is here.

# a sample text

        Insert after this line: single-quoted-nomix '...'
        a line with tab 1
        the next line

        Insert after this line: double-quoted-nomix "..."
        a line with tab 2
        the next line

        Insert after this line: single-quoted-mix '"..."'
        a line with "tab" 3
        the next line

        Insert after this line: double-quoted-mix "'...'"
\ta line with 'tab' 4
        the next line

        Insert after this line: single-quoted-jinja2-mix '{{xx}} "..."'
        a line with "tab" 5
        the next line

        Insert after this line: double-quoted-jinja2-mix "{{xx}} '...'"
        a line with 'tab' 6
        the next line

You can see only case4 is affected. Again, Ansible 1.7.1 ends up with all normal results (good)

@ghost
Copy link

ghost commented Oct 14, 2014

Not only \t, I can see it also for \1 when backrefs is enabled. Tested on Fedora 20.

h2suzuki added a commit to h2suzuki/ansible-lxr-nupic.core that referenced this issue Oct 15, 2014
@ghost
Copy link

ghost commented Feb 13, 2015

As an update to my previous comment, this is another example of broken lineinfile, this time for \1:

---
- hosts: localhost
  connection: local
  gather_facts: False
  vars:
    original_file: ./lineinfile_backref.txt
    target_file: ./lineinfile_backref_test.txt

  tasks:
    - name: copy the data file
      copy: src={{ original_file }} dest={{ target_file }} force=yes

    - name: broken test
      lineinfile: dest={{ target_file }}
                  backrefs=yes regexp="(^[ ]+'values'.*)\),"
                  line="\1 'vn'),"

The example file is:

    'values': ('v1', 'v2', 'v3',),

The commit which introduced the regression is
ansible/ansible@dc9482d
which solves ansible/ansible#8806.

Reverting the patch (I tested on 1.8.2), the previous behavior is restored. Is it possible to fix that issue without breaking this?

apevec pushed a commit to redhat-openstack/khaleesi that referenced this issue Feb 13, 2015
A fix in ansible >= 1.7.2 broke the behavior of lineinfile with
regexp and backrefs in some conditions. More details here:
ansible/ansible-modules-core#174
Khaleesi now requires ansible >= 1.7.2, escape the single quotes,
which restore the required behavior. Please note that this change
breaks lineinfile < 1.7.2.

Also, preserve the spaces at the beginning of the replaced line.

Change-Id: Ia2be03936630f3827fa35e05df0f90ee00d7b342
@mackinra
Copy link

Has this issue been resolved yet? If so, in what version?

@ansibot
Copy link

ansibot commented Aug 1, 2016

@ansible, ping. This issue is waiting on your response.
click here for bot help

@ansibot
Copy link

ansibot commented Sep 13, 2016

@ansible, ping. This issue is still waiting on your response.
click here for bot help

@bcoca bcoca removed the P3 label Sep 13, 2016
@ansibot
Copy link

ansibot commented Oct 6, 2016

@ansible, ping. This issue is still waiting on your response.
click here for bot help

2 similar comments
@ansibot
Copy link

ansibot commented Oct 22, 2016

@ansible, ping. This issue is still waiting on your response.
click here for bot help

@ansibot
Copy link

ansibot commented Nov 6, 2016

@ansible, ping. This issue is still waiting on your response.
click here for bot help

@ansibot
Copy link

ansibot commented Nov 22, 2016

@ansible, ping. This issue is still waiting on your response.
click here for bot help

@ansibot
Copy link

ansibot commented Dec 7, 2016

This repository has been locked. All new issues and pull requests should be filed in https://github.com/ansible/ansible

Please read through the repomerge page in the dev guide. The guide contains links to tools which automatically move your issue or pull request to the ansible/ansible repo.

@ansibot
Copy link

ansibot commented Sep 12, 2017

This issue was migrated to ansible/ansible#30225

@ansibot ansibot closed this as completed Sep 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants