community.general.json_patch_recipe filter – Apply JSON-Patch (RFC 6902) operations to an object

Note

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

New in community.general 10.3.0

Synopsis

  • This filter sequentially applies JSON patch operations and returns a modified object.

  • If there is a test operation in the list, the filter continues if the test succeeded and returns a none value otherwise.

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_patch_recipe.

Parameter

Comments

Input

any / required

A list or a dictionary representing a 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_patch_recipe(positional1, positional2, ...)

Parameter

Comments

operations

list / elements=dictionary / required

A list of JSON patch operations to apply.

from

string

The source location for the copy and move operation. Mandatory for operations[].op=copy and operations[].op=move, ignored otherwise.

op

string / required

Operation to perform (see RFC 6902).

Choices:

  • "add"

  • "copy"

  • "move"

  • "remove"

  • "replace"

  • "test"

path

string / required

JSON Pointer path to the target location (see RFC 6901).

value

any

Value to use in the operation. Ignored for operations[].op=copy, operations[].op=move, and operations[].op=remove.

fail_test

boolean

If false, a failed operations[].op=test will return none. If true, the filter invocation will fail with an error.

Choices:

  • false ← (default)

  • true

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: Apply a series of operations
  ansible.builtin.debug:
    msg: "{{ input | community.general.json_patch_recipe(operations) }}"
  vars:
    input: {}
    operations:
      - op: 'add'
        path: '/foo'
        value: 1
      - op: 'add'
        path: '/bar'
        value: []
      - op: 'add'
        path: '/bar/-'
        value: 2
      - op: 'add'
        path: '/bar/0'
        value: 1
      - op: 'remove'
        path: '/bar/0'
      - op: 'move'
        from: '/foo'
        path: '/baz'
      - op: 'copy'
        from: '/baz'
        path: '/bax'
      - op: 'copy'
        from: '/baz'
        path: '/bay'
      - op: 'replace'
        path: '/baz'
        value: [10, 20, 30]
  # => {"bar":[2],"bax":1,"bay":1,"baz":[10,20,30]}

Return Value

Key

Description

Return value

any

A modified object or none if operations[].op=test, fail_test=false and the test failed.

Returned: always

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.