|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# (c) 2018, Ansible by Red Hat, inc |
| 5 | +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 | + |
| 8 | +from __future__ import absolute_import, division, print_function |
| 9 | + |
| 10 | + |
| 11 | +__metaclass__ = type |
| 12 | + |
| 13 | + |
| 14 | +DOCUMENTATION = """ |
| 15 | +module: cli_backup |
| 16 | +author: Kate Case (@Qalthos) |
| 17 | +short_description: Back up device configuration from network devices over network_cli |
| 18 | +description: |
| 19 | +- This module provides platform agnostic way of backing up text based configuration from |
| 20 | + network devices over network_cli connection plugin. |
| 21 | +version_added: 4.2.0 |
| 22 | +extends_documentation_fragment: |
| 23 | +- ansible.netcommon.network_agnostic |
| 24 | +options: |
| 25 | + defaults: |
| 26 | + description: |
| 27 | + - The I(defaults) argument will influence how the running-config is collected |
| 28 | + from the device. When the value is set to true, the command used to collect |
| 29 | + the running-config is append with the all keyword. When the value is set to |
| 30 | + false, the command is issued without the all keyword. |
| 31 | + default: no |
| 32 | + type: bool |
| 33 | + filename: |
| 34 | + description: |
| 35 | + - The filename to be used to store the backup configuration. If the filename |
| 36 | + is not given it will be generated based on the hostname, current time and |
| 37 | + date in format defined by <hostname>_config.<current-date>@<current-time> |
| 38 | + type: str |
| 39 | + dir_path: |
| 40 | + description: |
| 41 | + - This option provides the path ending with directory name in which the backup |
| 42 | + configuration file will be stored. If the directory does not exist it will |
| 43 | + be first created and the filename is either the value of C(filename) or |
| 44 | + default filename as described in C(filename) options description. If the |
| 45 | + path value is not given in that case a I(backup) directory will be created |
| 46 | + in the current working directory and backup configuration will be copied |
| 47 | + in C(filename) within I(backup) directory. |
| 48 | + type: path |
| 49 | +""" |
| 50 | + |
| 51 | +EXAMPLES = """ |
| 52 | +- name: configurable backup path |
| 53 | + ansible.netcommon.cli_backup: |
| 54 | + filename: backup.cfg |
| 55 | + dir_path: /home/user |
| 56 | +""" |
| 57 | + |
| 58 | +RETURN = """ |
| 59 | +backup_path: |
| 60 | + description: The full path to the backup file |
| 61 | + returned: always |
| 62 | + type: str |
| 63 | + sample: /playbooks/ansible/backup/hostname_config.2016-07-16@22:28:34 |
| 64 | +""" |
| 65 | + |
| 66 | +from ansible.module_utils.basic import AnsibleModule |
| 67 | +from ansible.module_utils.connection import Connection |
| 68 | + |
| 69 | + |
| 70 | +def validate_args(module, device_operations): |
| 71 | + """validate param if it is supported on the platform""" |
| 72 | + feature_list = [ |
| 73 | + "defaults", |
| 74 | + ] |
| 75 | + |
| 76 | + for feature in feature_list: |
| 77 | + if module.params[feature]: |
| 78 | + supports_feature = device_operations.get("supports_%s" % feature) |
| 79 | + if supports_feature is None: |
| 80 | + module.fail_json( |
| 81 | + msg="This platform does not specify whether %s is supported or not. " |
| 82 | + "Please report an issue against this platform's cliconf plugin." % feature |
| 83 | + ) |
| 84 | + elif not supports_feature: |
| 85 | + module.fail_json(msg="Option %s is not supported on this platform" % feature) |
| 86 | + |
| 87 | + |
| 88 | +def main(): |
| 89 | + """main entry point for execution""" |
| 90 | + argument_spec = dict( |
| 91 | + defaults=dict(default=False, type="bool"), |
| 92 | + filename=dict(), |
| 93 | + dir_path=dict(type="path"), |
| 94 | + ) |
| 95 | + |
| 96 | + module = AnsibleModule( |
| 97 | + argument_spec=argument_spec, |
| 98 | + ) |
| 99 | + |
| 100 | + result = {"changed": False} |
| 101 | + |
| 102 | + connection = Connection(module._socket_path) |
| 103 | + capabilities = module.from_json(connection.get_capabilities()) |
| 104 | + |
| 105 | + if capabilities: |
| 106 | + device_operations = capabilities.get("device_operations", dict()) |
| 107 | + validate_args(module, device_operations) |
| 108 | + else: |
| 109 | + device_operations = dict() |
| 110 | + |
| 111 | + if module.params["defaults"]: |
| 112 | + if "get_default_flag" in capabilities.get("rpc"): |
| 113 | + flags = connection.get_default_flag() |
| 114 | + else: |
| 115 | + flags = "all" |
| 116 | + else: |
| 117 | + flags = [] |
| 118 | + |
| 119 | + running = connection.get_config(flags=flags) |
| 120 | + result["__backup__"] = running |
| 121 | + |
| 122 | + module.exit_json(**result) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + main() |
0 commit comments