diff --git a/changelogs/fragments/441-json-add-indent-parameter.yaml b/changelogs/fragments/441-json-add-indent-parameter.yaml new file mode 100644 index 0000000..8a0359b --- /dev/null +++ b/changelogs/fragments/441-json-add-indent-parameter.yaml @@ -0,0 +1,2 @@ +minor_changes: + - json and jsonl - Add the ``ANSIBLE_JSON_INDENT`` parameter diff --git a/plugins/callback/json.py b/plugins/callback/json.py index 512f25b..6f86bd1 100644 --- a/plugins/callback/json.py +++ b/plugins/callback/json.py @@ -25,6 +25,16 @@ DOCUMENTATION = ''' - key: show_custom_stats section: defaults type: bool + json_indent: + name: Use indenting for the JSON output + description: 'If specified, use this many spaces for indenting in the JSON output. If <= 0, write to a single line.' + default: 4 + env: + - name: ANSIBLE_JSON_INDENT + ini: + - key: json_indent + section: defaults + type: integer notes: - When using a strategy such as free, host_pinned, or a custom strategy, host results will be added to new task results in ``.plays[].tasks[]``. As such, there will exist duplicate @@ -61,6 +71,12 @@ class CallbackModule(CallbackBase): self._task_map = {} self._is_lockstep = False + self.set_options() + + self._json_indent = self.get_option('json_indent') + if self._json_indent <= 0: + self._json_indent = None + def _new_play(self, play): self._is_lockstep = play.strategy in LOCKSTEP_CALLBACKS return { @@ -145,7 +161,7 @@ class CallbackModule(CallbackBase): 'global_custom_stats': global_custom_stats, } - self._display.display(json.dumps(output, cls=AnsibleJSONEncoder, indent=4, sort_keys=True)) + self._display.display(json.dumps(output, cls=AnsibleJSONEncoder, indent=self._json_indent, sort_keys=True)) def _record_task_result(self, on_info, result, **kwargs): """This function is used as a partial to add failed/skipped info in a single method""" diff --git a/plugins/callback/jsonl.py b/plugins/callback/jsonl.py index dc77b03..1e03163 100644 --- a/plugins/callback/jsonl.py +++ b/plugins/callback/jsonl.py @@ -26,6 +26,16 @@ DOCUMENTATION = ''' - key: show_custom_stats section: defaults type: bool + json_indent: + name: Use indenting for the JSON output + description: 'If specified, use this many spaces for indenting in the JSON output. If not specified or <= 0, write to a single line.' + default: 0 + env: + - name: ANSIBLE_JSON_INDENT + ini: + - key: json_indent + section: defaults + type: integer notes: - When using a strategy such as free, host_pinned, or a custom strategy, host results will be added to new task results in ``.plays[].tasks[]``. As such, there will exist duplicate @@ -63,6 +73,12 @@ class CallbackModule(CallbackBase): self._task_map = {} self._is_lockstep = False + self.set_options() + + self._json_indent = self.get_option('json_indent') + if self._json_indent <= 0: + self._json_indent = None + def _new_play(self, play): self._is_lockstep = play.strategy in LOCKSTEP_CALLBACKS return { @@ -158,7 +174,7 @@ class CallbackModule(CallbackBase): def _write_event(self, event_name, output): output['_event'] = event_name output['_timestamp'] = current_time() - self._display.display(json.dumps(output, cls=AnsibleJSONEncoder, separators=',:', sort_keys=True)) + self._display.display(json.dumps(output, cls=AnsibleJSONEncoder, indent=self._json_indent, separators=',:', sort_keys=True)) def _record_task_result(self, event_name, on_info, result, **kwargs): """This function is used as a partial to add failed/skipped info in a single method"""