Replace indented string in file

I am trying to replace a specific part of an indented string, but I cannot get it working with lineinfile/replace modules for some reason.

What I would like:

before: Substitute “s~(src|href)=('|")/((?!version).*)~$1=$2/version//$3~i”

after: Substitute “s~(src|href)=('|")/((?!version).*)~$1=$2/9.1.2.3/$3~i”

Anyone help me out with this one please?

Cheers!

Are those actual entire lines of content?
Because they contain regex patterns.

Hey,
Are those actual entire lines of content?

Yes, they are the enter lines of the content. It is part of an application configuration file.

Because they contain regex patterns.
Yes and I think this is why I cant get it working

Any pointers would be great, I just need to insert a replace “version” with a version number i.e. 9.1.2.3

I think you misunderstood me.
Are those lines you put there the same as in your source file?
Because I think they’re not.

Instead of posting what appear to be randomly selected lines of a playbook:

  • post the actual playbook/task that you have
  • post a snippet of the real file content that you want to change

I have a file called “bla.conf” which contains this indented line Substitute “s~(src|href)=('|")/((?!version).*)~$1=$2/version//$3~i”

I want to change the indented line from bla.conf to Substitute “s~(src|href)=('|")/((?!version).*)~$1=$2/NEWVERSIONHERE//$3~i”

Ah OK.
In such cases it is very hard to get the escaping right by hand, so what I usually do is use a helper task using regex_escape.
This lets ansible to the escaping for you. Once you have that escaped regex, the only thing you need to do, is to add parenthesis: