🌐
GitHub
github.com › particledecay › ansible-jsonpatch
GitHub - particledecay/ansible-jsonpatch: An Ansible module for patching JSON files
An Ansible module for patching JSON files. Contribute to particledecay/ansible-jsonpatch development by creating an account on GitHub.
Starred by 119 users
Forked by 29 users
Languages   Python 100.0% | Python 100.0%
🌐
Ansible
docs.ansible.com › projects › ansible › latest › collections › kubernetes › core › k8s_json_patch_module.html
kubernetes.core.k8s_json_patch module – Apply JSON patch operations to existing objects — Ansible Community Documentation
To use it in a playbook, specify: kubernetes.core.k8s_json_patch. New in kubernetes.core 2.0.0 · This module is used to apply RFC 6902 JSON patch operations only. Use the kubernetes.core.k8s module for strategic merge or JSON merge operations. The jsonpatch library is required for check mode.
🌐
Ansible
docs.ansible.com › projects › ansible › latest › collections › community › general › json_patch_filter.html
community.general.json_patch filter – Apply a JSON-Patch (RFC 6902) operation to an object — Ansible Community Documentation
jsonpatch Python Package · 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": ...
🌐
GitHub
github.com › particledecay › ansible-jsonpatch › blob › master › README.md
ansible-jsonpatch/README.md at master · particledecay/ansible-jsonpatch
An Ansible module for patching JSON files. Contribute to particledecay/ansible-jsonpatch development by creating an account on GitHub.
Author   particledecay
🌐
Ansible
docs.ansible.com › projects › ansible › latest › collections › community › general › json_patch_recipe_filter.html
community.general.json_patch_recipe filter – Apply JSON-Patch (RFC 6902) operations to an object — Ansible Community Documentation
jsonpatch Python Package · A Python library for applying JSON patches · - 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: ...
🌐
GitHub
github.com › GuillaumeSmaha › ansible-role-gluu › blob › master › library › jsonpatch.py
ansible-role-gluu/library/jsonpatch.py at master · GuillaumeSmaha/ansible-role-gluu
This Ansible module can be used to add, delete, and update elements · within a JSON file. options: name: path · required: true · ''' · EXAMPLES = ''' --- - hosts: all · gather_facts: no · become: no · · tasks:  ...
Author   GuillaumeSmaha
🌐
GitHub
raw.githubusercontent.com › ParticleDecay › ansible-jsonpatch › master › json_patch.py
https://raw.githubusercontent.com/ParticleDecay/an...
= bool(self.module.params.get('create_parents')) self.operations = self.module.params['operations'] try: self.patcher = JSONPatcher(self.json_doc, self.create_parents, *self.operations) except Exception as e: self.module.fail_json(msg=str(e)) self.do_backup = self.module.params.get('backup', False) self.pretty_print = self.module.params.get('pretty', False) def run(self): changed, tested = self.patcher.patch() result = {'changed': changed} if tested is not None: result['tested'] = tested if result['changed']: # let's write the changes dump_kwargs = {} if self.pretty_print: dump_kwargs.update({
🌐
GitHub
github.com › particledecay › ansible-jsonpatch › blob › master › test_json_patch.py
ansible-jsonpatch/test_json_patch.py at master · particledecay/ansible-jsonpatch
An Ansible module for patching JSON files. Contribute to particledecay/ansible-jsonpatch development by creating an account on GitHub.
Author   particledecay
🌐
Ansible
docs.ansible.com › projects › ansible › latest › collections › community › general › json_diff_filter.html
community.general.json_diff filter – Create a JSON patch by comparing two JSON files — Ansible Community Documentation
jsonpatch Python Package · A Python library for applying JSON patches · - 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": ...
Find elsewhere
🌐
GitHub
github.com › UnitedTraders › ansible-role-jsonpatch
GitHub - UnitedTraders/ansible-role-jsonpatch: Ansible command for patching JSON files
Ansible command for patching JSON files. Contribute to UnitedTraders/ansible-role-jsonpatch development by creating an account on GitHub.
Author   UnitedTraders
🌐
GitHub
github.com › fabiocaseri › ansible-jsonpatch
GitHub - fabiocaseri/ansible-jsonpatch: An Ansible module for patching JSON files
This Ansible module follows RFC 6901 for JSON notation and RFC 6902 for JSON patching. In addition to the standard, there is an extra notation feature not outlined in RFC 6901: you can specify every member of an array with a * character in the path.
Author   fabiocaseri
🌐
GitHub
github.com › particledecay › ansible-jsonpatch › blob › master › json_patch.py
ansible-jsonpatch/json_patch.py at master · particledecay/ansible-jsonpatch
An Ansible module for patching JSON files. Contribute to particledecay/ansible-jsonpatch development by creating an account on GitHub.
Author   particledecay
Top answer
1 of 2
10

