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

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*]

`

Hello All,

Can anyone please suggest how can i get this done.

Thanks in advance.

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

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?

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)

   - name: test unsafe variable in the file
     replace:
       path: /home/anks/yaml/filewithcontent
       regexp: '{{ var1 | regex_escape() }}'
       replace: "{{ var2 | regex_replace('\\\\n', '\\\\\\\\n') }}"

Try this

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

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

2) In *regex* escape Python regular expressions special characters only
   https://pythex.org/

3) In *replace* escape control characters only
   https://en.wiktionary.org/wiki/Appendix:Control_characters

Cheers,

  -vlado