# how to replace a big string in file using ansible

**URL:** <https://forum.ansible.com/t/how-to-replace-a-big-string-in-file-using-ansible/23312>\
**Category:** Ansible Project\
**Created:** [December 21, 2016, 6:00pm UTC](https://forum.ansible.com/t/how-to-replace-a-big-string-in-file-using-ansible/23312 "2016-12-21T18:00:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![deva.srao](https://avatars.discourse-cdn.com/v4/letter/d/b38774/32.png) [@deva.srao](https://forum.ansible.com/u/deva.srao)\
**Post date:** [December 21, 2016, 6:00pm UTC](https://forum.ansible.com/t/how-to-replace-a-big-string-in-file-using-ansible/23312/1 "2016-12-21T18:00:26Z")

</div>

Hi

I have string in a file that contains lot of special characters. how to replace that string using ansible modules.

From

$base\_url = ‘http://’ . $\_SERVER[‘SERVER\_NAME’];

To

$base\_url = ‘[http://server1.abc.com](http://server1.abc.com)’;

Can some one help on this.

Thanks  
Dev

---

<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:** [December 22, 2016, 6:38pm UTC](https://forum.ansible.com/t/how-to-replace-a-big-string-in-file-using-ansible/23312/2 "2016-12-22T18:38:30Z")

</div>

It depends on the rest of the file but if that's the only line starting with $base\_url you can do something like this  
&nbsp;&nbsp;&nbsp;- name: Replace line  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest=/tmp/filename  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp='^\$base\_url = '  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line="$base\_url = '[http://server1.abc.com](http://server1.abc.com)';"
