Skip to content

Commit ecab194

Browse files
rperseeAtlas974
authored andcommitted
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`.
1 parent 78a7fa8 commit ecab194

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

jinja2cli/cli.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,21 @@ def _parse_env(data):
221221

222222

223223
def _load_ansible_filters():
224-
from ansible.plugins.filter.core import FilterModule
224+
from pkgutil import iter_modules
225+
from jinja2.utils import import_string
225226

226-
return FilterModule().filters()
227+
try:
228+
import ansible.plugins.filter
229+
except ImportError:
230+
print("This feature requires the `ansible-core` package.")
231+
raise
232+
233+
filters = dict()
234+
for module in iter_modules(ansible.plugins.filter.__path__):
235+
filter_module = import_string(f"ansible.plugins.filter.{module.name}")
236+
filters.update(filter_module.FilterModule().filters())
237+
238+
return filters
227239

228240

229241
def render(template_path, data, extensions, strict=False, ansible=False):

0 commit comments

Comments
 (0)