ansible:
2.10.5 (with pip3 install)
python version = 3.9.1+ (default, Jan 20 2021, 14:49:22) [GCC 10.2.1 20210110]
Escaping special characters in the searched string such as the following should be simply done with '\':
- ' --> \'
- ( --> \(
- , --> \,
- ) --> \)
However, it seems to be impossible to perform this task with a single '\' or even a double '\\'; for instance, running the following playbook fails:
```
---
- name: Escaping special characters in regex_replace fails
hosts:
- localhost
strategy: debug
vars:
searched_string: "('string_a', 'string_b'),('string_c', 'string_d')"
tasks:
- name: Escaping with '\' - Result expected is "string_a@string_b,string_c@string_d"
set_fact:
replaced_string: "{{ searched_string |
regex_replace('\(\'([^\']+)\'\, \'([^\']+)\'\)', '\\1@\\2') }}"
...
```
leads to:
```
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)
Syntax Error while loading YAML.
found unknown escape character
The error appears to be in 'escape_special_characters_in_regex_replace.yml': line 16, column 53, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
replaced_string: "{{ searched_string |
regex_replace('\(\'([^\']+)\'\, \'([^\']+)\'\)', '\\1@\\2') }}"
^ here
```
I thought about using the jinja2 'replace' filter to remove all special characters first, but that would just be throwing the can down the road, right? ;-)
What am I missing?