regex_escape can be used to escape literal characters in a string. In this example, fo.o contains the literal period character.
- name: "use regex_escape to escape literal characters"
debug:
msg: "{{ 'fo.o' | regex_escape() }}"
Which should return the following. Notice the period is now escaped with two back slashes.
ok: [server1.example.com] => {
"msg": "fo\\.o"
}
Let's say a variable named "foo" contains a value of fo*o. Notice the wildcard character.
vars:
foo: "fo*o"
Notice in this example that foo is not wrapped in quotes. When wrapped in quotes, foo is interpreted as a string. When not wrapped in quotes, foo is be interpreted as a variable
- name: "use regex_escape to escape literal characters"
debug:
msg: "{{ foo | regex_escape }}"
Which should return the following. Notice the wildcard is escaped with two back slashes.
ok: [server1.example.com] => {
"msg": "fo\\.o"
}
Let's say you are doing a regular expression search that contains wildcards, like this.
when: "foo is not regex ('01 00 * * * /path/to/example.sh')"
Sometimes, you may just want to do something like this. In this example, the wildcard characters are wrapped in brackets.
when: "foo is not regex ('01 00 [*] [*] [*] /path/to/example.sh')"