# How to replace a line with special characters to another line with special characters using variables?

**URL:** <https://forum.ansible.com/t/how-to-replace-a-line-with-special-characters-to-another-line-with-special-characters-using-variables/31521>\
**Category:** Ansible Project\
**Created:** [October 23, 2019, 6:35am UTC](https://forum.ansible.com/t/how-to-replace-a-line-with-special-characters-to-another-line-with-special-characters-using-variables/31521 "2019-10-23T06:35:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ankit2](https://forum.ansible.com/u/Ankit2)\
**Post date:** [October 23, 2019, 6:35am UTC](https://forum.ansible.com/t/how-to-replace-a-line-with-special-characters-to-another-line-with-special-characters-using-variables/31521/1 "2019-10-23T06:35:35Z")

</div>

Hello All,

I am trying to update a configuration file where there are existing lines (rules) with special characters (:\*/. etc.). In normal scenario, i can always manually escape all the characters but in this particular case, i have got pre-populated values in two variables: **var1** contains the text to replace and **var2** contains the new text which needs to be updated instead of value in var1.

For Example. The following is one sample config file where i want to replace [monitor://D:\Logs\Org.SNP.Publisher\*] **to** [monitor://D:\new\path\dir\*]

`

---

<div class="post-metadata">

**Author:** ![Ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ankit2](https://forum.ansible.com/u/Ankit2)\
**Post date:** [October 24, 2019, 9:32am UTC](https://forum.ansible.com/t/how-to-replace-a-line-with-special-characters-to-another-line-with-special-characters-using-variables/31521/2 "2019-10-24T09:32:05Z")

</div>

Hello All,

Can anyone please suggest how can i get this done.

Thanks in advance.

---

<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:** [October 24, 2019, 6:45pm UTC](https://forum.ansible.com/t/how-to-replace-a-line-with-special-characters-to-another-line-with-special-characters-using-variables/31521/3 "2019-10-24T18:45:09Z")

</div>

Replace module is using Python re[1] module.  
Your replace: contain escape sequences, if Python re recognize a escape sequence it will replace it.

You have \n, \b and \d of these Python re do recognize \n at least, so you need to be escaped it by putting an extra \ in it so it becomes \\n

[1] [https://docs.python.org/2/library/re.html](https://docs.python.org/2/library/re.html)

---

<div class="post-metadata">

**Author:** ![Ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ankit2](https://forum.ansible.com/u/Ankit2)\
**Post date:** [October 28, 2019, 5:48am UTC](https://forum.ansible.com/t/how-to-replace-a-line-with-special-characters-to-another-line-with-special-characters-using-variables/31521/4 "2019-10-28T05:48:24Z")

</div>

Hello Kai,

Thanks for your response.  
Actually the issue here is that these pattern value and replace text values are provided to the playbook using surveys i.e., during run time and hence, it won’t be possible to manually escape them by the users who run the template.

Is there a way to auto escape any special chars in the pattern and replace the text without escaped values in the file?

---

<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:** [October 29, 2019, 10:06pm UTC](https://forum.ansible.com/t/how-to-replace-a-line-with-special-characters-to-another-line-with-special-characters-using-variables/31521/5 "2019-10-29T22:06:01Z")

</div>

You could write your own filter that escapes every escape sequences Python regexp has or change the replace module to also accept plain text aka automatically escape all escapes sequence.

A workaround is to use the regex\_replace filter, this is only for \n, but you would need to do it for every escape sequence(there are many of them)

&nbsp;&nbsp;&nbsp;- name: test unsafe variable in the file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /home/anks/yaml/filewithcontent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '{{ var1 | regex\_escape() }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace: "{{ var2 | regex\_replace('\\\\n', '\\\\\\\\n') }}"

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 30, 2019, 4:52am UTC](https://forum.ansible.com/t/how-to-replace-a-line-with-special-characters-to-another-line-with-special-characters-using-variables/31521/6 "2019-10-30T04:52:57Z")

</div>

Try this

&nbsp;&nbsp;&nbsp;&nbsp;- replace:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: 'playbook.yml'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '{{ regex }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace: '{{ replace }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regex: '\[monitor\://D\:\\Logs\\Org\.SNP\.Publisher\\\*]'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace: '[monitor://D:\\new\\path\\dir\\*]'

1) Put \*regex\* and \*replace\* into separate variables and use "7.3.2.  
&nbsp;&nbsp;&nbsp;Single-Quoted Style".  
&nbsp;&nbsp;&nbsp;Quoting: '...the “\” and “"” characters may be freely used. This restricts  
&nbsp;&nbsp;&nbsp;single-quoted scalars to printable characters ...'  
&nbsp;&nbsp;&nbsp;[https://yaml.org/spec/1.2/spec.html#id2788097](https://yaml.org/spec/1.2/spec.html#id2788097)

2) In \*regex\* escape Python regular expressions special characters only  
&nbsp;&nbsp;&nbsp;[https://pythex.org/](https://pythex.org/)

3) In \*replace\* escape control characters only  
&nbsp;&nbsp;&nbsp;[https://en.wiktionary.org/wiki/Appendix:Control\_characters](https://en.wiktionary.org/wiki/Appendix:Control_characters)

Cheers,

&nbsp;&nbsp;-vlado
