# Replace IP by another en replace module

**URL:** <https://forum.ansible.com/t/replace-ip-by-another-en-replace-module/21274>\
**Category:** Ansible Project\
**Created:** [April 26, 2016, 12:34pm UTC](https://forum.ansible.com/t/replace-ip-by-another-en-replace-module/21274 "2016-04-26T12:34:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cavette](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@Cavette](https://forum.ansible.com/u/Cavette)\
**Post date:** [April 26, 2016, 12:34pm UTC](https://forum.ansible.com/t/replace-ip-by-another-en-replace-module/21274/1 "2016-04-26T12:34:13Z")

</div>

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

---

<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:** [April 30, 2016, 11:35am UTC](https://forum.ansible.com/t/replace-ip-by-another-en-replace-module/21274/2 "2016-04-30T11:35:27Z")

</div>

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](https://docs.python.org/2/library/re.html#re.sub)
