def main(): argument_spec = dict( lines=dict( aliases=["commands"], required=True, type="list", elements="str" ), before=dict(type="list", elements="str"), after=dict(type="list", elements="str"), match=dict( default="line", choices=["line", "strict", "exact"], type="str" ), replace=dict(default="line", choices=["line", "block"], type="str"), force=dict(default=False, type="bool"), config=dict(type="str"), ) argument_spec.update(asa_argument_spec) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True ) lines = module.params["lines"] result = {"changed": False} if len(lines) > 0: candidate = NetworkConfig(indent=1) candidate.add(lines) acl_name = parse_acl_name(module) if not module.params["force"]: contents = get_acl_config(module, acl_name) config = NetworkConfig(indent=1, contents=contents) commands = candidate.difference(config) if commands and module.params["replace"] == "block": commands = str(candidate).split("\n") else: commands = dumps(commands, "commands").split("\n") commands = [str(c) for c in commands if c] else: commands = str(candidate).split("\n") if commands: if module.params["before"]: commands[:0] = module.params["before"] if module.params["after"]: commands.extend(module.params["after"]) if not module.check_mode: load_config(module, commands) result["changed"] = True result["updates"] = commands module.exit_json(**result)
def run(module, result): match = module.params["match"] replace = module.params["replace"] path = module.params["parents"] candidate = get_candidate(module) if match != "none": contents = module.params["config"] if not contents: contents = get_config(module) config = NetworkConfig(indent=1, contents=contents) configobjs = candidate.difference(config, path=path, match=match, replace=replace) else: configobjs = candidate.items if configobjs: commands = dumps(configobjs, "commands").split("\n") if module.params["lines"]: if module.params["before"]: commands[:0] = module.params["before"] if module.params["after"]: commands.extend(module.params["after"]) result["updates"] = commands # send the configuration commands to the device and merge # them with the current running config if not module.check_mode: load_config(module, commands) result["changed"] = True if module.params["save"]: module.warn( "module param save is deprecated, please use newer and updated param save_when instead which is released with more functionality!" ) save_config(module, result) if module.params["save_when"] == "always": save_config(module, result) elif module.params["save_when"] == "modified": running_config_checksum = run_commands( module, "show running-config | include checksum:") startup_config_checksum = run_commands( module, "show startup-config | include checksum:") if running_config_checksum != startup_config_checksum: save_config(module, result) elif module.params["save_when"] == "changed" and result["changed"]: save_config(module, result)
def main(): argument_spec = dict(lines=dict(aliases=['commands'], required=True, type='list'), before=dict(type='list'), after=dict(type='list'), match=dict(default='line', choices=['line', 'strict', 'exact']), replace=dict(default='line', choices=['line', 'block']), force=dict(default=False, type='bool'), config=dict()) argument_spec.update(asa_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) lines = module.params['lines'] result = {'changed': False} if len(lines) > 0: candidate = NetworkConfig(indent=1) candidate.add(lines) acl_name = parse_acl_name(module) if not module.params['force']: contents = get_acl_config(module, acl_name) config = NetworkConfig(indent=1, contents=contents) commands = candidate.difference(config) commands = dumps(commands, 'commands').split('\n') commands = [str(c) for c in commands if c] else: commands = str(candidate).split('\n') if commands: if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) if not module.check_mode: load_config(module, commands) result['changed'] = True result['updates'] = commands module.exit_json(**result)
def main(): argument_spec = dict( name=dict(required=True), group_type=dict( choices=["network-object", "service-object", "port-object"], required=True, ), protocol=dict(choices=["udp", "tcp", "tcp-udp"]), host_ip=dict(type="list", elements="str"), description=dict(), group_object=dict(type="list", elements="str"), ip_mask=dict(type="list", elements="str"), port_range=dict(type="list"), port_eq=dict(type="list", elements="str"), service_cfg=dict(type="list", elements="str"), state=dict( choices=["present", "absent", "replace"], default="present" ), ) required_if = [ ("group_type", "port-object", ["protocol"]), ("group_type", "service-object", ["service_cfg"]), ] module = AnsibleModule( argument_spec=argument_spec, required_if=required_if, supports_check_mode=True, ) result = {"changed": False} want = map_params_to_obj(module) have = map_config_to_obj(module) config_commans = map_obj_to_commands(want, have, module) result["commands"] = config_commans if config_commans: if not module.check_mode: load_config(module, config_commans) result["changed"] = True module.exit_json(**result)
def run(module, result): match = module.params['match'] replace = module.params['replace'] path = module.params['parents'] candidate = get_candidate(module) if match != 'none': contents = module.params['config'] if not contents: contents = get_config(module) config = NetworkConfig(indent=1, contents=contents) configobjs = candidate.difference(config, path=path, match=match, replace=replace) else: configobjs = candidate.items if configobjs: commands = dumps(configobjs, 'commands').split('\n') if module.params['lines']: if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) result['updates'] = commands # send the configuration commands to the device and merge # them with the current running config if not module.check_mode: load_config(module, commands) result['changed'] = True if module.params['save']: if not module.check_mode: run_commands(module, 'write mem') result['changed'] = True
def run(module, result): match = module.params["match"] replace = module.params["replace"] path = module.params["parents"] candidate = get_candidate(module) if match != "none": contents = module.params["config"] if not contents: contents = get_config(module) config = NetworkConfig(indent=1, contents=contents) configobjs = candidate.difference(config, path=path, match=match, replace=replace) else: configobjs = candidate.items if configobjs: commands = dumps(configobjs, "commands").split("\n") if module.params["lines"]: if module.params["before"]: commands[:0] = module.params["before"] if module.params["after"]: commands.extend(module.params["after"]) result["updates"] = commands # send the configuration commands to the device and merge # them with the current running config if not module.check_mode: load_config(module, commands) result["changed"] = True if module.params["save"]: if not module.check_mode: run_commands(module, "write mem") result["changed"] = True
def main(): argument_spec = dict(name=dict(required=True), group_type=dict(choices=[ 'network-object', 'service-object', 'port-object' ], required=True), protocol=dict(choices=['udp', 'tcp', 'tcp-udp']), host_ip=dict(type='list'), description=dict(), group_object=dict(type='list'), ip_mask=dict(type='list'), port_range=dict(type='list'), port_eq=dict(type='list'), service_cfg=dict(type='list'), state=dict(choices=['present', 'absent', 'replace'], default='present')) required_if = [('group_type', 'port-object', ['protocol']), ('group_type', 'service-object', ['service_cfg'])] module = AnsibleModule(argument_spec=argument_spec, required_if=required_if, supports_check_mode=True) result = {'changed': False} want = map_params_to_obj(module) have = map_config_to_obj(module) config_commans = map_obj_to_commands(want, have, module) result['commands'] = config_commans if config_commans: if not module.check_mode: load_config(module, config_commans) result['changed'] = True module.exit_json(**result)