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

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,

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

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=‘.ssh://jenkins@srch-gerrit00.sv.xyz.com:29418/yyy.git’ line=’ git@github.com:kavin/yyy.git’ backrefs=yes backup=yes
with_fileglob:

  • /home/kavin/*/config.xml

  • name: Replace wipeworkspace to true
    local_action: lineinfile dest={{ item }} regexp=‘.\w+’ line=’ true’ backup=yes backrefs=yes
    with_fileglob:

  • /home/kavin/*/config.xml

Any recommendations, (maybe my regex is wrong ? )

Don’t use lineinfile? :slight_smile:

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?

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=‘.ssh://jenkins@srch-gerrit00.sv.xyz.com:29418/yyy.git’ line=’ git@github.com:kavin/yyy.git’ backrefs=yes backup=yes
with_fileglob:

  • /home/kavin/*/config.xml

  • name: Replace wipeworkspace to true
    local_action: lineinfile dest={{ item }} regexp=‘.\w+’ line=’ true’ backup=yes backrefs=yes
    with_fileglob:

  • /home/kavin/*/config.xml

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?!?!?

Perhaps we should change “backrefs” to “replace” or something to make this more obvious.

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.

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!

Hello Everyone,

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

Thanks!

Your version of Ansible is rather downlevel.

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

Thanks for your quick reply!
I tried ansible 1.7 (devel 5e27f12ada).
It would appear that the problem still exists.

Thanks!

Then perhaps we have an answer :slight_smile:

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

Thank you!

Thanks!