# lineinfile anywhere match

**URL:** <https://forum.ansible.com/t/lineinfile-anywhere-match/24637>\
**Category:** Ansible Project\
**Created:** [June 2, 2017, 3:55pm UTC](https://forum.ansible.com/t/lineinfile-anywhere-match/24637 "2017-06-02T15:55:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [June 2, 2017, 3:55pm UTC](https://forum.ansible.com/t/lineinfile-anywhere-match/24637/1 "2017-06-02T15:55:04Z")

</div>

How do I do an anywhere match for a string and replace with some text?

The below playbook doesnt work. But just looking for example to search for part of green - reen and replace this with jabba.

How do I do that? Thanks

List

> red
> 
> blahstuffcans
> 
> buns
> 
> buns
> 
> grey
> 
> green
> 
> brown
> 
> red
> 
> grey
> 
> grey

Playbook -

---

<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:** [June 2, 2017, 4:51pm UTC](https://forum.ansible.com/t/lineinfile-anywhere-match/24637/2 "2017-06-02T16:51:44Z")

</div>

> How do I do an anywhere match for a string and replace with some text?
> 
> The below playbook doesnt work. But just looking for example to search for  
> part of green - reen and replace this with jabba.
> 
> How do I do that? Thanks

I suggest reading about regexp here  
[https://docs.python.org/2/library/re.html](https://docs.python.org/2/library/re.html)  
and then  
[https://docs.ansible.com/ansible/lineinfile\_module.html](https://docs.ansible.com/ansible/lineinfile_module.html)

> List
> 
> red  
> blahstuffcans  
> buns  
> grey  
> green  
> brown  
> red  
> grey
> 
> Playbook -
> 
> ---  
> - hosts: local  
> &nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Line In File  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /home/ansible/test.txt  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '%reen%'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '%jabba%'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: foundstuff  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=foundstuff

% is not a valid regexp special character so you literally searching for a line containing %reen% and replace that line with %jabba%.  
But since %reen% is not in file it will add %jabba% to the bottom.

To make it work you need to do this

&nbsp;&nbsp;&nbsp;- name: Line In File  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /home/ansible/test.txt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '(.\*)reen(.\*)'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '\1jabba\2'

---

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [June 2, 2017, 4:54pm UTC](https://forum.ansible.com/t/lineinfile-anywhere-match/24637/3 "2017-06-02T16:54:59Z")

</div>

Ive read both. Its still not clear to me how to do an anywhere match on a string

---

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [June 2, 2017, 5:00pm UTC](https://forum.ansible.com/t/lineinfile-anywhere-match/24637/4 "2017-06-02T17:00:13Z")

</div>

> Found the answer, from the below list, purple replaced brown with the playbook further below

crimson  
blahstuffcans  
buns  
buns  
grey  
purple  
orange  
grey  
grey  
%jabba%  
jabba1

Below replaced brown with purple for example

---

<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:** [June 2, 2017, 5:06pm UTC](https://forum.ansible.com/t/lineinfile-anywhere-match/24637/5 "2017-06-02T17:06:56Z")

</div>

OK, but did you read my entire answer?  
The answer to your question is in it.

---

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [June 2, 2017, 5:13pm UTC](https://forum.ansible.com/t/lineinfile-anywhere-match/24637/6 "2017-06-02T17:13:14Z")

</div>

Ah I didnt see the bit at the bottom. Thanks
