Merge "update-json-file: add role to combine values into a .json"
This commit is contained in:
commit
e59b68d909
@ -45,6 +45,7 @@ General Purpose Roles
|
||||
.. zuul:autorole:: start-zuul-console
|
||||
.. zuul:autorole:: test-setup
|
||||
.. zuul:autorole:: trigger-readthedocs
|
||||
.. zuul:autorole:: update-json-file
|
||||
.. zuul:autorole:: upload-artifactory
|
||||
.. zuul:autorole:: upload-git-mirror
|
||||
.. zuul:autorole:: validate-dco-license
|
||||
|
@ -15,34 +15,14 @@
|
||||
- name: Update docker daemon configuration
|
||||
when: docker_userland_proxy is defined
|
||||
block:
|
||||
- name: Check if docker daemon configuration exists
|
||||
stat:
|
||||
path: /etc/docker/daemon.json
|
||||
register: docker_config_stat
|
||||
- name: Load docker daemon configuration
|
||||
when: docker_config_stat.stat.exists
|
||||
slurp:
|
||||
path: /etc/docker/daemon.json
|
||||
register: docker_config
|
||||
- name: Parse docker daemon configuration
|
||||
when: docker_config_stat.stat.exists
|
||||
set_fact:
|
||||
docker_config: "{{ docker_config.content | b64decode | from_json }}"
|
||||
- name: Set default docker daemon configuration
|
||||
when: not docker_config_stat.stat.exists
|
||||
set_fact:
|
||||
docker_config: {}
|
||||
- name: Add registry to docker daemon configuration
|
||||
- name: Add proxy config
|
||||
include_role:
|
||||
name: update-json-file
|
||||
vars:
|
||||
new_config:
|
||||
update_json_file_name: /etc/docker/daemon.json
|
||||
update_json_file_combine:
|
||||
userland-proxy: "{{ docker_userland_proxy }}"
|
||||
set_fact:
|
||||
docker_config: "{{ docker_config | combine(new_config) }}"
|
||||
- name: Save docker daemon configuration
|
||||
copy:
|
||||
content: "{{ docker_config | to_nice_json }}"
|
||||
dest: /etc/docker/daemon.json
|
||||
become: true
|
||||
update_json_file_become: true
|
||||
|
||||
- name: Reset ssh connection to pick up docker group
|
||||
meta: reset_connection
|
||||
|
48
roles/update-json-file/README.rst
Normal file
48
roles/update-json-file/README.rst
Normal file
@ -0,0 +1,48 @@
|
||||
Update JSON file
|
||||
|
||||
This role reads a JSON file, merges it with supplied values using
|
||||
Ansible's ``combine`` filter and writes it back out. It is useful for
|
||||
updating configuration files. Note this role is not currently
|
||||
idempotent and will write the file each time.
|
||||
|
||||
**Role Variables**
|
||||
|
||||
.. zuul:rolevar:: update_json_file_name
|
||||
:type: path
|
||||
|
||||
The path to the file to edit.
|
||||
|
||||
.. zuul:rolevar:: update_json_file_combine
|
||||
:type: object
|
||||
|
||||
The data to be combined with the existing file data. This uses the
|
||||
Jinja ``combine`` filter.
|
||||
|
||||
.. zuul:rolevar:: update_json_file_debug
|
||||
:default: false
|
||||
:type: bool
|
||||
|
||||
If enabled, output the combined result in a debug task.
|
||||
|
||||
.. zuul:rolevar:: update_json_file_default
|
||||
:default: {}
|
||||
|
||||
The default value if the given file does not exist.
|
||||
|
||||
.. zuul:rolevar:: update_json_file_become
|
||||
:type: bool
|
||||
:default: false
|
||||
|
||||
The ``become:`` status when writing out the new file.
|
||||
|
||||
.. zuul:rolevar:: update_json_file_mode
|
||||
|
||||
The mode for the combined file.
|
||||
|
||||
.. zuul:rolevar:: update_json_file_user
|
||||
|
||||
The user for the combined file.
|
||||
|
||||
.. zuul:rolevar:: update_json_file_group
|
||||
|
||||
The group for the combined file.
|
3
roles/update-json-file/defaults/main.yaml
Normal file
3
roles/update-json-file/defaults/main.yaml
Normal file
@ -0,0 +1,3 @@
|
||||
update_json_file_debug: false
|
||||
update_json_file_become: false
|
||||
update_json_file_default: {}
|
38
roles/update-json-file/tasks/main.yaml
Normal file
38
roles/update-json-file/tasks/main.yaml
Normal file
@ -0,0 +1,38 @@
|
||||
- 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 }}'
|
40
test-playbooks/update-json-file.yaml
Normal file
40
test-playbooks/update-json-file.yaml
Normal file
@ -0,0 +1,40 @@
|
||||
- hosts: all
|
||||
tasks:
|
||||
|
||||
- include_role:
|
||||
name: update-json-file
|
||||
vars:
|
||||
update_json_file_name: test.json
|
||||
update_json_file_default:
|
||||
foo: bar
|
||||
update_json_file_combine:
|
||||
moo: boo
|
||||
update_json_file_debug: true
|
||||
|
||||
- include_role:
|
||||
name: update-json-file
|
||||
vars:
|
||||
update_json_file_name: test.json
|
||||
update_json_file_combine:
|
||||
new: content
|
||||
a:
|
||||
- list
|
||||
- of
|
||||
- items
|
||||
update_json_file_debug: true
|
||||
|
||||
- name: Load resulting merged file
|
||||
slurp:
|
||||
path: 'test.json'
|
||||
register: _file
|
||||
|
||||
- name: Parse merged file
|
||||
set_fact:
|
||||
_config: "{{ _file.content | b64decode | from_json }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- _config['foo'] == 'bar'
|
||||
- _config['moo'] == 'boo'
|
||||
- _config['new'] == 'content'
|
||||
- _config['a'] == ['list', 'of', 'items']
|
@ -642,6 +642,14 @@
|
||||
- name: fedora-32
|
||||
label: fedora-32
|
||||
|
||||
- job:
|
||||
name: zuul-jobs-test-update-json-file
|
||||
description: Test the json edit role
|
||||
run: test-playbooks/update-json-file.yaml
|
||||
files:
|
||||
- test-playbooks/update-json-file.yaml
|
||||
- roles/update-json-file/.*
|
||||
|
||||
# -* AUTOGENERATED *-
|
||||
# The following project section is autogenerated by
|
||||
# tox -e update-test-platforms
|
||||
@ -692,6 +700,7 @@
|
||||
- zuul-jobs-test-upload-git-mirror
|
||||
- zuul-jobs-test-shake-build
|
||||
- zuul-jobs-test-ensure-zookeeper
|
||||
- zuul-jobs-test-update-json-file
|
||||
gate:
|
||||
jobs: &id001
|
||||
- zuul-jobs-test-add-authorized-keys
|
||||
@ -732,5 +741,6 @@
|
||||
- zuul-jobs-test-upload-git-mirror
|
||||
- zuul-jobs-test-shake-build
|
||||
- zuul-jobs-test-ensure-zookeeper
|
||||
- zuul-jobs-test-update-json-file
|
||||
periodic-weekly:
|
||||
jobs: *id001
|
||||
|
Loading…
Reference in New Issue
Block a user