Not a direct answer to your question about the error with modify_json but a working solution.

I would go with jq for that. jq is a lightweight and flexible command-line JSON processor and available for nearly every Linux distribution. When not, use the prebuilt binaries that come without dependencies.

As the website states:

jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

I shrinked your play to a minimal working solution with the same result. The jq executable must be in the PATH of the system it is running on. Feel free to customize it to your needs.

---
- name: Sync Power Schedules From Database to Survey Spec
  hosts: localhost
  gather_facts: no

  vars:
    choices_key: ".spec[6].choices"
    choices_value: "23:00-02:00\n02:00-04:00\n04:00-06:00\n00:00-04:00"
    json_file: "{{playbook_dir}}/s.json"

  tasks:

  - name: "modify json"
    command: >
      jq "{{choices_key}}=\"{{choices_value}}\"" "{{json_file}}"
    register: json

  - debug:
      var: json.stdout

I think this is more elegant as the solution with the extra json_modify.py module. For more information on jq please see the manual page.

2 of 2
1

There's just a couple of minor issues in your playbook

  1. You are loading the json data from the file as a string, rather than json/dict. Instead of your command/cat task, use this to load the data in:

     - set_fact:
         surveySpec: "{{ lookup('file', sharedDataPathFile) | from_json }}"
    
  2. The other problem you would have hit is you are attempting to update just the choices value (i.e. a string), rather than the choices dict item. Just a small change required in your json_modify pointer:

     - json_modify:
         data: "{{ surveySpec }}"
         pointer: "/spec/5"
         action: update
         update: "{{ new_choices }}"
       register: result
    

One other thing .. the array index is 5, not 6

🌐
Red Hat
access.redhat.com › solutions › 4904041
Can I Use JSON PATCH To Update Standalone API Fields in Ansible Tower ? - Red Hat Customer Portal
April 22, 2020 - I want to update a standalone API field in Ansible Tower instead of doing a PATCH of the complete JSON data structure.
🌐
Geekytidbits
geekytidbits.com › ansible-module-modify-json
Ansible Module for Modifying JSON | Geeky Tidbits
I just recently finished a side project in Node.js and decided to use Ansible for provisioning a Digital Ocean server and deploying the app to it. As part of the deploy playbook, I needed a way to set production specific config values in a JSON file. There isn’t a native way to do this in Ansible (that I know of at least) but I wrote a module to do it.
🌐
GitHub
github.com › ansible-collections › kubernetes.core › pull › 99
Split JSON patch out into a new module by gravesm · Pull Request #99 · ansible-collections/kubernetes.core
The merge_type parameter allows more than one merge strategy to be specified, but there is no case where a JSON patch could work for JSON merge or strategic merge, nor the other way around. This commit moves JSON patch functionality out into a separate module. Check mode support is provided through the third party jsonpatch library.
Author   ansible-collections
🌐
GitHub
github.com › particledecay › ansible-jsonpatch › issues › 14
wish: add ansible-jsonpatch to galaxy · Issue #14 · particledecay/ansible-jsonpatch
May 17, 2020 - I just want to let you @particledecay know that we're still using ansible-jsonpatch, it's being accessed by some playbooks of ours, and we are very happy and grateful to be able to do so.