# lineinfile - simply replace a string

**URL:** <https://forum.ansible.com/t/lineinfile-simply-replace-a-string/24490>\
**Category:** Ansible Project\
**Created:** [May 16, 2017, 5:17am UTC](https://forum.ansible.com/t/lineinfile-simply-replace-a-string/24490 "2017-05-16T05:17:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AnsiNoob](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@AnsiNoob](https://forum.ansible.com/u/AnsiNoob)\
**Post date:** [May 16, 2017, 5:17am UTC](https://forum.ansible.com/t/lineinfile-simply-replace-a-string/24490/1 "2017-05-16T05:17:29Z")

</div>

Hi

Two things that I miss in Ansible when compared to shell scripting is easy ‘grep’ and ‘sed’. That was easy to do in shell!

My case is very simple. I have a file with lot of lines (sample below) and the line mentioned below comment has to be checked for its presence.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 16, 2017, 12:30pm UTC](https://forum.ansible.com/t/lineinfile-simply-replace-a-string/24490/2 "2017-05-16T12:30:16Z")

</div>

You seem to be missing the leading white space

---

<div class="post-metadata">

**Author:** ![Listing](https://avatars.discourse-cdn.com/v4/letter/l/3d9bf3/32.png) [@Listing](https://forum.ansible.com/u/Listing)\
**Post date:** [May 16, 2017, 12:48pm UTC](https://forum.ansible.com/t/lineinfile-simply-replace-a-string/24490/3 "2017-05-16T12:48:38Z")

</div>

FWIW: you can also use grep or sed in command and then register the result. If you then output result.stdout you will get the result of the grep/sed.

---

<div class="post-metadata">

**Author:** ![AnsiNoob](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@AnsiNoob](https://forum.ansible.com/u/AnsiNoob)\
**Post date:** [May 16, 2017, 1:59pm UTC](https://forum.ansible.com/t/lineinfile-simply-replace-a-string/24490/4 "2017-05-16T13:59:52Z")

</div>

@Dick

Thanks for your reply. I don't want to use leading white space in the string mentioned in line. That is because there are multiple files and some may have leading white space before the string and some may not.

Ansible should be able to find a snippet which is part of one line in a multi-line file.

---

<div class="post-metadata">

**Author:** ![AnsiNoob](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@AnsiNoob](https://forum.ansible.com/u/AnsiNoob)\
**Post date:** [May 16, 2017, 2:04pm UTC](https://forum.ansible.com/t/lineinfile-simply-replace-a-string/24490/5 "2017-05-16T14:04:26Z")

</div>

@Listing - I will eventually use grep and sed only buy I wish Ansible had a easy to use module for this. I have been using Ansible for sometime now and I'm not to close this gap.

---

<div class="post-metadata">

**Author:** ![John\_Jolet](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@John\_Jolet](https://forum.ansible.com/u/John_Jolet)\
**Post date:** [May 16, 2017, 5:00pm UTC](https://forum.ansible.com/t/lineinfile-simply-replace-a-string/24490/6 "2017-05-16T17:00:22Z")

</div>

did you try ( ?) or some such?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 16, 2017, 9:37pm UTC](https://forum.ansible.com/t/lineinfile-simply-replace-a-string/24490/7 "2017-05-16T21:37:41Z")

</div>

Then you should use a regex that does just that?

&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/test.txt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^(\s+)another=myother$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '\1another=myother'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 17, 2017, 7:20am UTC](https://forum.ansible.com/t/lineinfile-simply-replace-a-string/24490/8 "2017-05-17T07:20:17Z")

</div>

AnsiNoob, what are you trying to achieve in the first place?  
More specifically, what do you mean by: " # Below line's presence  
needs to be checked" ?

lineinfile is meant to make sure (not "checking") a specific line is  
in a file, optionally aware of the lines before and after.  
But looking at your sample file, it looks like a more complex data structure.  
Adding just a line at the bottom doesn't seem to make sense in this case?

In summary, can you be more clear and specific, possibly with more examples?

---

<div class="post-metadata">

**Author:** ![AnsiNoob](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@AnsiNoob](https://forum.ansible.com/u/AnsiNoob)\
**Post date:** [May 17, 2017, 4:21pm UTC](https://forum.ansible.com/t/lineinfile-simply-replace-a-string/24490/9 "2017-05-17T16:21:27Z")

</div>

Thanks for your time Dick.

I’ve give some thought to this and I agree that lineinfile is meant to replace/add a line if not present and not for ‘just checking’ an entire line, a word, or few words.

You are right that my file is complex data structure. We have 100 such files and our job is to check for existence of specific string (another=myother is just an example) in all files. I thought I could use lineinfile module in dry run mode (check\_mode: yes) so that lineinfile would just check for presence and not replace anything.

I do have a workaround now but would be interested to know how people use Ansible to perform simple greps to check for presence of specific settings/entries in different configuration files in order to generate reports.

What I’m doing now:

regex: ‘(another=myother)’  
line: ‘any sample string’

When above is run in check mode then lineinfile outputs saying it did find the string in regexp and replaced it with string in line section but it actually does not as it was executed in check mode.

The ‘changed’ output in yellow is what I use to report that the string exists.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 17, 2017, 5:56pm UTC](https://forum.ansible.com/t/lineinfile-simply-replace-a-string/24490/10 "2017-05-17T17:56:11Z")

</div>

This is an assertion from my side, most people don't.  
Ansible is a configuration management tool and not a reporting tool.  
That means you tell it how you would like your server to be configured and Ansible do the job.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 17, 2017, 8:02pm UTC](https://forum.ansible.com/t/lineinfile-simply-replace-a-string/24490/11 "2017-05-17T20:02:17Z")

</div>

but if you MUST misuse ansible, the `find` module has a `contains`  
option that functions mostly like grep
