Adding changed option to save_when (#40640)
* Adding changed option to save_when * Fixing version when changed was added.
This commit is contained in:
parent
7cf776ca5c
commit
d9533c3cbf
2 changed files with 52 additions and 18 deletions
|
@ -169,9 +169,11 @@ 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.6.
|
||||||
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:
|
||||||
|
@ -321,6 +323,17 @@ def execute_show_commands(module, commands, output='text'):
|
||||||
return body
|
return body
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
"""
|
"""
|
||||||
|
@ -342,16 +355,16 @@ 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=['running', 'startup', 'intended']),
|
diff_against=dict(choices=['running', 'startup', 'intended']),
|
||||||
diff_ignore_lines=dict(type='list'),
|
diff_ignore_lines=dict(type='list'),
|
||||||
|
|
||||||
# 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(nxos_argument_spec)
|
argument_spec.update(nxos_argument_spec)
|
||||||
|
@ -436,24 +449,18 @@ def main():
|
||||||
|
|
||||||
diff_ignore_lines = module.params['diff_ignore_lines']
|
diff_ignore_lines = module.params['diff_ignore_lines']
|
||||||
|
|
||||||
if module.params['save']:
|
if module.params['save_when'] == 'always' or module.params['save']:
|
||||||
module.params['save_when'] = 'always'
|
save_config(module, result)
|
||||||
|
elif module.params['save_when'] == 'modified':
|
||||||
if module.params['save_when'] != 'never':
|
|
||||||
output = execute_show_commands(module, ['show running-config', 'show startup-config'])
|
output = execute_show_commands(module, ['show running-config', 'show startup-config'])
|
||||||
|
|
||||||
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:
|
elif module.params['save_when'] == 'changed' and result['changed']:
|
||||||
cmd = {'command': 'copy running-config startup-config', 'output': 'text'}
|
save_config(module, result)
|
||||||
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')
|
|
||||||
|
|
||||||
if module._diff:
|
if module._diff:
|
||||||
if not running_config:
|
if not running_config:
|
||||||
|
|
|
@ -41,6 +41,9 @@ class TestNxosConfigModule(TestNxosModule):
|
||||||
self.get_capabilities = self.mock_get_capabilities.start()
|
self.get_capabilities = self.mock_get_capabilities.start()
|
||||||
self.get_capabilities.return_value = {'device_info': {'network_os_platform': 'N9K-NXOSV'}}
|
self.get_capabilities.return_value = {'device_info': {'network_os_platform': 'N9K-NXOSV'}}
|
||||||
|
|
||||||
|
self.mock_save_config = patch('ansible.modules.network.nxos.nxos_config.save_config')
|
||||||
|
self.save_config = self.mock_save_config.start()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
super(TestNxosConfigModule, self).tearDown()
|
super(TestNxosConfigModule, self).tearDown()
|
||||||
self.mock_get_config.stop()
|
self.mock_get_config.stop()
|
||||||
|
@ -148,3 +151,27 @@ class TestNxosConfigModule(TestNxosModule):
|
||||||
set_module_args(args)
|
set_module_args(args)
|
||||||
result = self.execute_module()
|
result = self.execute_module()
|
||||||
self.assertIn('__backup__', result)
|
self.assertIn('__backup__', result)
|
||||||
|
|
||||||
|
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=['hostname foo', 'interface GigabitEthernet0/0', 'no ip address'])
|
||||||
|
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)
|
||||||
|
|
Loading…
Reference in a new issue