FreeKB - Ansible regex_escape
Ansible - regex_escape

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')"

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 73bd9 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |