Skip to content

Instantly share code, notes, and snippets.

@jpralves
Created November 25, 2020 16:15
Show Gist options
  • Save jpralves/718c233a7e8f02f57fc46fe1a17196ff to your computer and use it in GitHub Desktop.
Save jpralves/718c233a7e8f02f57fc46fe1a17196ff to your computer and use it in GitHub Desktop.
Merge the content of a json file with another json file

Ansible instructions to merge two json files.

Assumes files: original.json, merge.json and new.json

---
- name: get content of original file
  slurp:
    src: "original.json"
  register: original_config
  ignore_errors: yes

- name: convert content to json and load merge json
  set_fact:
    original_json: "{{ original_config.content|default('e30=') | b64decode | from_json }}"
    merge_json: "{{ lookup('file', merge.json' ) | from_json }}"

- name: merge content
  copy:
    content: "{{ original_json | combine(merge_json) | to_nice_json(indent=2) }}"
    dest: "new.json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment