From b23948cf4d697659291121ca741bdf9fd360075f Mon Sep 17 00:00:00 2001 From: Atlas974 Date: Wed, 22 Jun 2022 08:50:38 +0200 Subject: [PATCH] Import error message and more filters for Ansible Added import error message when `Ansible Core` is not installed (and `--ansible` is used). Importing all filters from `ansible.plugins.filter`. --- jinja2cli/cli.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/jinja2cli/cli.py b/jinja2cli/cli.py index 5ab46bf..7aa2579 100644 --- a/jinja2cli/cli.py +++ b/jinja2cli/cli.py @@ -221,9 +221,21 @@ def _parse_env(data): def _load_ansible_filters(): - from ansible.plugins.filter.core import FilterModule + from pkgutil import iter_modules + from jinja2.utils import import_string - return FilterModule().filters() + try: + import ansible.plugins.filter + except ImportError: + print("This feature requires the `ansible-core` package.") + raise + + filters = dict() + for module in iter_modules(ansible.plugins.filter.__path__): + filter_module = import_string(f"ansible.plugins.filter.{module.name}") + filters.update(filter_module.FilterModule().filters()) + + return filters def render(template_path, data, extensions, strict=False, ansible=False):