community.general.json_diff filter – Create a JSON patch by comparing two JSON files

Note

This filter plugin is part of the community.general collection (version 10.3.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.json_diff.

New in community.general 10.3.0

Synopsis

  • This filter compares the input with the argument and computes a list of operations that can be consumed by the community.general.json_patch_recipe to change the input to the argument.

Requirements

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

  • jsonpatch

Input

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

Parameter

Comments

Input

any / required

A list or a dictionary representing a source JSON object, or a string containing a JSON object.

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.json_diff(positional1, positional2, ...)

Parameter

Comments

target

any / required

A list or a dictionary representing a target JSON object, or a string containing a JSON object.

See Also

See also

RFC 6902

JavaScript Object Notation (JSON) Patch

RFC 6901

JavaScript Object Notation (JSON) Pointer

jsonpatch Python Package

A Python library for applying JSON patches

Examples

- name: Compute a difference
  ansible.builtin.debug:
    msg: "{{ input | community.general.json_diff(target) }}"
  vars:
    input: {"foo": 1, "bar":{"baz": 2}, "baw": [1, 2, 3], "hello": "day"}
    target: {"foo": 1, "bar": {"baz": 2}, "baw": [1, 3], "baq": {"baz": 2}, "hello": "night"}
  # => [
  #   {"op": "add", "path": "/baq", "value": {"baz": 2}},
  #   {"op": "remove", "path": "/baw/1"},
  #   {"op": "replace", "path": "/hello", "value": "night"}
  # ]

Return Value

Key

Description

Return value

list / elements=dictionary

A list of JSON patch operations to apply.

Returned: success

Authors

  • Stanislav Meduna (@numo68)

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.