From c1e4085251ddfd4e126b761120183d4054f1cb44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gross?= Date: Wed, 6 Aug 2014 10:56:16 +0200 Subject: [PATCH 1/3] Add regular expression escaping filter. --- lib/ansible/plugins/filter/core.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index e8e3e17f77..a4f16e907d 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -222,6 +222,10 @@ def version_compare(value, version, operator='eq', strict=False): except Exception, e: raise errors.AnsibleFilterError('Version comparison: %s' % e) +def re_escape(string): + '''Escape all regular expressions special characters from STRING.''' + return re.escape(string) + @environmentfilter def rand(environment, end, start=None, step=None): r = SystemRandom() @@ -356,6 +360,7 @@ class FilterModule(object): 'search': search, 'regex': regex, 'regex_replace': regex_replace, + 're_escape': re_escape, # ? : ; 'ternary': ternary, From 36534668f0eaed60be087ba1dd0197b522a6a18a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gross?= Date: Wed, 6 Aug 2014 11:11:44 +0200 Subject: [PATCH 2/3] Change name from re_escape to regex_escape to fit existing function names. --- lib/ansible/plugins/filter/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index a4f16e907d..b8b506e508 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -222,7 +222,7 @@ def version_compare(value, version, operator='eq', strict=False): except Exception, e: raise errors.AnsibleFilterError('Version comparison: %s' % e) -def re_escape(string): +def regex_escape(string): '''Escape all regular expressions special characters from STRING.''' return re.escape(string) @@ -360,7 +360,7 @@ class FilterModule(object): 'search': search, 'regex': regex, 'regex_replace': regex_replace, - 're_escape': re_escape, + 'regex_escape': regex_escape, # ? : ; 'ternary': ternary, From c0b7fcd304beba636e90e4bf3397487ff9772ef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gross?= Date: Sun, 26 Jul 2015 19:08:34 +0200 Subject: [PATCH 3/3] Add documentation for regex_escape filter --- docsite/rst/playbooks_filters.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docsite/rst/playbooks_filters.rst b/docsite/rst/playbooks_filters.rst index 2f9e83c288..d95f617f19 100644 --- a/docsite/rst/playbooks_filters.rst +++ b/docsite/rst/playbooks_filters.rst @@ -401,6 +401,11 @@ To replace text in a string with regex, use the "regex_replace" filter:: .. note:: If "regex_replace" filter is used with variables inside YAML arguments (as opposed to simpler 'key=value' arguments), then you need to escape backreferences (e.g. ``\\1``) with 4 backslashes (``\\\\``) instead of 2 (``\\``). +To escape special characters within a regex, use the "regex_escape" filter:: + + # convert '^f.*o(.*)$' to '\^f\.\*o\(\.\*\)\$' + {{ '^f.*o(.*)$' | regex_escape() }} + A few useful filters are typically added with each new Ansible release. The development documentation shows how to extend Ansible filters by writing your own as plugins, though in general, we encourage new ones to be added to core so everyone can make use of them.