On 28.10.2019 06:48, Ankit Vashistha wrote:
> 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') }}"
--
Kai Stian Olstad