Skip to content

Commit

Permalink
Import error message and more filters for Ansible
Browse files Browse the repository at this point in the history
Added import error message when `Ansible Core` is not installed (and
`--ansible` is used).
Importing all filters from `ansible.plugins.filter`.
  • Loading branch information
Atlas974 committed Jun 22, 2022
1 parent 78a7fa8 commit b23948c
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions jinja2cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit b23948c

Please sign in to comment.