From 083ddb9bc225c0a5bf688d9180107d97a8e6f255 Mon Sep 17 00:00:00 2001 From: James Mighion Date: Thu, 31 May 2018 07:35:12 -0700 Subject: [PATCH] Adding changed option to save_when for aireos (#40681) * Adding changed option to save_when for aireos * Deprecating save option for aireos_config. * Updating version_added to 2.7 since the PR missed the window for 2.6 --- .../modules/network/aireos/aireos_config.py | 52 ++++++++++++++----- .../network/aireos/test_aireos_config.py | 32 ++++++++++-- 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/lib/ansible/modules/network/aireos/aireos_config.py b/lib/ansible/modules/network/aireos/aireos_config.py index f7b42a510e..5736727b09 100644 --- a/lib/ansible/modules/network/aireos/aireos_config.py +++ b/lib/ansible/modules/network/aireos/aireos_config.py @@ -82,11 +82,29 @@ options: aliases: ['config'] save: description: - - The C(save) argument instructs the module to save the running- - config to the startup-config at the conclusion of the module - running. If check mode is specified, this argument is ignored. + - The C(save) argument instructs the module to save the + running-config to startup-config. This operation is performed + after any changes are made to the current running config. If + no changes are made, the configuration is still saved to the + startup config. This option will always cause the module to + return changed. This argument is mutually exclusive with I(save_when). + - This option is deprecated as of Ansible 2.7, use C(save_when) type: bool default: 'no' + save_when: + description: + - When changes are made to the device running-configuration, the + changes are not copied to non-volatile storage by default. Using + this argument will change that. If the argument is set to + I(always), then the running-config will always be copied to the + startup-config and the module will always return as changed. + If the argument is set to I(never), the running-config will never + be copied to the startup-config. If the argument is set to I(changed), + then the running-config will only be copied to the startup-config if + the task has made a change. + default: never + choices: ['always', 'never', 'changed'] + version_added: "2.7" diff_against: description: - When using the C(ansible-playbook --diff) command line argument @@ -178,6 +196,16 @@ def get_candidate(module): return candidate +def save_config(module, result): + result['changed'] = True + if not module.check_mode: + command = {"command": "save config", "prompt": "Are you sure you want to save", "answer": "y"} + run_commands(module, command) + else: + module.warn('Skipping command `save config` due to check_mode. Configuration not copied to ' + 'non-volatile storage') + + def main(): """ main entry point for module execution """ @@ -196,7 +224,9 @@ def main(): backup=dict(type='bool', default=False), - save=dict(type='bool', default=False), + # save is deprecated as of 2.7, use save_when instead + save=dict(type='bool', default=False, removed_in_version='2.11'), + save_when=dict(choices=['always', 'never', 'changed'], default='never'), diff_against=dict(choices=['running', 'intended']), diff_ignore_lines=dict(type='list') @@ -204,7 +234,8 @@ def main(): argument_spec.update(aireos_argument_spec) - mutually_exclusive = [('lines', 'src')] + mutually_exclusive = [('lines', 'src'), + ('save', 'save_when')] required_if = [('diff_against', 'intended', ['intended_config'])] @@ -255,13 +286,10 @@ def main(): diff_ignore_lines = module.params['diff_ignore_lines'] - if module.params['save']: - result['changed'] = True - if not module.check_mode: - command = {"command": "save config", "prompt": "Are you sure you want to save", "answer": "y"} - run_commands(module, command) - else: - module.warn('Skipping command `save config` due to check_mode. Configuration not copied to non-volatile storage') + if module.params['save_when'] == 'always' or module.params['save']: + save_config(module, result) + elif module.params['save_when'] == 'changed' and result['changed']: + save_config(module, result) if module._diff: output = run_commands(module, 'show run-config commands') diff --git a/test/units/modules/network/aireos/test_aireos_config.py b/test/units/modules/network/aireos/test_aireos_config.py index 7f15c87937..002e839b72 100644 --- a/test/units/modules/network/aireos/test_aireos_config.py +++ b/test/units/modules/network/aireos/test_aireos_config.py @@ -42,6 +42,9 @@ class TestCiscoWlcConfigModule(TestCiscoWlcModule): self.mock_run_commands = patch('ansible.modules.network.aireos.aireos_config.run_commands') self.run_commands = self.mock_run_commands.start() + self.mock_save_config = patch('ansible.modules.network.aireos.aireos_config.save_config') + self.save_config = self.mock_save_config.start() + def tearDown(self): super(TestCiscoWlcConfigModule, self).tearDown() self.mock_get_config.stop() @@ -70,10 +73,9 @@ class TestCiscoWlcConfigModule(TestCiscoWlcModule): self.assertIn('__backup__', result) def test_aireos_config_save(self): - self.run_commands.return_value = "sysname foo" set_module_args(dict(save=True)) - self.execute_module(changed=True) - self.assertEqual(self.run_commands.call_count, 1) + self.execute_module() + self.assertEqual(self.save_config.call_count, 1) self.assertEqual(self.get_config.call_count, 0) self.assertEqual(self.load_config.call_count, 0) @@ -103,3 +105,27 @@ class TestCiscoWlcConfigModule(TestCiscoWlcModule): lines = ['sysname router', 'interface create mtc-1 1'] set_module_args(dict(lines=lines, match='none')) self.execute_module(changed=True, commands=lines, sort=False) + + def test_nxos_config_save_always(self): + args = dict(save_when='always') + set_module_args(args) + self.execute_module() + self.assertEqual(self.save_config.call_count, 1) + self.assertEqual(self.get_config.call_count, 0) + self.assertEqual(self.load_config.call_count, 0) + + def test_nxos_config_save_changed_true(self): + args = dict(save_when='changed', lines=['sysname foo', 'interface create mtc-3 3']) + set_module_args(args) + self.execute_module(changed=True) + self.assertEqual(self.save_config.call_count, 1) + self.assertEqual(self.get_config.call_count, 1) + self.assertEqual(self.load_config.call_count, 1) + + def test_nxos_config_save_changed_false(self): + args = dict(save_when='changed') + set_module_args(args) + self.execute_module() + self.assertEqual(self.save_config.call_count, 0) + self.assertEqual(self.get_config.call_count, 0) + self.assertEqual(self.load_config.call_count, 0)