Replace IP by another en replace module

Hello,

I’m trying to replace an Ip address by anoher in a file but it doesn’t work :

Initial file contains :

My code below doesn’t work (invalid group reference) :

  • name : Change IP in file
    replace: dest=myfile.xml regexp=‘(.){{master_ip}}(.)’ replace=‘\1{{ansible_default_ipv4.address}}\2’

But If i put 1 space before and after my variable, it works but have useless spaces.

  • name : Change IP in file
    replace: dest=myfile.xml regexp=‘(.){{master_ip}}(.)’ replace=‘\1 {{ansible_default_ipv4.address}} \2’

Result :

Some has got any clue of the trouble ?

thanks

I think the problem might be this.
Lets say that ansible_default_ipv4.address is 2.2.2.2

Then your replace will be '\12.2.2.2\2', so the group you are referencing is \12 and not \1.

Python re.sub has an alternative way that should work,

replace: dest=myfile.xml regexp='(.*){{master_ip}}(.*)' replace='\g<1>{{ansible_default_ipv4.address}}\g<2>'

You can read more about it here
https://docs.python.org/2/library/re.html#re.sub