From 408cd0988641cf92c1929005cfdaffe091182391 Mon Sep 17 00:00:00 2001 From: s3lph Date: Wed, 8 Feb 2023 01:37:33 +0100 Subject: [PATCH] feat(config): implement diff mode support --- plugins/modules/config.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/plugins/modules/config.py b/plugins/modules/config.py index a6ab66b..4304c4f 100644 --- a/plugins/modules/config.py +++ b/plugins/modules/config.py @@ -90,16 +90,15 @@ def iter_system(module, result, tree=None, value=None): tree = [] if value is None: value = module.params['system'] - changed = False # Recursively iterate the options tree for k, v in value.items(): subtree = tree + [k] if isinstance(v, dict): - changed = changed or iter_system(module, result, subtree, v) + iter_system(module, result, subtree, v) continue if isinstance(v, list): 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 elif isinstance(v, int): typ = 'integer' @@ -126,7 +125,12 @@ def iter_system(module, result, tree=None, value=None): continue elif v == old_value: 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: # 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' module.fail_json(msg=msg, **result) - return changed - def run_module(): # define available arguments/parameters a user can pass to the module @@ -172,8 +174,9 @@ def run_module(): # for consumption, for example, in a subsequent task result = dict( changed=False, + diff=[dict(before='', after='')], ) - + # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module @@ -202,7 +205,7 @@ def run_module(): module.fail_json(msg=msg, **result) # Apply Nextcloud system configuration recursively - result['changed'] = iter_system(module, result) + iter_system(module, result) # Apply Nextcloud app configuration for app, ac in module.params['apps'].items(): @@ -221,7 +224,11 @@ def run_module(): old_value = rc.stdout if old_value == v: 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: # Delete key if value is None