From f5d865a208cdbff17db3525fed1658b141f0a385 Mon Sep 17 00:00:00 2001 From: Ganesh Nalawade Date: Tue, 16 May 2017 19:54:36 +0530 Subject: [PATCH] Fixes #24251 save config only if it is changed (#24345) * Fixes #24251 save config only if it is changed Save to startup configuration only when it is different from running configuration. * Fix unit test issue --- lib/ansible/modules/network/eos/eos_config.py | 6 ++++-- lib/ansible/modules/network/ios/ios_config.py | 6 ++++-- test/units/modules/network/ios/test_ios_config.py | 3 ++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/network/eos/eos_config.py b/lib/ansible/modules/network/eos/eos_config.py index 2b401548a3..7163c59dbb 100644 --- a/lib/ansible/modules/network/eos/eos_config.py +++ b/lib/ansible/modules/network/eos/eos_config.py @@ -331,8 +331,10 @@ def main(): if module.params['save']: if not module.check_mode: - run_commands(module, ['copy running-config startup-config']) - result['changed'] = True + response = run_commands(module, ['show running-config diffs']) + if len(response[0]): + run_commands(module, ['copy running-config startup-config']) + result['changed'] = True module.exit_json(**result) diff --git a/lib/ansible/modules/network/ios/ios_config.py b/lib/ansible/modules/network/ios/ios_config.py index 5a60314c91..d3f8a0f155 100644 --- a/lib/ansible/modules/network/ios/ios_config.py +++ b/lib/ansible/modules/network/ios/ios_config.py @@ -382,8 +382,10 @@ def main(): if module.params['save']: if not module.check_mode: - run_commands(module, ['copy running-config startup-config\r']) - result['changed'] = True + response = run_commands(module, ['show archive config differences']) + if response[0].find('!No changes were found') < 0: + run_commands(module, ['copy running-config startup-config\r']) + result['changed'] = True module.exit_json(**result) diff --git a/test/units/modules/network/ios/test_ios_config.py b/test/units/modules/network/ios/test_ios_config.py index f7d2af4582..cc34aef665 100644 --- a/test/units/modules/network/ios/test_ios_config.py +++ b/test/units/modules/network/ios/test_ios_config.py @@ -69,9 +69,10 @@ class TestIosConfigModule(TestIosModule): self.assertIn('__backup__', result) def test_ios_config_save(self): + self.run_commands.return_value = "Hostname foo" set_module_args(dict(save=True)) self.execute_module(changed=True) - self.assertEqual(self.run_commands.call_count, 1) + self.assertEqual(self.run_commands.call_count, 2) self.assertEqual(self.get_config.call_count, 0) self.assertEqual(self.load_config.call_count, 0) args = self.run_commands.call_args[0][1]