Skip to content

Commit d9533c3

Browse files
jmighiontrishnaguha
authored andcommitted
Adding changed option to save_when (ansible#40640)
* Adding changed option to save_when * Fixing version when changed was added.
1 parent 7cf776c commit d9533c3

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

lib/ansible/modules/network/nxos/nxos_config.py

+25-18
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,11 @@
169169
will only be copied to the startup-config if it has changed since
170170
the last save to startup-config. If the argument is set to
171171
I(never), the running-config will never be copied to the
172-
startup-config
172+
startup-config. If the argument is set to I(changed), then the running-config
173+
will only be copied to the startup-config if the task has made a change.
174+
I(changed) was added in Ansible 2.6.
173175
default: never
174-
choices: ['always', 'never', 'modified']
176+
choices: ['always', 'never', 'modified', 'changed']
175177
version_added: "2.4"
176178
diff_against:
177179
description:
@@ -321,6 +323,17 @@ def execute_show_commands(module, commands, output='text'):
321323
return body
322324

323325

326+
def save_config(module, result):
327+
result['changed'] = True
328+
if not module.check_mode:
329+
cmd = {'command': 'copy running-config startup-config', 'output': 'text'}
330+
run_commands(module, [cmd])
331+
else:
332+
module.warn('Skipping command `copy running-config startup-config` '
333+
'due to check_mode. Configuration not copied to '
334+
'non-volatile storage')
335+
336+
324337
def main():
325338
""" main entry point for module execution
326339
"""
@@ -342,16 +355,16 @@ def main():
342355
defaults=dict(type='bool', default=False),
343356
backup=dict(type='bool', default=False),
344357

345-
save_when=dict(choices=['always', 'never', 'modified'], default='never'),
358+
save_when=dict(choices=['always', 'never', 'modified', 'changed'], default='never'),
346359

347360
diff_against=dict(choices=['running', 'startup', 'intended']),
348361
diff_ignore_lines=dict(type='list'),
349362

350363
# save is deprecated as of ans2.4, use save_when instead
351-
save=dict(default=False, type='bool', removed_in_version='2.4'),
364+
save=dict(default=False, type='bool', removed_in_version='2.8'),
352365

353366
# force argument deprecated in ans2.2
354-
force=dict(default=False, type='bool', removed_in_version='2.2')
367+
force=dict(default=False, type='bool', removed_in_version='2.6')
355368
)
356369

357370
argument_spec.update(nxos_argument_spec)
@@ -436,24 +449,18 @@ def main():
436449

437450
diff_ignore_lines = module.params['diff_ignore_lines']
438451

439-
if module.params['save']:
440-
module.params['save_when'] = 'always'
441-
442-
if module.params['save_when'] != 'never':
452+
if module.params['save_when'] == 'always' or module.params['save']:
453+
save_config(module, result)
454+
elif module.params['save_when'] == 'modified':
443455
output = execute_show_commands(module, ['show running-config', 'show startup-config'])
444456

445457
running_config = NetworkConfig(indent=1, contents=output[0], ignore_lines=diff_ignore_lines)
446458
startup_config = NetworkConfig(indent=1, contents=output[1], ignore_lines=diff_ignore_lines)
447459

448-
if running_config.sha1 != startup_config.sha1 or module.params['save_when'] == 'always':
449-
result['changed'] = True
450-
if not module.check_mode:
451-
cmd = {'command': 'copy running-config startup-config', 'output': 'text'}
452-
run_commands(module, [cmd])
453-
else:
454-
module.warn('Skipping command `copy running-config startup-config` '
455-
'due to check_mode. Configuration not copied to '
456-
'non-volatile storage')
460+
if running_config.sha1 != startup_config.sha1:
461+
save_config(module, result)
462+
elif module.params['save_when'] == 'changed' and result['changed']:
463+
save_config(module, result)
457464

458465
if module._diff:
459466
if not running_config:

test/units/modules/network/nxos/test_nxos_config.py

+27
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def setUp(self):
4141
self.get_capabilities = self.mock_get_capabilities.start()
4242
self.get_capabilities.return_value = {'device_info': {'network_os_platform': 'N9K-NXOSV'}}
4343

44+
self.mock_save_config = patch('ansible.modules.network.nxos.nxos_config.save_config')
45+
self.save_config = self.mock_save_config.start()
46+
4447
def tearDown(self):
4548
super(TestNxosConfigModule, self).tearDown()
4649
self.mock_get_config.stop()
@@ -148,3 +151,27 @@ def test_nxos_config_backup_returns__backup__(self):
148151
set_module_args(args)
149152
result = self.execute_module()
150153
self.assertIn('__backup__', result)
154+
155+
def test_nxos_config_save_always(self):
156+
args = dict(save_when='always')
157+
set_module_args(args)
158+
self.execute_module()
159+
self.assertEqual(self.save_config.call_count, 1)
160+
self.assertEqual(self.get_config.call_count, 0)
161+
self.assertEqual(self.load_config.call_count, 0)
162+
163+
def test_nxos_config_save_changed_true(self):
164+
args = dict(save_when='changed', lines=['hostname foo', 'interface GigabitEthernet0/0', 'no ip address'])
165+
set_module_args(args)
166+
self.execute_module(changed=True)
167+
self.assertEqual(self.save_config.call_count, 1)
168+
self.assertEqual(self.get_config.call_count, 1)
169+
self.assertEqual(self.load_config.call_count, 1)
170+
171+
def test_nxos_config_save_changed_false(self):
172+
args = dict(save_when='changed')
173+
set_module_args(args)
174+
self.execute_module()
175+
self.assertEqual(self.save_config.call_count, 0)
176+
self.assertEqual(self.get_config.call_count, 0)
177+
self.assertEqual(self.load_config.call_count, 0)

0 commit comments

Comments
 (0)