From 594675120c7bfcbfdf3a791741b57081e168b6ab Mon Sep 17 00:00:00 2001 From: Petr OspalĂ˝ Date: Wed, 19 Dec 2018 13:28:29 +0100 Subject: Add ansible library to modify json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue-ID: OOM-1551 Change-Id: I791bf7163a023ddc140cee22d474fcf532beae17 Signed-off-by: Petr OspalĂ˝ --- ansible/library/json_add.py | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 ansible/library/json_add.py (limited to 'ansible/library') diff --git a/ansible/library/json_add.py b/ansible/library/json_add.py new file mode 100644 index 00000000..6aad2d7c --- /dev/null +++ b/ansible/library/json_add.py @@ -0,0 +1,90 @@ +#!/usr/bin/python + +from ansible.module_utils.basic import AnsibleModule +import json +import os + +DOCUMENTATION=""" +--- +module: json_add +descritption: + - This module will search top level objects in json and adds specified + value into list for specified key. + - If file does not exists module will create it automatically. + +options: + path: + required: true + aliases=[name, destfile, dest] + description: + - The json file to modify. + key: + required: true + description: + - Top level object. + value: + required: true + description: + - Value to add to specified key. +""" + +def load_json(path): + if os.path.exists(path): + with open(path, 'r') as f: + return json.load(f) + else: + return {} + +def value_is_set(path, key, value, json_obj): + return value in json_obj.get(key, []) + +def insert_to_json(path, key, value, check_mode=False): + json_obj = load_json(path) + if not value_is_set(path, key, value, json_obj): + if not check_mode: + json_obj.setdefault(key, []).append(value) + store_json(path, json_obj) + return True, 'Value %s added to %s.' % (value, key) + else: + return False, '' + +def store_json(path, json_obj): + with open(path, 'w') as f: + json.dump(json_obj, f, indent=4) + +def check_file_attrs(module, changed, message, diff): + file_args = module.load_file_common_arguments(module.params) + if module.set_fs_attributes_if_different(file_args, False, diff=diff): + + if changed: + message += ' ' + changed = True + message += 'File attributes changed.' + + return changed, message + +def run_module(): + module = AnsibleModule( + argument_spec=dict( + path=dict(type='path', required=True, aliases=['name', 'destfile', 'dest']), + key=dict(type='str', required=True), + value=dict(type='str', required=True), + ), + add_file_common_args=True, + supports_check_mode=True + ) + params = module.params + path = params['path'] + key = params['key'] + value = params['value'] + try: + changed, msg = insert_to_json(path, key, value, module.check_mode) + fs_diff = {} + changed, msg = check_file_attrs(module, changed, msg, fs_diff) + module.exit_json(changed=changed, msg=msg, file_attr_diff=fs_diff) + except IOError as e: + module.fail_json(msg=e.msg) + +if __name__ == '__main__': + run_module() + -- cgit 1.2.3-korg