community.general.jc filter – Convert output of many shell commands and file-types to JSON

Note

This filter plugin is part of the community.general collection (version 8.6.0).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install community.general. You need further requirements to be able to use this filter plugin, see Requirements for details.

To use it in a playbook, specify: community.general.jc.

New in community.general 1.1.0

Synopsis

  • Convert output of many shell commands and file-types to JSON.

  • Uses the jc library.

Requirements

The below requirements are needed on the local controller node that executes this filter.

Input

This describes the input of the filter, the value before | community.general.jc.

Parameter

Comments

Input

string / required

The data to convert.

Positional parameters

This describes positional parameters of the filter. These are the values positional1, positional2 and so on in the following example: input | community.general.jc(positional1, positional2, ...)

Parameter

Comments

parser

string / required

The correct parser for the input data.

For example ifconfig.

Note: use underscores instead of dashes (if any) in the parser module name.

See https://github.com/kellyjonbrazil/jc#parsers for the latest list of parsers.

Keyword parameters

This describes keyword parameters of the filter. These are the values key1=value1, key2=value2 and so on in the following example: input | community.general.jc(key1=value1, key2=value2, ...)

Parameter

Comments

quiet

boolean

Set to false to not suppress warnings.

Choices:

  • false

  • true ← (default)

raw

boolean

Set to true to return pre-processed JSON.

Choices:

  • false ← (default)

  • true

Notes

Note

  • When keyword and positional parameters are used together, positional parameters must be listed before keyword parameters: input | community.general.jc(positional1, positional2, key1=value1, key2=value2)

Examples

- name: Install the prereqs of the jc filter (jc Python package) on the Ansible controller
  delegate_to: localhost
  ansible.builtin.pip:
    name: jc
    state: present

- name: Run command
  ansible.builtin.command: uname -a
  register: result

- name: Convert command's result to JSON
  ansible.builtin.debug:
    msg: "{{ result.stdout | community.general.jc('uname') }}"
  # Possible output:
  #
  # "msg": {
  #   "hardware_platform": "x86_64",
  #   "kernel_name": "Linux",
  #   "kernel_release": "4.15.0-112-generic",
  #   "kernel_version": "#113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020",
  #   "machine": "x86_64",
  #   "node_name": "kbrazil-ubuntu",
  #   "operating_system": "GNU/Linux",
  #   "processor": "x86_64"
  # }

Return Value

Key

Description

Return value

any

The processed output.

Returned: success

Authors

  • Kelly Brazil (@kellyjonbrazil)

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.