From aef4e3d8fac0a6ab916a4b53ef797c861335b491 Mon Sep 17 00:00:00 2001 From: Trishna Guha Date: Fri, 11 Aug 2017 21:46:37 +0530 Subject: [PATCH] fix nxos_snapshot (#27993) Signed-off-by: Trishna Guha --- .../modules/network/nxos/nxos_snapshot.py | 99 +++---------------- 1 file changed, 15 insertions(+), 84 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_snapshot.py b/lib/ansible/modules/network/nxos/nxos_snapshot.py index 157bc56b2f1..758497886cf 100644 --- a/lib/ansible/modules/network/nxos/nxos_snapshot.py +++ b/lib/ansible/modules/network/nxos/nxos_snapshot.py @@ -131,24 +131,15 @@ EXAMPLES = ''' description: Done with Ansible save_snapshot_locally: true path: /home/user/snapshots/ - host: "{{ inventory_hostname }}" - username: "{{ un }}" - password: "{{ pwd }}" # Delete a snapshot - nxos_snapshot: action: delete snapshot_name: test_snapshot - host: "{{ inventory_hostname }}" - username: "{{ un }}" - password: "{{ pwd }}" # Delete all existing snapshots - nxos_snapshot: action: delete_all - host: "{{ inventory_hostname }}" - username: "{{ un }}" - password: "{{ pwd }}" # Add a show command for snapshots creation - nxos_snapshot: @@ -156,9 +147,6 @@ EXAMPLES = ''' show_command: show ip interface brief row_id: ROW_intf element_key1: intf-name - host: "{{ inventory_hostname }}" - username: "{{ un }}" - password: "{{ pwd }}" # Compare two snapshots - nxos_snapshot: @@ -168,47 +156,16 @@ EXAMPLES = ''' comparison_results_file: compare_snapshots.txt compare_option: summary path: '../snapshot_reports/' - host: "{{ inventory_hostname }}" - username: "{{ un }}" - password: "{{ pwd }}" ''' RETURN = ''' -existing_snapshots: - 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: +commands: description: commands sent to the device returned: verbose mode type: list 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 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 -def execute_show_command(command, module, command_type='cli_show_ascii'): - cmds = [command] - if module.params['transport'] == 'cli': - body = run_commands(module, cmds) - elif module.params['transport'] == 'nxapi': - body = run_commands(module, cmds) +def execute_show_command(command, module): + command = [{ + 'command': command, + 'output': 'text', + }] - return body + return run_commands(module, command) def get_existing(module): @@ -433,45 +389,20 @@ def main(): module.fail_json(msg='snapshot_name is required when action=delete') existing_snapshots = invoke('get_existing', module) - final_snapshots = existing_snapshots - changed = False - action_results = invoke('action_%s' % action, module, existing_snapshots) - result = {} - written_file = '' - if module.check_mode and action != 'compare': - module.exit_json(changed=True, commands=action_results) - else: + result = {'changed': False, 'commands': []} + + if not module.check_mode: if action == 'compare': - written_file = write_on_file(action_results, - module.params['comparison_results_file'], - module) - result['updates'] = [] + result['commands'] = [] else: if action_results: load_config(module, action_results) - changed = True - final_snapshots = invoke('get_existing', module) - 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 + result['commands'] = action_results + result['changed'] = True module.exit_json(**result) - if __name__ == '__main__': - main() - + main()