Skip to content

Instantly share code, notes, and snippets.

@glaszig
Created October 21, 2018 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glaszig/c65ae389a5dd8edb8b5ae73d286d990c to your computer and use it in GitHub Desktop.
Save glaszig/c65ae389a5dd8edb8b5ae73d286d990c to your computer and use it in GitHub Desktop.
Jinja plugin (for use with Ansible) to run re.escape against input
# (c) 2018, glaszig@gmail.com
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the MIT license.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# MIT License for more details.
#
# You should have received a copy of the MIT.
# If not, see <http://opensource.org/licenses/MIT>.
# Usage:
# {{ "The (quick) brown fox jump$" | regexp_escape }}
# # => "The \(quick\) brown fox jump\$"
from re import escape
def regexp_escape(collection):
'''
applies re.escape to collection
'''
if isinstance(collection, basestring):
return escape(collection)
elif item in collection:
return [escape(i) for i in collection]
class FilterModule(object):
def filters(self):
return {
'regexp_escape': regexp_escape
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment