def get_candidate_config(module): candidate = '' if module.params['src']: candidate_obj = NetworkConfig(indent=0) candidate_obj.loadfp(module.params['src']) candidate = dumps(candidate_obj, 'raw') elif module.params['lines']: candidate_obj = NetworkConfig(indent=4) parents = module.params['parents'] or list() candidate_obj.add(module.params['lines'], parents=parents) candidate = dumps(candidate_obj, 'raw') return candidate
def get_diff(self, candidate=None, running=None, diff_match=None, diff_ignore_lines=None): diff = {} device_operations = self.get_device_operations() option_values = self.get_option_values() if candidate is None and device_operations['supports_generate_diff']: raise ValueError( "candidate configuration is required to generate diff") if diff_match not in option_values['diff_match']: raise ValueError( "'match' value %s in invalid, valid values are %s" % (diff_match, ', '.join(option_values['diff_match']))) candidate_obj = NetworkConfig(indent=1) candidate_obj.load(candidate) if running and diff_match != 'none': running_obj = NetworkConfig(indent=1, contents=running, ignore_lines=diff_ignore_lines) config_diff_objs = candidate_obj.difference(running_obj, match=diff_match) else: config_diff_objs = candidate_obj.items diff['config_diff'] = dumps(config_diff_objs, 'commands') if config_diff_objs else '' return diff
def get_diff(self, candidate=None, running=None, diff_match='line', diff_ignore_lines=None, path=None, diff_replace='line'): diff = {} # prepare candidate configuration candidate_obj = ImishConfig(indent=1) candidate_obj.load(candidate) if running and diff_match != 'none' and diff_replace != 'config': # running configuration running_obj = ImishConfig(indent=1, contents=running, ignore_lines=diff_ignore_lines) configdiffobjs = candidate_obj.difference(running_obj, path=path, match=diff_match, replace=diff_replace) else: configdiffobjs = candidate_obj.items diff['config_diff'] = dumps(configdiffobjs, 'commands') if configdiffobjs else '' return diff
def get_diff( self, candidate=None, running=None, diff_match="line", diff_ignore_lines=None, path=None, diff_replace="line", ): diff = {} # prepare candidate configuration candidate_obj = NetworkConfig(indent=3) candidate_obj.load(candidate) if running and diff_match != "none" and diff_replace != "config": # running configuration running_obj = NetworkConfig( indent=3, contents=running, ignore_lines=diff_ignore_lines ) configdiffobjs = candidate_obj.difference( running_obj, path=path, match=diff_match, replace=diff_replace ) else: configdiffobjs = candidate_obj.items diff["config_diff"] = ( dumps(configdiffobjs, "commands") if configdiffobjs else {} ) return diff
def run(module, result): match = module.params['match'] replace = module.params['replace'] replace_config = replace == 'config' path = module.params['parents'] comment = module.params['comment'] admin = module.params['admin'] check_mode = module.check_mode candidate = get_candidate(module) if match != 'none' and replace != 'config': contents = get_running_config(module) configobj = NetworkConfig(contents=contents, indent=1) commands = candidate.difference(configobj, path=path, match=match, replace=replace) else: commands = candidate.items if commands: commands = dumps(commands, 'commands').split('\n') if any((module.params['lines'], module.params['src'])): if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) result['commands'] = commands diff = load_config(module, commands) if diff: result['diff'] = dict(prepared=diff) result['changed'] = True
def test_updates_repeat_parents(): expected_diff = [ "interface loopback0", "ip ospf 1 area 0.0.0.0", "interface GigabitEthernet0/1", "ip ospf 1 area 0.0.0.0", "interface GigabitEthernet0/2", "ip ospf 1 area 0.0.0.0", "router isis fabric", "is-type level-2", "net 49.0000.0000.0003.00", "interface loopback0", "ip router isis fabric", "interface GigabitEthernet0/1", "ip router isis fabric", "interface GigabitEthernet0/2", "ip router isis fabric", ] candidate_obj = config.NetworkConfig(indent=1) candidate_obj.load(WANT_SRC_2) running_obj = config.NetworkConfig(indent=1, contents=HAVE_SRC_2) configdiffobjs = candidate_obj.difference(running_obj) diff_list = config.dumps(configdiffobjs, "commands").split("\n") for generated_diff_line, candidate_diff_line in zip( diff_list, expected_diff): print(generated_diff_line, candidate_diff_line) assert generated_diff_line == candidate_diff_line.strip()
def get_diff(self, candidate=None, running=None, diff_match='line', diff_ignore_lines=None, path=None, diff_replace='line'): diff = {} device_operations = self.get_device_operations() option_values = self.get_option_values() if candidate is None and device_operations['supports_generate_diff']: raise ValueError("candidate configuration is required to generate diff") if diff_match not in option_values['diff_match']: raise ValueError("'match' value %s in invalid, valid values are %s" % (diff_match, ', '.join(option_values['diff_match']))) if diff_replace not in option_values['diff_replace']: raise ValueError("'replace' value %s in invalid, valid values are %s" % (diff_replace, ', '.join(option_values['diff_replace']))) # prepare candidate configuration sanitized_candidate = sanitize_config(candidate) candidate_obj = NetworkConfig(indent=1) candidate_obj.load(sanitized_candidate) if running and diff_match != 'none': # running configuration running = mask_config_blocks_from_diff(running, candidate, "ansible") running = sanitize_config(running) running_obj = NetworkConfig(indent=1, contents=running, ignore_lines=diff_ignore_lines) configdiffobjs = candidate_obj.difference(running_obj, path=path, match=diff_match, replace=diff_replace) else: configdiffobjs = candidate_obj.items diff['config_diff'] = dumps(configdiffobjs, 'commands') if configdiffobjs else '' return diff
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 get_diff( self, candidate=None, running=None, diff_match="line", diff_ignore_lines=None, path=None, diff_replace="line", ): diff = {} device_operations = self.get_device_operations() option_values = self.get_option_values() if candidate is None and device_operations["supports_generate_diff"]: raise ValueError( "candidate configuration is required to generate diff" ) if diff_match not in option_values["diff_match"]: raise ValueError( "'match' value %s in invalid, valid values are %s" % (diff_match, ", ".join(option_values["diff_match"])) ) if diff_replace not in option_values["diff_replace"]: raise ValueError( "'replace' value %s in invalid, valid values are %s" % (diff_replace, ", ".join(option_values["diff_replace"])) ) # prepare candidate configuration sanitized_candidate = sanitize_config(candidate) candidate_obj = NetworkConfig(indent=1, comment_tokens=["!"]) candidate_obj.load(sanitized_candidate) if running and diff_match != "none": # running configuration running = mask_config_blocks_from_diff( running, candidate, "ansible" ) running = sanitize_config(running) running_obj = NetworkConfig( indent=1, contents=running, ignore_lines=diff_ignore_lines, comment_tokens=["!"], ) configdiffobjs = candidate_obj.difference( running_obj, path=path, match=diff_match, replace=diff_replace ) else: configdiffobjs = candidate_obj.items diff["config_diff"] = ( dumps(configdiffobjs, "commands") if configdiffobjs else "" ) return diff
def get_candidate(module): candidate = NetworkConfig(indent=1) if module.params['src']: candidate.load(module.params['src']) elif module.params['lines']: candidate.add(module.params['lines']) candidate = dumps(candidate, 'raw') return candidate
def get_candidate_config(module): candidate = '' if module.params['src']: candidate = module.params['src'] elif module.params['lines']: candidate_obj = NetworkConfig(indent=1) candidate_obj.add(module.params['lines']) candidate = dumps(candidate_obj, 'raw') return candidate
def get_candidate_config(module): candidate = "" if module.params["src"]: candidate = module.params["src"] elif module.params["lines"]: candidate_obj = NetworkConfig(indent=1) parents = module.params["parents"] or list() candidate_obj.add(module.params["lines"], parents=parents) candidate = dumps(candidate_obj, "raw") return candidate
def get_candidate(self): candidate = '' if self.want.src: candidate = self.want.src elif self.want.lines: candidate_obj = NetworkConfig(indent=1) parents = self.want.parents or list() candidate_obj.add(self.want.lines, parents=parents) candidate = dumps(candidate_obj, 'raw') return candidate
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 test_updates_repeat_lines(): candidate_obj = config.NetworkConfig(indent=3) candidate_obj.load(WANT_SRC_1) running_obj = config.NetworkConfig(indent=3, contents=HAVE_SRC_1) configdiffobjs = candidate_obj.difference(running_obj) diff_list = config.dumps(configdiffobjs, "commands").split("\n") want_src_list = WANT_SRC_1.strip().split("\n") for generated_diff_line, candidate_diff_line in zip( diff_list, want_src_list): assert generated_diff_line == candidate_diff_line.strip()
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 get_candidate_config(module): """ gets the set of commands to configure """ candidate = "" candidate_obj = NetworkConfig(indent=1) if module.params["src"]: candidate_obj.loadfp(module.params["src"]) elif module.params["lines"]: parents = module.params["parents"] or list() candidate_obj.add(module.params["lines"], parents=parents) candidate = dumps(candidate_obj, "raw") return candidate
def get_candidate(module): candidate = '' if module.params['src']: if module.params['replace'] != 'config': candidate = module.params['src'] if module.params['replace'] == 'config': candidate = 'config replace {0}'.format(module.params['replace_src']) elif module.params['lines']: candidate_obj = NetworkConfig(indent=2) parents = module.params['parents'] or list() candidate_obj.add(module.params['lines'], parents=parents) candidate = dumps(candidate_obj, 'raw') return candidate
def get_candidate(module): candidate = "" if module.params["src"]: if module.params["replace"] != "config": candidate = module.params["src"] if module.params["replace"] == "config": candidate = "config replace {0}".format(module.params["replace_src"]) elif module.params["lines"]: candidate_obj = NetworkConfig(indent=2) parents = module.params["parents"] or list() candidate_obj.add(module.params["lines"], parents=parents) candidate = dumps(candidate_obj, "raw") return candidate
def run(module, result): match = module.params['match'] replace = module.params['replace'] replace_config = replace == 'config' diff_ignore_lines = module.params['diff_ignore_lines'] path = module.params['parents'] check_mode = module.check_mode candidate = get_candidate(module) if match != 'none' and replace != 'config': contents = get_running_config(module) configobj = NetworkConfig(contents=contents, indent=1) commands = candidate.difference(configobj, path=path, match=match, replace=replace) else: commands = candidate.items if commands: commands = dumps(commands, 'commands').split('\n') if any((module.params['lines'], module.params['src'])): if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) result['commands'] = commands diff = load_config(module, commands) if diff: result['diff'] = dict(prepared=diff) result['changed'] = True running_config = module.params['running_config'] startup_config = None if module.params['save_when'] == 'always': save_config(module, result) elif module.params['save_when'] == 'modified': output = run_commands(module, ['show running-config', 'show startup-config']) running_config = NetworkConfig(indent=1, contents=output[0], ignore_lines=diff_ignore_lines) startup_config = NetworkConfig(indent=1, contents=output[1], ignore_lines=diff_ignore_lines) if running_config.sha1 != startup_config.sha1: save_config(module, result) elif module.params['save_when'] == 'changed' and result['changed']: save_config(module, result)
def get_candidate(self): candidate = '' if self.want.src: candidate = self.want.src elif self.want.lines: candidate_obj = ImishConfig(indent=1) parents = self.want.parents or list() if self.want.allow_duplicates: candidate_obj.add(self.want.lines, parents=parents, duplicates=True) else: candidate_obj.add(self.want.lines, parents=parents) candidate = dumps(candidate_obj, 'raw') return candidate
def get_diff( self, candidate=None, running=None, diff_match="line", diff_ignore_lines=None, path=None, diff_replace="line", ): diff = {} device_operations = self.get_device_operations() option_values = self.get_option_values() if candidate is None and device_operations["supports_generate_diff"]: raise ValueError( "candidate configuration is required to generate diff") if diff_match not in option_values["diff_match"]: raise ValueError( "'match' value %s in invalid, valid values are %s" % (diff_match, ", ".join(option_values["diff_match"])), ) if diff_replace not in option_values["diff_replace"]: raise ValueError( "'replace' value %s in invalid, valid values are %s" % (diff_replace, ", ".join(option_values["diff_replace"])), ) # prepare candidate configuration candidate_obj = NetworkConfig(indent=2) candidate_obj.load(candidate) if running and diff_match != "none" and diff_replace != "config": # running configuration running_obj = NetworkConfig(indent=2, contents=running, ignore_lines=diff_ignore_lines) configdiffobjs = candidate_obj.difference( running_obj, path=path, match=diff_match, replace=diff_replace, ) else: configdiffobjs = candidate_obj.items diff["config_diff"] = dumps(configdiffobjs, "commands") if configdiffobjs else "" return diff
def run(module, result): match = module.params['match'] replace = module.params['replace'] path = module.params['parents'] candidate, want_banners = get_candidate(module) if match != 'none': config, have_banners = get_config(module, result) path = module.params['parents'] configobjs = candidate.difference(config, path=path, match=match, replace=replace) else: configobjs = candidate.items have_banners = {} banners = diff_banners(want_banners, have_banners) if configobjs or banners: 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 result['banners'] = banners # send the configuration commands to the device and merge # them with the current running config if not module.check_mode: if commands: module.config(commands) if banners: load_banners(module, banners) result['changed'] = True if module.params['save']: if not module.check_mode: module.config.save_config() result['changed'] = True
def run(module, result): match = module.params['match'] replace = module.params['replace'] path = module.params['parents'] configobjs = None 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 result['changed'] or module.params['save_when'] == 'always': result['changed'] = True if not module.check_mode: cmd = {'command': 'write memory'} run_commands(module, [cmd])
def main(): """ main entry point for module execution """ argument_spec = dict(rollback_location=dict(), local_max_checkpoints=dict(type='int'), remote_max_checkpoints=dict(type='int'), rescue_location=dict(), state=dict(default='present', choices=['present', 'absent'])) argument_spec.update(sros_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) state = module.params['state'] result = dict(changed=False) commands = list() invoke(state, module, commands) candidate = NetworkConfig(indent=4, contents='\n'.join(commands)) config = get_device_config(module) configobjs = candidate.difference(config) if configobjs: # commands = dumps(configobjs, 'lines') commands = dumps(configobjs, 'commands') commands = sanitize_config(commands.split('\n')) result['updates'] = commands result['commands'] = 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 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 get_diff(self, candidate=None, running=None, diff_match='line', path=None, diff_replace='line'): diff = {} device_operations = self.get_device_operations() option_values = self.get_option_values() if candidate is None and device_operations['supports_generate_diff']: raise ValueError( "candidate configuration is required to generate diff") if diff_match not in option_values['diff_match']: raise ValueError( "'match' value %s in invalid, valid values are %s" % (diff_match, ', '.join(option_values['diff_match']))) if diff_replace not in option_values['diff_replace']: raise ValueError( "'replace' value %s in invalid, valid values are %s" % (diff_replace, ', '.join(option_values['diff_replace']))) # prepare candidate configuration candidate_obj = NetworkConfig(indent=4, contents=candidate) if running and diff_match != "none" and diff_replace != "config": running_obj = load_running_config(running=running) configdiffobjs = candidate_obj.difference(running_obj, path=path, match=diff_match, replace=diff_replace) else: configdiffobjs = candidate_obj.items diff["config_diff"] = (dumps(configdiffobjs, "commands") if configdiffobjs else "") return diff
def get_diff(self, candidate=None, running=None, diff_match='line', diff_ignore_lines=None, path=None, diff_replace=None): diff = {} device_operations = self.get_device_operations() option_values = self.get_option_values() if candidate is None and device_operations['supports_generate_diff']: raise ValueError("candidate configuration is required to generate diff") if diff_match not in option_values['diff_match']: raise ValueError("'match' value %s in invalid, valid values are %s" % (diff_match, ', '.join(option_values['diff_match']))) if diff_replace: raise ValueError("'replace' in diff is not supported") if diff_ignore_lines: raise ValueError("'diff_ignore_lines' in diff is not supported") if path: raise ValueError("'path' in diff is not supported") # prepare candidate configuration candidate_obj = NetworkConfig(indent=1) want_src, want_banners = self._extract_banners(candidate) candidate_obj.load(want_src) if running and diff_match != 'none': # running configuration have_src, have_banners = self._extract_banners(running) running_obj = NetworkConfig(indent=1, contents=have_src, ignore_lines=diff_ignore_lines) configdiffobjs = candidate_obj.difference(running_obj, path=path, match=diff_match, replace=diff_replace) else: configdiffobjs = candidate_obj.items have_banners = {} diff['config_diff'] = dumps(configdiffobjs, 'commands') if configdiffobjs else '' banners = self._diff_banners(want_banners, have_banners) diff['banner_diff'] = banners if banners else {} return diff
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 total_commands = [] 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']) total_commands.extend(commands) result['updates'] = total_commands if module.params['save']: total_commands.append('configuration write') if total_commands: result['changed'] = True if not module.check_mode: load_config(module, total_commands)
def run(module, result): match = module.params['match'] candidate = get_candidate(module) if match != 'none': config_text = get_active_config(module) config = NetworkConfig(indent=4, contents=config_text) configobjs = candidate.difference(config) else: configobjs = candidate.items if configobjs: commands = dumps(configobjs, 'commands') commands = commands.split('\n') result['commands'] = commands 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