particledecay / ansible-jsonpatch

An Ansible module for patching JSON files
MIT License
118 stars 27 forks source link

converting to pretty format only #16

Closed rebelnn closed 1 year ago

rebelnn commented 3 years ago

Hi, I'm using this module only to convert the format to "pretty" however I get an error message:

my code:

 - name: Ensure the Ansible project is enabled
    json_patch:
      src: "test8.json"
      pretty: yes
      create: yes
      operations:
        - op: copy
          path: "/etc/ansible/test9_jolehet.json"

here's my output:

MSG:

MODULE FAILURE
See stdout/stderr for the exact error

MODULE_STDERR:

Traceback (most recent call last):
  File "/Users/asd/.ansible/tmp/ansible-local-12529opgh61t/ansible-tmp-1602859283.986748-1320-41886083824797/AnsiballZ_json_patch.py", line 102, in <module>
    _ansiballz_main()
  File "/Users/asd/.ansible/tmp/ansible-local-12529opgh61t/ansible-tmp-1602859283.986748-1320-41886083824797/AnsiballZ_json_patch.py", line 94, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/Users/asd/.ansible/tmp/ansible-local-12529opgh61t/ansible-tmp-1602859283.986748-1320-41886083824797/AnsiballZ_json_patch.py", line 40, in invoke_module
    runpy.run_module(mod_name='ansible.modules.json_patch', init_globals=None, run_name='__main__', alter_sys=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 188, in run_module
    fname, loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 82, in _run_module_code
    mod_name, mod_fname, mod_loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/var/folders/sz/g96y86bd1ndfx_r38rlq2ds00000gn/T/ansible_json_patch_payload_5banL0/ansible_json_patch_payload.zip/ansible/modules/json_patch.py", line 567, in <module>
  File "/var/folders/sz/g96y86bd1ndfx_r38rlq2ds00000gn/T/ansible_json_patch_payload_5banL0/ansible_json_patch_payload.zip/ansible/modules/json_patch.py", line 561, in main
  File "/var/folders/sz/g96y86bd1ndfx_r38rlq2ds00000gn/T/ansible_json_patch_payload_5banL0/ansible_json_patch_payload.zip/ansible/modules/json_patch.py", line 246, in run
  File "/var/folders/sz/g96y86bd1ndfx_r38rlq2ds00000gn/T/ansible_json_patch_payload_5banL0/ansible_json_patch_payload.zip/ansible/modules/json_patch.py", line 341, in patch
TypeError: copy() takes exactly 4 arguments (3 given)

:ansible asd$ ansible --version | grep "python version" python version = 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) [Clang 6.0 (clang-600.0.57)] :ansible asd$ ansible --version ansible 2.9.9

Any idea what could it be?

particledecay commented 3 years ago

There are a few problems here:

  1. The copy operation is a JSON Patch operation, not an Ansible operation. So the path you specified as a filename is incorrect, it should actually be a path in JSON notation as described in RFC 6901. An example of this would be /firstObj/secondObj/2/myVal.
  2. The copy operation will copy a value from one path to another path. Right now you're missing the from path. See this for an example.
  3. I'm not sure you could actually prettify the JSON without performing a destructive operation on the JSON itself first. I haven't really considered the need for a noop style of patching. One potential hack might be:
- name: Ensure the Ansible project is enabled
  json_patch:
    src: "test8.json"
    pretty: yes
    create: yes
    operations:
      - op: add
        path: /foo
        value: bar
      - op: remove
        path: /foo

I think that might work as long as the top-level JSON object is an object and not an array? This would just create and remove a dummy value but it's enough to tell the module that something needs to be written because it changed.