particledecay / ansible-jsonpatch

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

How to add object with multiple values into array #23

Closed OndrosI closed 1 year ago

OndrosI commented 2 years ago

Hello, i have this json

{
  "repositories": [
    { 
      "name": "some-name", 
      "type": "vcs", 
      "url": "git@xxx.git" 
    }
  ]
}

and i am trying to create this

{
  "repositories": [
    { 
      "name": "some-name", 
      "type": "vcs", 
      "url": "git@xxx.git" 
    },
    { 
      "name": "new-value", 
      "type": "vcs", 
      "url": "git@new-value.git" 
    }
  ]
}

but i dont know how. Is there possibility add multiple values into object?

like this:

- name: append key/values
  json_patch:
    src: /file.json
    operations:
      - op: add
        path: '/repositories/-'
        value: 
          name: "new-value"
          type: "vcs"
          url: "git@new-value.git"
    pretty: yes
    create: yes
particledecay commented 1 year ago

You can have multiple operations, unfortunately the object properties would have to be in separate operations.