lineinfile module replace only if line is present, do not add

9,035 views
Skip to first unread message

Kavin Kankeshwar

unread,
Aug 19, 2013, 9:51:36 PM8/19/13
to ansible...@googlegroups.com
Hi,

Wanted to know if it is possible for lineinfile to only replace line if regex matches, if it does not match do not add new line.

Is this possible ? From looks of it , does not seem to work.

Regards,
--
Kavin.Kankeshwar

yann lawrence

unread,
Aug 27, 2013, 3:16:22 PM8/27/13
to ansible...@googlegroups.com
Hello Kavin, 



"... is it possible for lineinfile to only replace line if regex matches, if it does not match do not add new line."

This is exactly what lineinfile does... could you possibly give me an example of the problems you are having.. There may be a problem with your regular expression 

Ta, 

Yann

Kavin Kankeshwar

unread,
Aug 27, 2013, 7:49:25 PM8/27/13
to ansible...@googlegroups.com
Yes, Even i thought it would, only replace not add. To make it work (i just added backrefs=yes and it works as i wanted to.

if i remove back refs , it keeps adding a new line.

    -name: Replace repo with new repo 
      local_action: lineinfile dest={{ item }} regexp='.*<url>ssh://jenkins@srch-gerrit00\.sv\.xyz\.com:29418/yyy\.git</url>*' line='        <url>g...@github.com:kavin/yyy.git<url>' backrefs=yes backup=yes
      with_fileglob:
        - /home/kavin/*/config.xml

    - name: Replace wipeworkspace to true
      local_action: lineinfile dest={{ item }} regexp='.*<wipeOutWorkspace>\w+</wipeOutWorkspace>*' line='    <wipeOutWorkspace>true</wipeOutWorkspace>' backup=yes backrefs=yes
      with_fileglob:
        - /home/kavin/*/config.xml

Regards,
--
Kavin.Kankeshwar



--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Kavin Kankeshwar

unread,
Aug 29, 2013, 2:20:51 AM8/29/13
to ansible...@googlegroups.com
Any recommendations, (maybe my regex is wrong ? )

Regards,
--
Kavin.Kankeshwar

Michael DeHaan

unread,
Aug 29, 2013, 8:23:33 PM8/29/13
to ansible...@googlegroups.com
Don't use lineinfile? :)




--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

C. Morgan Hamill

unread,
Aug 30, 2013, 10:25:04 AM8/30/13
to ansible-project
Excerpts from Kavin Kankeshwar's message of 2013-08-27 19:49:25 -0400:
> Yes, Even i thought it would, only replace not add. To make it work (i just
> added backrefs=yes and it works as i wanted to.
>
> if i remove back refs , it keeps adding a new line.

Wait, just to make sure I'm clear, when backrefs=yes, lineinfile
*doesn't* add a line, but only replaces the line matching 'regexp' if it
is found, and if 'regexp' is not found, does nothing?

But without backrefs=yes, lineinfile replaces 'regexp' if found, and if
not, adds 'line' to the file?

It seems like that's inconsistent, and probably a bug? Also a weird
one, as I don't think backrefs should have any affect on the actual
line-replacing behavior of lineinfile, only whether backrefs are allowed
or not?
--
Morgan

Kavin Kankeshwar

unread,
Aug 30, 2013, 10:45:20 AM8/30/13
to ansible...@googlegroups.com
Yes, This confused me too a lot, As i have been using lineinfile before and earlier all my use cases were same, find and replace, if not matched add the file. So it worked fine for me. (i think it does it because state=present)

But in this case, I just wanted to replace a line if found and if not found dont do anything. And i could do what i wanted with using backrefs=yes.

My first thought was my regex itself is wrong or im doing something stupid. I am no regex guru but here is a snippet of my playbook.


    -name: Replace repo with new repo 
      local_action: lineinfile dest={{ item }} regexp='.*<url>ssh://jenkins@srch-gerrit00\.sv\.xyz\.com:29418/yyy\.git</url>*' line='        <url>g...@github.com:kavin/yyy.git<url>' backrefs=yes backup=yes
      with_fileglob:
        - /home/kavin/*/config.xml

    - name: Replace wipeworkspace to true
      local_action: lineinfile dest={{ item }} regexp='.*<wipeOutWorkspace>\w+</wipeOutWorkspace>*' line='    <wipeOutWorkspace>true</wipeOutWorkspace>' backup=yes backrefs=yes
      with_fileglob:
        - /home/kavin/*/config.xml




Regards,
--
Kavin.Kankeshwar



senorsmile

unread,
Oct 15, 2013, 4:25:32 PM10/15/13
to ansible...@googlegroups.com
Holy cow, this confused me as well.  This behavior really doesn't make any sense to me.  Perhaps the documentation needs to be very clear about this behavior?!?!?

---
OK, I read the doc again.  After knowing this, it does hint at this behavior, but it's still not obvious.  It seems the default should be backrefs=yes  (which is apparently what it used to be?)

Michael DeHaan

unread,
Oct 15, 2013, 7:36:14 PM10/15/13
to ansible...@googlegroups.com
Perhaps we should change "backrefs" to "replace" or something to make this more obvious.



C. Morgan Hamill

unread,
Oct 16, 2013, 9:05:11 AM10/16/13
to ansible-project
Excerpts from Michael DeHaan's message of 2013-10-15 19:36:14 -0400:
> Perhaps we should change "backrefs" to "replace" or something to make this
> more obvious.

Is this behavior intended? I thought the 'backrefs' option was supposed
to simply enable backrefs ('\1 \2' etc), not change the actual behavior
of the module.
--
Morgan Hamill

Michael DeHaan

unread,
Oct 16, 2013, 9:13:07 AM10/16/13
to ansible...@googlegroups.com
That's what I was implying.

I don't see why you would get a substring out of one line and then add another.   

Please file a docs ticket on this (github.com/ansible/ansible)

Thanks!




--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Ming Qian

unread,
Jul 14, 2014, 12:21:45 PM7/14/14
to ansible...@googlegroups.com
Hello Everyone,

Has this problem been fixed?
I actually had the exact same issue today.
I am using ansible 1.5.4

Thanks!

Michael DeHaan

unread,
Jul 14, 2014, 3:10:01 PM7/14/14
to ansible...@googlegroups.com
Your version of Ansible is rather downlevel.

If you have a problem in 1.6.6 (the latest) let us know.




--
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.

Ming Qian

unread,
Jul 14, 2014, 4:14:33 PM7/14/14
to ansible...@googlegroups.com
Thanks for your quick reply!
I tried ansible 1.7 (devel 5e27f12ada).
It would appear that the problem still exists.

Thanks!

Michael DeHaan

unread,
Jul 14, 2014, 4:38:25 PM7/14/14
to ansible...@googlegroups.com
Then perhaps we have an answer :)

If you don't see a related issue in github please do file one.

Thank you!




Ming Qian

unread,
Jul 14, 2014, 4:53:25 PM7/14/14
to ansible...@googlegroups.com
Thanks!




--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/XmaC_YHWh00/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages