feat(config): implement diff mode support

This commit is contained in:
s3lph 2023-02-08 01:37:33 +01:00
parent f3115f8fd7
commit 408cd09886
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5

View file

@ -90,16 +90,15 @@ def iter_system(module, result, tree=None, value=None):
tree = [] tree = []
if value is None: if value is None:
value = module.params['system'] value = module.params['system']
changed = False
# Recursively iterate the options tree # Recursively iterate the options tree
for k, v in value.items(): for k, v in value.items():
subtree = tree + [k] subtree = tree + [k]
if isinstance(v, dict): if isinstance(v, dict):
changed = changed or iter_system(module, result, subtree, v) iter_system(module, result, subtree, v)
continue continue
if isinstance(v, list): if isinstance(v, list):
v = {str(i): v for i, v in enumerate(v)} v = {str(i): v for i, v in enumerate(v)}
changed = changed or iter_system(module, result, subtree, v) iter_system(module, result, subtree, v)
continue continue
elif isinstance(v, int): elif isinstance(v, int):
typ = 'integer' typ = 'integer'
@ -126,7 +125,12 @@ def iter_system(module, result, tree=None, value=None):
continue continue
elif v == old_value: elif v == old_value:
continue continue
changed = True result['changed'] = True
stjoined = ' => '.join(subtree)
if old_value is not None:
result['diff'][0]['before'] += 'system => {} => {}'.format(app, stjoined, old_value)
if v is not None:
result['diff'][0]['after'] += 'system => {} => {}'.format(app, stjoined, v)
if not module.check_mode: if not module.check_mode:
# Remove key if the new value is none # Remove key if the new value is none
@ -152,8 +156,6 @@ def iter_system(module, result, tree=None, value=None):
msg = 'occ config:system:set returned non-zero exit code. Run with -vvv to view the output' msg = 'occ config:system:set returned non-zero exit code. Run with -vvv to view the output'
module.fail_json(msg=msg, **result) module.fail_json(msg=msg, **result)
return changed
def run_module(): def run_module():
# define available arguments/parameters a user can pass to the module # define available arguments/parameters a user can pass to the module
@ -172,6 +174,7 @@ def run_module():
# for consumption, for example, in a subsequent task # for consumption, for example, in a subsequent task
result = dict( result = dict(
changed=False, changed=False,
diff=[dict(before='', after='')],
) )
# the AnsibleModule object will be our abstraction working with Ansible # the AnsibleModule object will be our abstraction working with Ansible
@ -202,7 +205,7 @@ def run_module():
module.fail_json(msg=msg, **result) module.fail_json(msg=msg, **result)
# Apply Nextcloud system configuration recursively # Apply Nextcloud system configuration recursively
result['changed'] = iter_system(module, result) iter_system(module, result)
# Apply Nextcloud app configuration # Apply Nextcloud app configuration
for app, ac in module.params['apps'].items(): for app, ac in module.params['apps'].items():
@ -221,7 +224,11 @@ def run_module():
old_value = rc.stdout old_value = rc.stdout
if old_value == v: if old_value == v:
continue continue
changed = True result['changed'] = True
if old_value is not None:
result['diff'][0]['before'] += 'apps => {} => {} => {}'.format(app, k, old_value)
if v is not None:
result['diff'][0]['after'] += 'apps => {} => {} => {}'.format(app, k, v)
if not module.check_mode: if not module.check_mode:
# Delete key if value is None # Delete key if value is None