diff --git a/lib/ansible/modules/network/eos/eos_template.py b/lib/ansible/modules/network/eos/eos_template.py index c7a33c43372..550d3bad616 100644 --- a/lib/ansible/modules/network/eos/eos_template.py +++ b/lib/ansible/modules/network/eos/eos_template.py @@ -125,6 +125,30 @@ def get_config(module): config = module.config return config +def filter_exit(commands): + # Filter out configuration mode commands followed immediately by an + # exit command indented by one level only, e.g. + # - route-map map01 permit 10 + # - exit + # + # Build a temporary list as we filter, then copy the temp list + # back onto the commands list. + temp = [] + ind_prev = 999 + count = 0 + for c in commands: + ind_this = c.count(' ') + if re.search(r"^\s*exit$", c) and ind_this == ind_prev + 1: + temp.pop() + count -= 1 + if count != 0: + ind_prev = temp[-1].count(' ') + continue + temp.append(c) + ind_prev = ind_this + count += 1 + return temp + def main(): """ main entry point for module execution """ @@ -146,52 +170,33 @@ def main(): replace = module.params['replace'] + commands = list() + running = None + result = dict(changed=False) candidate = NetworkConfig(contents=module.params['src'], indent=3) - contents = get_config(module) - if contents: - config = NetworkConfig(contents=contents, indent=3) - result['_backup'] = contents - - - if not module.params['force']: - commands = candidate.difference(config) - else: + if replace: + if module.params['transport'] == 'cli': + module.fail_json(msg='config replace is only supported over eapi') commands = str(candidate).split('\n') + else: + contents = get_config(module) + if contents: + running = NetworkConfig(contents=contents, indent=3) + result['_backup'] = contents - # Filter out configuration mode commands followed immediately by an - # exit command indented by one level only, e.g. - # - route-map map01 permit 10 - # - exit - # - # Build a temporary list as we filter, then copy the temp list - # back onto the commands list. - temp = [] - ind_prev = 999 - count = 0 - for c in commands: - ind_this = c.count(' ') - if re.search(r"^\s*exit$", c) and ind_this == ind_prev + 1: - temp.pop() - count -= 1 - if count != 0: - ind_prev = temp[-1].count(' ') - continue - temp.append(c) - ind_prev = ind_this - count += 1 - - commands = temp + if not module.params['force']: + commands = candidate.difference((running or list())) + else: + commands = str(candidate).split('\n') if commands: + commands = filter_exit(commands) if not module.check_mode: commands = [str(c).strip() for c in commands] - if replace: - response = module.config_replace(commands) - else: - response = module.configure(commands) + response = module.configure(commands, replace=replace) result['responses'] = response result['changed'] = True