169
169
will only be copied to the startup-config if it has changed since
170
170
the last save to startup-config. If the argument is set to
171
171
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.
173
175
default: never
174
- choices: ['always', 'never', 'modified']
176
+ choices: ['always', 'never', 'modified', 'changed' ]
175
177
version_added: "2.4"
176
178
diff_against:
177
179
description:
@@ -321,6 +323,17 @@ def execute_show_commands(module, commands, output='text'):
321
323
return body
322
324
323
325
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
+
324
337
def main ():
325
338
""" main entry point for module execution
326
339
"""
@@ -342,16 +355,16 @@ def main():
342
355
defaults = dict (type = 'bool' , default = False ),
343
356
backup = dict (type = 'bool' , default = False ),
344
357
345
- save_when = dict (choices = ['always' , 'never' , 'modified' ], default = 'never' ),
358
+ save_when = dict (choices = ['always' , 'never' , 'modified' , 'changed' ], default = 'never' ),
346
359
347
360
diff_against = dict (choices = ['running' , 'startup' , 'intended' ]),
348
361
diff_ignore_lines = dict (type = 'list' ),
349
362
350
363
# 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 ' ),
352
365
353
366
# 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 ' )
355
368
)
356
369
357
370
argument_spec .update (nxos_argument_spec )
@@ -436,24 +449,18 @@ def main():
436
449
437
450
diff_ignore_lines = module .params ['diff_ignore_lines' ]
438
451
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' :
443
455
output = execute_show_commands (module , ['show running-config' , 'show startup-config' ])
444
456
445
457
running_config = NetworkConfig (indent = 1 , contents = output [0 ], ignore_lines = diff_ignore_lines )
446
458
startup_config = NetworkConfig (indent = 1 , contents = output [1 ], ignore_lines = diff_ignore_lines )
447
459
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 )
457
464
458
465
if module ._diff :
459
466
if not running_config :
0 commit comments