Ansible lineinfile not working as expected (newbie question)

570 views
Skip to first unread message

Boyd J

unread,
Mar 30, 2016, 9:45:45 PM3/30/16
to Ansible Project
I'm learning Ansible for a new project, and I'm stuck on what looks like a basic issue. 
Using lineinfile with backrefs and \1 doesn't work for me.

Simplified playbook to comment out a line starting with "This":
---
- hosts: localhost
  tasks:
  - name: comment out This
    lineinfile: dest=test.txt backrefs=yes regexp='^This.*' line='#\\1'

test.txt file:
Nothing special
This is a test
Nothing special

Result of: "ansible-playbook edit5.yml"
Nothing special
#\
Nothing special

I've tried single, double, 4 \'s.  Single \ gives an error "invalid group reference".  3 gives "#\1", 4 gives "#\" again.
I've been googling for an answer, but all the excerpts I see give similar results.  Other characters other than # work the same.
I am running Ansible 1.9.4 on RHEL 6.7.  I also tried replace: with similar results.

Thank you!
Boyd

Matt Martz

unread,
Mar 30, 2016, 10:09:02 PM3/30/16
to ansible...@googlegroups.com
You haven't actually created a group reference.  You have to wrap what you want to reference in () to create a group

So maybe something like:

regexp='^(This.*)' line='#\\1'
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/24129fc3-a557-46e4-bec9-6f6f04e2decd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Boyd J

unread,
Apr 1, 2016, 11:55:26 AM4/1/16
to Ansible Project
Aha, thank you, that worked, at least after I changed \\1 to \1.  Now I understand what group reference and those () I was seeing in examples mean; the \1 and \2 reference those in order.
Reply all
Reply to author
Forward
0 new messages