fix nxos_snapshot (#27993)
Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>
This commit is contained in:
parent
17a259a163
commit
aef4e3d8fa
1 changed files with 15 additions and 84 deletions
|
@ -131,24 +131,15 @@ EXAMPLES = '''
|
||||||
description: Done with Ansible
|
description: Done with Ansible
|
||||||
save_snapshot_locally: true
|
save_snapshot_locally: true
|
||||||
path: /home/user/snapshots/
|
path: /home/user/snapshots/
|
||||||
host: "{{ inventory_hostname }}"
|
|
||||||
username: "{{ un }}"
|
|
||||||
password: "{{ pwd }}"
|
|
||||||
|
|
||||||
# Delete a snapshot
|
# Delete a snapshot
|
||||||
- nxos_snapshot:
|
- nxos_snapshot:
|
||||||
action: delete
|
action: delete
|
||||||
snapshot_name: test_snapshot
|
snapshot_name: test_snapshot
|
||||||
host: "{{ inventory_hostname }}"
|
|
||||||
username: "{{ un }}"
|
|
||||||
password: "{{ pwd }}"
|
|
||||||
|
|
||||||
# Delete all existing snapshots
|
# Delete all existing snapshots
|
||||||
- nxos_snapshot:
|
- nxos_snapshot:
|
||||||
action: delete_all
|
action: delete_all
|
||||||
host: "{{ inventory_hostname }}"
|
|
||||||
username: "{{ un }}"
|
|
||||||
password: "{{ pwd }}"
|
|
||||||
|
|
||||||
# Add a show command for snapshots creation
|
# Add a show command for snapshots creation
|
||||||
- nxos_snapshot:
|
- nxos_snapshot:
|
||||||
|
@ -156,9 +147,6 @@ EXAMPLES = '''
|
||||||
show_command: show ip interface brief
|
show_command: show ip interface brief
|
||||||
row_id: ROW_intf
|
row_id: ROW_intf
|
||||||
element_key1: intf-name
|
element_key1: intf-name
|
||||||
host: "{{ inventory_hostname }}"
|
|
||||||
username: "{{ un }}"
|
|
||||||
password: "{{ pwd }}"
|
|
||||||
|
|
||||||
# Compare two snapshots
|
# Compare two snapshots
|
||||||
- nxos_snapshot:
|
- nxos_snapshot:
|
||||||
|
@ -168,47 +156,16 @@ EXAMPLES = '''
|
||||||
comparison_results_file: compare_snapshots.txt
|
comparison_results_file: compare_snapshots.txt
|
||||||
compare_option: summary
|
compare_option: summary
|
||||||
path: '../snapshot_reports/'
|
path: '../snapshot_reports/'
|
||||||
host: "{{ inventory_hostname }}"
|
|
||||||
username: "{{ un }}"
|
|
||||||
password: "{{ pwd }}"
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
RETURN = '''
|
RETURN = '''
|
||||||
existing_snapshots:
|
commands:
|
||||||
description: list of existing snapshots.
|
|
||||||
returned: verbose mode
|
|
||||||
type: list
|
|
||||||
sample: [{"date": "Tue Sep 13 10:58:08 2016",
|
|
||||||
"description": "First snapshot", "name": "first_snap"},
|
|
||||||
{"date": "Tue Sep 13 10:27:31 2016", "description": "Pre-snapshot",
|
|
||||||
"name": "pre_snapshot"}]
|
|
||||||
final_snapshots:
|
|
||||||
description: list of final snapshots.
|
|
||||||
returned: verbose mode
|
|
||||||
type: list
|
|
||||||
sample: [{"date": "Tue Sep 13 10:58:08 2016",
|
|
||||||
"description": "First snapshot", "name": "first_snap"},
|
|
||||||
{"date": "Tue Sep 13 10:27:31 2016", "description": "Pre-snapshot",
|
|
||||||
"name": "pre_snapshot"},
|
|
||||||
{"date": "Tue Sep 13 10:37:50 2016", "description": "Post-snapshot",
|
|
||||||
"name": "post_snapshot"}]
|
|
||||||
report_file:
|
|
||||||
description: name of the file where the new snapshot or snapshots
|
|
||||||
comparison have been stored.
|
|
||||||
returned: verbose mode
|
|
||||||
type: string
|
|
||||||
sample: "/home/gabriele/Desktop/ntc-ansible/ansible_snapshot"
|
|
||||||
updates:
|
|
||||||
description: commands sent to the device
|
description: commands sent to the device
|
||||||
returned: verbose mode
|
returned: verbose mode
|
||||||
type: list
|
type: list
|
||||||
sample: ["snapshot create post_snapshot Post-snapshot"]
|
sample: ["snapshot create post_snapshot Post-snapshot"]
|
||||||
changed:
|
|
||||||
description: check to see if a change was made on the device
|
|
||||||
returned: always
|
|
||||||
type: boolean
|
|
||||||
sample: true
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
@ -217,14 +174,13 @@ from ansible.module_utils.nxos import load_config, run_commands
|
||||||
from ansible.module_utils.nxos import nxos_argument_spec, check_args
|
from ansible.module_utils.nxos import nxos_argument_spec, check_args
|
||||||
|
|
||||||
|
|
||||||
def execute_show_command(command, module, command_type='cli_show_ascii'):
|
def execute_show_command(command, module):
|
||||||
cmds = [command]
|
command = [{
|
||||||
if module.params['transport'] == 'cli':
|
'command': command,
|
||||||
body = run_commands(module, cmds)
|
'output': 'text',
|
||||||
elif module.params['transport'] == 'nxapi':
|
}]
|
||||||
body = run_commands(module, cmds)
|
|
||||||
|
|
||||||
return body
|
return run_commands(module, command)
|
||||||
|
|
||||||
|
|
||||||
def get_existing(module):
|
def get_existing(module):
|
||||||
|
@ -433,45 +389,20 @@ def main():
|
||||||
module.fail_json(msg='snapshot_name is required when action=delete')
|
module.fail_json(msg='snapshot_name is required when action=delete')
|
||||||
|
|
||||||
existing_snapshots = invoke('get_existing', module)
|
existing_snapshots = invoke('get_existing', module)
|
||||||
final_snapshots = existing_snapshots
|
|
||||||
changed = False
|
|
||||||
|
|
||||||
action_results = invoke('action_%s' % action, module, existing_snapshots)
|
action_results = invoke('action_%s' % action, module, existing_snapshots)
|
||||||
|
|
||||||
result = {}
|
result = {'changed': False, 'commands': []}
|
||||||
written_file = ''
|
|
||||||
if module.check_mode and action != 'compare':
|
if not module.check_mode:
|
||||||
module.exit_json(changed=True, commands=action_results)
|
|
||||||
else:
|
|
||||||
if action == 'compare':
|
if action == 'compare':
|
||||||
written_file = write_on_file(action_results,
|
result['commands'] = []
|
||||||
module.params['comparison_results_file'],
|
|
||||||
module)
|
|
||||||
result['updates'] = []
|
|
||||||
else:
|
else:
|
||||||
if action_results:
|
if action_results:
|
||||||
load_config(module, action_results)
|
load_config(module, action_results)
|
||||||
changed = True
|
result['commands'] = action_results
|
||||||
final_snapshots = invoke('get_existing', module)
|
result['changed'] = True
|
||||||
result['updates'] = action_results
|
|
||||||
|
|
||||||
if (action == 'create' and
|
|
||||||
module.params['save_snapshot_locally']):
|
|
||||||
snapshot = get_snapshot(module)
|
|
||||||
written_file = write_on_file(snapshot,
|
|
||||||
module.params['snapshot_name'], module)
|
|
||||||
|
|
||||||
result['changed'] = changed
|
|
||||||
if module._verbosity > 0:
|
|
||||||
end_state = invoke('get_existing', module)
|
|
||||||
result['final_snapshots'] = final_snapshots
|
|
||||||
result['existing_snapshots'] = existing_snapshots
|
|
||||||
if written_file:
|
|
||||||
result['report_file'] = written_file
|
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue