mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-22 14:01:42 -07:00
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:
parent
37bdee331c
commit
8ffed6df75
8 changed files with 74 additions and 4 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue