diff --git a/changelogs/fragments/6043-jc_plugin_parser_support.yml b/changelogs/fragments/6043-jc_plugin_parser_support.yml new file mode 100644 index 0000000000..3684f32fe4 --- /dev/null +++ b/changelogs/fragments/6043-jc_plugin_parser_support.yml @@ -0,0 +1,2 @@ +minor_changes: + - jc filter plugin - added the ability to use parser plugins (https://github.com/ansible-collections/community.general/pull/6043). diff --git a/plugins/filter/jc.py b/plugins/filter/jc.py index 742d4147a1..3aa8d20a5f 100644 --- a/plugins/filter/jc.py +++ b/plugins/filter/jc.py @@ -138,8 +138,14 @@ def jc_filter(data, parser, quiet=True, raw=False): raise AnsibleError('You need to install "jc" as a Python library on the Ansible controller prior to running jc filter') try: - jc_parser = importlib.import_module('jc.parsers.' + parser) - return jc_parser.parse(data, quiet=quiet, raw=raw) + # new API (jc v1.18.0 and higher) allows use of plugin parsers + if hasattr(jc, 'parse'): + return jc.parse(parser, data, quiet=quiet, raw=raw) + + # old API (jc v1.17.7 and lower) + else: + jc_parser = importlib.import_module('jc.parsers.' + parser) + return jc_parser.parse(data, quiet=quiet, raw=raw) except Exception as e: raise AnsibleFilterError('Error in jc filter plugin: %s' % e)