Note
This filter plugin is part of the community.general collection (version 10.7.3).
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.
New in community.general 10.3.0
none value otherwise.The below requirements are needed on the local controller node that executes this filter.
This describes the input of the filter, the value before | community.general.json_patch.
Parameter | Comments |
|---|---|
Input any / required | A list or a dictionary representing a JSON object, or a string containing a JSON object. |
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(positional1, positional2, ...)
Parameter | Comments |
|---|---|
op string / required | |
path string / required | JSON Pointer path to the target location (see RFC 6901). |
value any |
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.json_patch(key1=value1, key2=value2, ...)
Parameter | Comments |
|---|---|
fail_test boolean | If Choices:
|
from string |
Note
input | community.general.json_patch(positional1, positional2, key1=value1, key2=value2)
See also
JavaScript Object Notation (JSON) Patch
JavaScript Object Notation (JSON) Pointer
A Python library for applying JSON patches
- name: Insert a new element into an array at a specified index
ansible.builtin.debug:
msg: "{{ input | community.general.json_patch('add', '/1', {'baz': 'qux'}) }}"
vars:
input: ["foo": { "one": 1 }, "bar": { "two": 2 }]
# => [{"foo": {"one": 1}}, {"baz": "qux"}, {"bar": {"two": 2}}]
- name: Insert a new key into a dictionary
ansible.builtin.debug:
msg: "{{ input | community.general.json_patch('add', '/bar/baz', 'qux') }}"
vars:
input: { "foo": { "one": 1 }, "bar": { "two": 2 } }
# => {"foo": {"one": 1}, "bar": {"baz": "qux", "two": 2}}
- name: Input is a string
ansible.builtin.debug:
msg: "{{ input | community.general.json_patch('add', '/baz', 3) }}"
vars:
input: '{ "foo": { "one": 1 }, "bar": { "two": 2 } }'
# => {"foo": {"one": 1}, "bar": { "two": 2 }, "baz": 3}
- name: Existing key is replaced
ansible.builtin.debug:
msg: "{{ input | community.general.json_patch('add', '/bar', 'qux') }}"
vars:
input: { "foo": { "one": 1 }, "bar": { "two": 2 } }
# => {"foo": {"one": 1}, "bar": "qux"}
- name: Escaping tilde as ~0 and slash as ~1 in the path
ansible.builtin.debug:
msg: "{{ input | community.general.json_patch('add', '/~0~1', 'qux') }}"
vars:
input: {}
# => {"~/": "qux"}
- name: Add at the end of the array
ansible.builtin.debug:
msg: "{{ input | community.general.json_patch('add', '/-', 4) }}"
vars:
input: [1, 2, 3]
# => [1, 2, 3, 4]
- name: Remove a key
ansible.builtin.debug:
msg: "{{ input | community.general.json_patch('remove', '/bar') }}"
vars:
input: { "foo": { "one": 1 }, "bar": { "two": 2 } }
# => {"foo": {"one": 1} }
- name: Replace a value
ansible.builtin.debug:
msg: "{{ input | community.general.json_patch('replace', '/bar', 2) }}"
vars:
input: { "foo": { "one": 1 }, "bar": { "two": 2 } }
# => {"foo": {"one": 1}, "bar": 2}
- name: Copy a value
ansible.builtin.debug:
msg: "{{ input | community.general.json_patch('copy', '/baz', from='/bar') }}"
vars:
input: { "foo": { "one": 1 }, "bar": { "two": 2 } }
# => {"foo": {"one": 1}, "bar": { "two": 2 }, "baz": { "two": 2 }}
- name: Move a value
ansible.builtin.debug:
msg: "{{ input | community.general.json_patch('move', '/baz', from='/bar') }}"
vars:
input: { "foo": { "one": 1 }, "bar": { "two": 2 } }
# => {"foo": {"one": 1}, "baz": { "two": 2 }}
- name: Successful test
ansible.builtin.debug:
msg: "{{ input | community.general.json_patch('test', '/bar/two', 2) | ternary('OK', 'Failed') }}"
vars:
input: { "foo": { "one": 1 }, "bar": { "two": 2 } }
# => OK
- name: Unuccessful test
ansible.builtin.debug:
msg: "{{ input | community.general.json_patch('test', '/bar/two', 9) | ternary('OK', 'Failed') }}"
vars:
input: { "foo": { "one": 1 }, "bar": { "two": 2 } }
# => Failed
Key | Description |
|---|---|
Return value any | A modified object or Returned: always |
© 2012–2018 Michael DeHaan
© 2018–2025 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/latest/collections/community/general/json_patch_filter.html