Ian Wienand 0b9fad9583 update-json-file: add role to combine values into a .json
Ansible doens't really have a great built-in way to modify a json file
(unlike ini files).  The extant docker role does what seems to be the
usual standard, which is slurp in the file, parse it and then write it
back out.

In a follow-on change (I338616c41a65b007d56648fdab6da2a6a6b909f4) we
need to set some more values in the docker configuration .json file,
which made me think it's generic enough that we can have a role to
basically run read the file, |combine and write it back out.

This adds such a role with various options, and converts the existing
json configuration update in ensure-docker to use it.

Change-Id: I155a409945e0175249cf2dc630b839c7a97fb452
2020-10-05 15:18:58 +11:00

39 lines
1004 B
YAML

- name: Check if file exists
stat:
path: '{{ update_json_file_name }}'
register: _stat
- name: Load existing file
when: _stat.stat.exists
slurp:
path: '{{ update_json_file_name }}'
register: _file
- name: Parse exisiting file
when: _stat.stat.exists
set_fact:
_config: "{{ _file.content | b64decode | from_json }}"
- name: Set default for non existing file
when: not _stat.stat.exists
set_fact:
_config: '{{ update_json_file_default }}'
- name: Combine new configuration
set_fact:
_config: "{{ _config | combine(update_json_file_combine) }}"
- name: Debug _config variable
debug:
var: _config
when: update_json_file_debug
- name: Save new file
copy:
content: "{{ _config | to_nice_json }}"
dest: '{{ update_json_file_name }}'
mode: '{{ update_json_file_mode | default(omit) }}'
owner: '{{ update_json_file_owner | default(omit) }}'
group: '{{ update_json_file_group | default(omit) }}'
become: '{{ update_json_file_become }}'