| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
|
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
|
| 24 |
DOCUMENTATION = """ |
| 25 |
module: nxos_rollback |
| 26 |
extends_documentation_fragment: |
| 27 |
- cisco.nxos.nxos |
| 28 |
short_description: Set a checkpoint or rollback to a checkpoint. |
| 29 |
description: |
| 30 |
- This module offers the ability to set a configuration checkpoint file or rollback |
| 31 |
to a configuration checkpoint file on Cisco NXOS switches. |
| 32 |
version_added: 1.0.0 |
| 33 |
author: |
| 34 |
- Jason Edelman (@jedelman8) |
| 35 |
- Gabriele Gerbino (@GGabriele) |
| 36 |
notes: |
| 37 |
- Tested against NXOSv 7.3.(0)D1(1) on VIRL |
| 38 |
- Unsupported for Cisco MDS |
| 39 |
- Sometimes C(transport=nxapi) may cause a timeout error. |
| 40 |
options: |
| 41 |
checkpoint_file: |
| 42 |
description: |
| 43 |
- Name of checkpoint file to create. Mutually exclusive with rollback_to. |
| 44 |
type: str |
| 45 |
rollback_to: |
| 46 |
description: |
| 47 |
- Name of checkpoint file to rollback to. Mutually exclusive with checkpoint_file. |
| 48 |
type: str |
| 49 |
""" |
| 50 |
|
| 51 |
EXAMPLES = """ |
| 52 |
- cisco.nxos.nxos_rollback: |
| 53 |
checkpoint_file: backup.cfg |
| 54 |
username: '{{ un }}' |
| 55 |
password: '{{ pwd }}' |
| 56 |
host: '{{ inventory_hostname }}' |
| 57 |
- cisco.nxos.nxos_rollback: |
| 58 |
rollback_to: backup.cfg |
| 59 |
username: '{{ un }}' |
| 60 |
password: '{{ pwd }}' |
| 61 |
host: '{{ inventory_hostname }}' |
| 62 |
""" |
| 63 |
|
| 64 |
RETURN = """ |
| 65 |
filename: |
| 66 |
description: The filename of the checkpoint/rollback file. |
| 67 |
returned: success |
| 68 |
type: str |
| 69 |
sample: 'backup.cfg' |
| 70 |
status: |
| 71 |
description: Which operation took place and whether it was successful. |
| 72 |
returned: success |
| 73 |
type: str |
| 74 |
sample: 'rollback executed' |
| 75 |
""" |
| 76 |
|
| 77 |
|
| 78 |
from ansible.module_utils.basic import AnsibleModule |
| 79 |
|
| 80 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import run_commands |
| 81 |
|
| 82 |
|
| 83 |
def checkpoint(filename, module): |
| 84 |
commands = [ |
| 85 |
{"command": "terminal dont-ask", "output": "text"}, |
| 86 |
{"command": "checkpoint file %s" % filename, "output": "text"}, |
| 87 |
] |
| 88 |
run_commands(module, commands) |
| 89 |
|
| 90 |
|
| 91 |
def rollback(filename, module): |
| 92 |
commands = [ |
| 93 |
{ |
| 94 |
"command": "rollback running-config file %s" % filename, |
| 95 |
"output": "text", |
| 96 |
}, |
| 97 |
] |
| 98 |
run_commands(module, commands) |
| 99 |
|
| 100 |
|
| 101 |
def main(): |
| 102 |
argument_spec = dict(checkpoint_file=dict(required=False), rollback_to=dict(required=False)) |
| 103 |
|
| 104 |
module = AnsibleModule( |
| 105 |
argument_spec=argument_spec, |
| 106 |
mutually_exclusive=[["checkpoint_file", "rollback_to"]], |
| 107 |
supports_check_mode=False, |
| 108 |
) |
| 109 |
|
| 110 |
checkpoint_file = module.params["checkpoint_file"] |
| 111 |
rollback_to = module.params["rollback_to"] |
| 112 |
|
| 113 |
status = None |
| 114 |
filename = None |
| 115 |
changed = False |
| 116 |
|
| 117 |
if checkpoint_file: |
| 118 |
checkpoint(checkpoint_file, module) |
| 119 |
status = "checkpoint file created" |
| 120 |
elif rollback_to: |
| 121 |
rollback(rollback_to, module) |
| 122 |
status = "rollback executed" |
| 123 |
changed = True |
| 124 |
filename = rollback_to or checkpoint_file |
| 125 |
|
| 126 |
module.exit_json(changed=changed, status=status, filename=filename) |
| 127 |
|
| 128 |
|
| 129 |
if __name__ == "__main__": |
| 130 |
main() |