ansible-jsonpatch icon indicating copy to clipboard operation
ansible-jsonpatch copied to clipboard

Missing key is not created when trying to add an element to a key that does not exist yet.

Open schneidr opened this issue 3 years ago • 0 comments

I want to add an item to an array in the JSON file, if the key already exists it should be added, if the key doesn't exist yet it should be created.

Ansible task:

- name: configure docker daemon for denyusernshost
  json_patch:
    src: "/etc/docker/daemon.json"
    create: yes
    pretty: yes
    operations:
      - op: add
        path: "/authorization-plugins/-"
        value: "denyusernshost"

Expected result:

{
    "authorization-plugins": ["denyusernshost"]
}

Traceback:

Traceback (most recent call last):
File "/tmp/ansible_json_patch_payload_pk9_yaou/ansible_json_patch_payload.zip/ansible/modules/json_patch.py", line 404, in add
KeyError: 'authorization-plugins'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/username/.ansible/tmp/ansible-tmp-1630314042.2142181-13278-33899836330021/AnsiballZ_json_patch.py", line 102, in <module>
    _ansiballz_main()
File "/home/username/.ansible/tmp/ansible-tmp-1630314042.2142181-13278-33899836330021/AnsiballZ_json_patch.py", line 94, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/home/username/.ansible/tmp/ansible-tmp-1630314042.2142181-13278-33899836330021/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 "/usr/lib/python3.8/runpy.py", line 207, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
File "/usr/lib/python3.8/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
File "/tmp/ansible_json_patch_payload_pk9_yaou/ansible_json_patch_payload.zip/ansible/modules/json_patch.py", line 567, in <module>
File "/tmp/ansible_json_patch_payload_pk9_yaou/ansible_json_patch_payload.zip/ansible/modules/json_patch.py", line 561, in main
File "/tmp/ansible_json_patch_payload_pk9_yaou/ansible_json_patch_payload.zip/ansible/modules/json_patch.py", line 246, in run
File "/tmp/ansible_json_patch_payload_pk9_yaou/ansible_json_patch_payload.zip/ansible/modules/json_patch.py", line 341, in patch
File "/tmp/ansible_json_patch_payload_pk9_yaou/ansible_json_patch_payload.zip/ansible/modules/json_patch.py", line 406, in add
__main__.PathError: could not find 'authorization-plugins' member in JSON object

schneidr avatar Aug 30 '21 09:08 schneidr