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

**URL:** <https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332>\
**Category:** Ansible Project\
**Created:** [August 20, 2013, 1:51am UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332 "2013-08-20T01:51:36Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kavin\_Kankeshwar](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@Kavin\_Kankeshwar](https://forum.ansible.com/u/Kavin_Kankeshwar)\
**Post date:** [August 20, 2013, 1:51am UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/1 "2013-08-20T01:51:36Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![yann\_lawrence](https://avatars.discourse-cdn.com/v4/letter/y/3d9bf3/32.png) [@yann\_lawrence](https://forum.ansible.com/u/yann_lawrence)\
**Post date:** [August 27, 2013, 7:16pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/2 "2013-08-27T19:16:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kavin\_Kankeshwar](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@Kavin\_Kankeshwar](https://forum.ansible.com/u/Kavin_Kankeshwar)\
**Post date:** [August 27, 2013, 11:49pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/3 "2013-08-27T23:49:25Z")

</div>

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](mailto: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

---

<div class="post-metadata">

**Author:** ![Kavin\_Kankeshwar](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@Kavin\_Kankeshwar](https://forum.ansible.com/u/Kavin_Kankeshwar)\
**Post date:** [August 29, 2013, 6:20am UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/4 "2013-08-29T06:20:51Z")

</div>

Any recommendations, (maybe my regex is wrong ? )

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 30, 2013, 12:23am UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/5 "2013-08-30T00:23:33Z")

</div>

Don’t use lineinfile? 🙂

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [August 30, 2013, 2:25pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/6 "2013-08-30T14:25:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kavin\_Kankeshwar](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@Kavin\_Kankeshwar](https://forum.ansible.com/u/Kavin_Kankeshwar)\
**Post date:** [August 30, 2013, 2:45pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/7 "2013-08-30T14:45:20Z")

</div>

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](mailto: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

---

<div class="post-metadata">

**Author:** ![senorsmile](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/senorsmile/32/9024_2.png) [@senorsmile](https://forum.ansible.com/u/senorsmile)\
**Post date:** [October 15, 2013, 8:25pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/8 "2013-10-15T20:25:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 15, 2013, 11:36pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/9 "2013-10-15T23:36:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [October 16, 2013, 1:05pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/10 "2013-10-16T13:05:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 16, 2013, 1:13pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/11 "2013-10-16T13:13:07Z")

</div>

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](http://github.com/ansible/ansible))

Thanks!

---

<div class="post-metadata">

**Author:** ![Ming\_Qian](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Ming\_Qian](https://forum.ansible.com/u/Ming_Qian)\
**Post date:** [July 14, 2014, 4:21pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/12 "2014-07-14T16:21:45Z")

</div>

Hello Everyone,

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

Thanks!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 14, 2014, 7:09pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/13 "2014-07-14T19:09:58Z")

</div>

Your version of Ansible is rather downlevel.

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

---

<div class="post-metadata">

**Author:** ![Ming\_Qian](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Ming\_Qian](https://forum.ansible.com/u/Ming_Qian)\
**Post date:** [July 14, 2014, 8:14pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/14 "2014-07-14T20:14:33Z")

</div>

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

Thanks!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 14, 2014, 8:38pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/15 "2014-07-14T20:38:20Z")

</div>

Then perhaps we have an answer 🙂

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

Thank you!

---

<div class="post-metadata">

**Author:** ![Ming\_Qian](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Ming\_Qian](https://forum.ansible.com/u/Ming_Qian)\
**Post date:** [July 14, 2014, 8:53pm UTC](https://forum.ansible.com/t/lineinfile-module-replace-only-if-line-is-present-do-not-add/13332/16 "2014-07-14T20:53:20Z")

</div>

Thanks!
