mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
JC: Add plugin parser functionality to JC Filter Plugin (#6043)
* Add plugin parser functionality to JC Filter Plugin The parse function was added in jc v1.18.0 which allows plugin parsers to be used. This change will try the new API if available, else fallback to the old API so there is no change in behavior. * remove whitespace from blank line * Add changelog fragment for JC plugin parser support * add .yml extension to file name * Formatting * add period at end
This commit is contained in:
parent
a7e8e95b50
commit
c168f9c3be
2 changed files with 10 additions and 2 deletions
2
changelogs/fragments/6043-jc_plugin_parser_support.yml
Normal file
2
changelogs/fragments/6043-jc_plugin_parser_support.yml
Normal file
|
@ -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).
|
|
@ -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')
|
raise AnsibleError('You need to install "jc" as a Python library on the Ansible controller prior to running jc filter')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
jc_parser = importlib.import_module('jc.parsers.' + parser)
|
# new API (jc v1.18.0 and higher) allows use of plugin parsers
|
||||||
return jc_parser.parse(data, quiet=quiet, raw=raw)
|
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:
|
except Exception as e:
|
||||||
raise AnsibleFilterError('Error in jc filter plugin: %s' % e)
|
raise AnsibleFilterError('Error in jc filter plugin: %s' % e)
|
||||||
|
|
Loading…
Reference in a new issue