Fixing eos_config save option. Was removed when deprecated. Fixing ot… (#35628)

* Fixing eos_config save option. Was removed when deprecated. Fixing other deprecation documentation. Adding unit tests.

* Fixing removed_in_version for force.
This commit is contained in:
James Mighion 2018-02-07 22:24:17 -08:00 committed by Trishna Guha
parent 75a285ded9
commit 663c410da4
2 changed files with 58 additions and 17 deletions

View file

@ -117,7 +117,7 @@ options:
without first checking if already configured. without first checking if already configured.
- Note this argument should be considered deprecated. To achieve - Note this argument should be considered deprecated. To achieve
the equivalent, set the C(match=none) which is idempotent. This argument the equivalent, set the C(match=none) which is idempotent. This argument
will be removed in a future release. will be removed in Ansible 2.6.
required: false required: false
default: false default: false
type: bool type: bool
@ -164,7 +164,8 @@ options:
no changes are made, the configuration is still saved to the no changes are made, the configuration is still saved to the
startup config. This option will always cause the module to startup config. This option will always cause the module to
return changed. return changed.
- This option is deprecated as of Ansible 2.4, use C(save_when) - This option is deprecated as of Ansible 2.4 and will be removed
in Ansible 2.8, use C(save_when) instead.
required: false required: false
default: false default: false
type: bool type: bool
@ -180,10 +181,12 @@ options:
will only be copied to the startup-config if it has changed since will only be copied to the startup-config if it has changed since
the last save to startup-config. If the argument is set to the last save to startup-config. If the argument is set to
I(never), the running-config will never be copied to the I(never), the running-config will never be copied to the
startup-config 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.
I(changed) was added in Ansible 2.5.
required: false required: false
default: never default: never
choices: ['always', 'never', 'modified'] choices: ['always', 'never', 'modified', 'changed']
version_added: "2.4" version_added: "2.4"
diff_against: diff_against:
description: description:
@ -303,6 +306,17 @@ def get_running_config(module, config=None):
return NetworkConfig(indent=3, contents=contents) return NetworkConfig(indent=3, contents=contents)
def save_config(module, result):
result['changed'] = True
if not module.check_mode:
cmd = {'command': 'copy running-config startup-config', 'output': 'text'}
run_commands(module, [cmd])
else:
module.warn('Skipping command `copy running-config startup-config` '
'due to check_mode. Configuration not copied to '
'non-volatile storage')
def main(): def main():
""" main entry point for module execution """ main entry point for module execution
""" """
@ -321,7 +335,7 @@ def main():
defaults=dict(type='bool', default=False), defaults=dict(type='bool', default=False),
backup=dict(type='bool', default=False), backup=dict(type='bool', default=False),
save_when=dict(choices=['always', 'never', 'modified'], default='never'), save_when=dict(choices=['always', 'never', 'modified', 'changed'], default='never'),
diff_against=dict(choices=['startup', 'session', 'intended', 'running'], default='session'), diff_against=dict(choices=['startup', 'session', 'intended', 'running'], default='session'),
diff_ignore_lines=dict(type='list'), diff_ignore_lines=dict(type='list'),
@ -330,10 +344,10 @@ def main():
intended_config=dict(), intended_config=dict(),
# save is deprecated as of ans2.4, use save_when instead # save is deprecated as of ans2.4, use save_when instead
save=dict(default=False, type='bool', removed_in_version='2.4'), save=dict(default=False, type='bool', removed_in_version='2.8'),
# force argument deprecated in ans2.2 # force argument deprecated in ans2.2
force=dict(default=False, type='bool', removed_in_version='2.2') force=dict(default=False, type='bool', removed_in_version='2.6')
) )
argument_spec.update(eos_argument_spec) argument_spec.update(eos_argument_spec)
@ -412,22 +426,20 @@ def main():
diff_ignore_lines = module.params['diff_ignore_lines'] diff_ignore_lines = module.params['diff_ignore_lines']
if module.params['save_when'] != 'never': if module.params['save_when'] == 'always' or module.params['save']:
save_config(module, result)
elif module.params['save_when'] == 'modified':
output = run_commands(module, [{'command': 'show running-config', 'output': 'text'}, output = run_commands(module, [{'command': 'show running-config', 'output': 'text'},
{'command': 'show startup-config', 'output': 'text'}]) {'command': 'show startup-config', 'output': 'text'}])
running_config = NetworkConfig(indent=1, contents=output[0], ignore_lines=diff_ignore_lines) 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) startup_config = NetworkConfig(indent=1, contents=output[1], ignore_lines=diff_ignore_lines)
if running_config.sha1 != startup_config.sha1 or module.params['save_when'] == 'always': if running_config.sha1 != startup_config.sha1:
result['changed'] = True save_config(module, result)
if not module.check_mode:
cmd = {'command': 'copy running-config startup-config', 'output': 'text'} elif module.params['save_when'] == 'changed' and result['changed']:
run_commands(module, [cmd]) save_config(module, result)
else:
module.warn('Skipping command `copy running-config startup-config` '
'due to check_mode. Configuration not copied to '
'non-volatile storage')
if module._diff: if module._diff:
if not running_config: if not running_config:

View file

@ -36,6 +36,8 @@ class TestEosConfigModule(TestEosModule):
self.mock_load_config = patch('ansible.modules.network.eos.eos_config.load_config') self.mock_load_config = patch('ansible.modules.network.eos.eos_config.load_config')
self.load_config = self.mock_load_config.start() self.load_config = self.mock_load_config.start()
self.mock_run_commands = patch('ansible.modules.network.eos.eos_config.run_commands')
self.run_commands = self.mock_run_commands.start()
def tearDown(self): def tearDown(self):
super(TestEosConfigModule, self).tearDown() super(TestEosConfigModule, self).tearDown()
@ -153,3 +155,30 @@ class TestEosConfigModule(TestEosModule):
result = self.execute_module(changed=True) result = self.execute_module(changed=True)
mock_run_commands.stop() mock_run_commands.stop()
def test_eos_config_save_changed_true(self):
commands = ['hostname foo', 'interface GigabitEthernet0/0', 'no ip address']
set_module_args(dict(save_when='changed', lines=commands))
self.execute_module(changed=True)
self.assertEqual(self.run_commands.call_count, 1)
self.assertEqual(self.get_config.call_count, 1)
self.assertEqual(self.load_config.call_count, 1)
args = self.run_commands.call_args[0][1][0]['command']
self.assertIn('copy running-config startup-config', args)
def test_eos_config_save_changed_false(self):
set_module_args(dict(save_when='changed'))
self.execute_module(changed=False)
self.assertEqual(self.run_commands.call_count, 0)
self.assertEqual(self.get_config.call_count, 0)
self.assertEqual(self.load_config.call_count, 0)
def test_eos_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.get_config.call_count, 0)
self.assertEqual(self.load_config.call_count, 0)
args = self.run_commands.call_args[0][1][0]['command']
self.assertIn('copy running-config startup-config', args)