Replace IP by another en replace module

287 views
Skip to first unread message

Cavette

unread,
Apr 26, 2016, 12:31:38 PM4/26/16
to Ansible Project
Hello,

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

Initial file contains :

<to uri="jetty:http://W.X.Y.Z:8080/someStuff"/>

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 : <to uri="jetty:http:// R.S.T.U :8080/someStuff"/>

Some has got any clue of the trouble ?

thanks

Kai Stian Olstad

unread,
Apr 30, 2016, 7:35:48 AM4/30/16
to ansible...@googlegroups.com
On 04/26/2016 02:34 PM, Cavette wrote:
> 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.

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

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages