lineinfile anywhere match

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 -

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
and then
https://docs.ansible.com/ansible/lineinfile_module.html

List

red
blahstuffcans
buns
grey
green
brown
red
grey

Playbook -

---
- hosts: local
   tasks:
     - name: Line In File
       lineinfile:
         dest: /home/ansible/test.txt
         regexp: '%reen%'
         line: '%jabba%'
       register: foundstuff
     - 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

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

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

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

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

Ah I didnt see the bit at the bottom. Thanks