def main(): """ main entry point for module execution """ element_spec = dict( dest=dict(choices=['console', 'host', 'file', 'user']), name=dict(), facility=dict(), level=dict(), rotate_frequency=dict(type='int'), size=dict(type='int'), files=dict(type='int'), src_addr=dict(), state=dict(default='present', choices=['present', 'absent']), active=dict(default=True, type='bool') ) aggregate_spec = deepcopy(element_spec) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) argument_spec = dict( aggregate=dict(type='list', elements='dict', options=aggregate_spec), ) argument_spec.update(element_spec) argument_spec.update(junos_argument_spec) required_if = [('dest', 'host', ['name', 'facility', 'level']), ('dest', 'file', ['name', 'facility', 'level']), ('dest', 'user', ['name', 'facility', 'level']), ('dest', 'console', ['facility', 'level'])] module = AnsibleModule(argument_spec=argument_spec, required_if=required_if, supports_check_mode=True) warnings = list() result = {'changed': False} if warnings: result['warnings'] = warnings params = to_param_list(module) requests = list() for param in params: # if key doesn't exist in the item, get it from module.params for key in param: if param.get(key) is None: param[key] = module.params[key] module._check_required_if(required_if, param) item = param.copy() dest = item.get('dest') if dest == 'console' and item.get('name'): module.fail_json(msg="%s and %s are mutually exclusive" % ('console', 'name')) top = 'system/syslog' is_facility_key = False field_top = None if dest: if dest == 'console': field_top = dest is_facility_key = True else: field_top = dest + '/contents' is_facility_key = False param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([ ('name', {'xpath': 'name', 'is_key': True, 'top': dest}), ('facility', {'xpath': 'name', 'is_key': is_facility_key, 'top': field_top}), ('size', {'xpath': 'size', 'leaf_only': True, 'is_key': True, 'top': 'archive'}), ('files', {'xpath': 'files', 'leaf_only': True, 'is_key': True, 'top': 'archive'}), ('rotate_frequency', {'xpath': 'log-rotate-frequency', 'leaf_only': True}), ]) if item.get('level'): param_to_xpath_map['level'] = {'xpath': item.get('level'), 'tag_only': True, 'top': field_top} validate_param_values(module, param_to_xpath_map, param=item) want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append(map_obj_to_ele(module, want, top, param=item)) diff = None with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action='merge') commit = not module.check_mode if diff: if commit: commit_configuration(module) else: discard_changes(module) result['changed'] = True if module._diff: result['diff'] = {'prepared': diff} module.exit_json(**result)
def main(): """ main entry point for module execution """ element_spec = dict( dest=dict(choices=["console", "host", "file", "user"]), name=dict(), facility=dict(), level=dict(), rotate_frequency=dict(type="int"), size=dict(type="int"), files=dict(type="int"), src_addr=dict(), state=dict(default="present", choices=["present", "absent"]), active=dict(default=True, type="bool"), ) aggregate_spec = deepcopy(element_spec) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) argument_spec = dict( aggregate=dict(type="list", elements="dict", options=aggregate_spec)) argument_spec.update(element_spec) argument_spec.update(junos_argument_spec) required_if = [ ("dest", "host", ["name", "facility", "level"]), ("dest", "file", ["name", "facility", "level"]), ("dest", "user", ["name", "facility", "level"]), ("dest", "console", ["facility", "level"]), ] module = AnsibleModule( argument_spec=argument_spec, required_if=required_if, supports_check_mode=True, ) warnings = list() result = {"changed": False} if warnings: result["warnings"] = warnings params = to_param_list(module) requests = list() for param in params: # if key doesn't exist in the item, get it from module.params for key in param: if param.get(key) is None: param[key] = module.params[key] module._check_required_if(required_if, param) item = param.copy() dest = item.get("dest") if dest == "console" and item.get("name"): module.fail_json(msg="%s and %s are mutually exclusive" % ("console", "name")) top = "system/syslog" is_facility_key = False field_top = None if dest: if dest == "console": field_top = dest is_facility_key = True else: field_top = dest + "/contents" is_facility_key = False param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([ ("name", { "xpath": "name", "is_key": True, "top": dest }), ( "facility", { "xpath": "name", "is_key": is_facility_key, "top": field_top, }, ), ( "size", { "xpath": "size", "leaf_only": True, "is_key": True, "top": "archive", }, ), ( "files", { "xpath": "files", "leaf_only": True, "is_key": True, "top": "archive", }, ), ( "rotate_frequency", { "xpath": "log-rotate-frequency", "leaf_only": True }, ), ]) if item.get("level"): param_to_xpath_map["level"] = { "xpath": item.get("level"), "tag_only": True, "top": field_top, } validate_param_values(module, param_to_xpath_map, param=item) want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append(map_obj_to_ele(module, want, top, param=item)) diff = None with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action="merge") commit = not module.check_mode if diff: if commit: commit_configuration(module) else: discard_changes(module) result["changed"] = True if module._diff: result["diff"] = {"prepared": diff} module.exit_json(**result)