Support custom jinja2 filters.

This uses the plugin framework to add filter plugins.
The previously hardcoded core filters are defined using the plugin
framework now.
This commit is contained in:
Jeroen Hoekx 2012-11-05 15:09:34 +01:00
commit 8ffed6df75
8 changed files with 74 additions and 4 deletions

View file

@ -221,10 +221,11 @@ def template_from_file(basedir, path, vars):
realpath = utils.path_dwim(basedir, path)
loader=jinja2.FileSystemLoader([basedir,os.path.dirname(realpath)])
environment = jinja2.Environment(loader=loader, trim_blocks=True)
environment.filters['to_json'] = json.dumps
environment.filters['from_json'] = json.loads
environment.filters['to_yaml'] = yaml.dump
environment.filters['from_yaml'] = yaml.load
for filter_plugin in utils.plugins.filter_loader.all():
filters = filter_plugin.filters()
if not isinstance(filters, dict):
raise errors.AnsibleError("FilterModule.filters should return a dict.")
environment.filters.update(filters)
try:
data = codecs.open(realpath, encoding="utf8").read()
except UnicodeDecodeError: