Hi, the problem is that you're inside a YAML " " string, which already interprets escaping.
Try using `>-` or something like that (https://yaml-multiline.info/) instead of double quotes for the string that's templated, like replaced_string: >- {{ searched_string | regex_replace( '\(\'([^\']+)\'\, \'([^\']+)\'\)', '\\1@\\2') }} Then it should work. (Alternatively, if you use " ", you need to double escape - which is extremely ugly to read.) Cheers, Felix On Wed, 3 Feb 2021 10:49:44 -0800 (PST) jean-christophe manciot <[email protected]> wrote: > 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? -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20210203195757.16d4cb42%40rovaniemi.
