├── LICENSE ├── README.md ├── defaults └── main.yml ├── handlers └── main.yml ├── meta └── main.yml ├── tasks ├── asa-backup.yml ├── ios-backup.yml ├── iosxr-backup.yml ├── main.yml └── nxos-backup.yml ├── tests ├── inventory └── test.yml └── vars └── main.yml /LICENSE: -------------------------------------------------------------------------------- 1 | CISCO SAMPLE CODE LICENSE 2 | Version 1.1 3 | Copyright (c) 2019 Cisco and/or its affiliates 4 | 5 | These terms govern this Cisco Systems, Inc. ("Cisco"), example or demo 6 | source code and its associated documentation (together, the "Sample 7 | Code"). By downloading, copying, modifying, compiling, or redistributing 8 | the Sample Code, you accept and agree to be bound by the following terms 9 | and conditions (the "License"). If you are accepting the License on 10 | behalf of an entity, you represent that you have the authority to do so 11 | (either you or the entity, "you"). Sample Code is not supported by Cisco 12 | TAC and is not tested for quality or performance. This is your only 13 | license to the Sample Code and all rights not expressly granted are 14 | reserved. 15 | 16 | 1. LICENSE GRANT: Subject to the terms and conditions of this License, 17 | Cisco hereby grants to you a perpetual, worldwide, non-exclusive, non- 18 | transferable, non-sublicensable, royalty-free license to copy and 19 | modify the Sample Code in source code form, and compile and 20 | redistribute the Sample Code in binary/object code or other executable 21 | forms, in whole or in part, solely for use with Cisco products and 22 | services. For interpreted languages like Java and Python, the 23 | executable form of the software may include source code and 24 | compilation is not required. 25 | 26 | 2. CONDITIONS: You shall not use the Sample Code independent of, or to 27 | replicate or compete with, a Cisco product or service. Cisco products 28 | and services are licensed under their own separate terms and you shall 29 | not use the Sample Code in any way that violates or is inconsistent 30 | with those terms (for more information, please visit: 31 | www.cisco.com/go/terms). 32 | 33 | 3. OWNERSHIP: Cisco retains sole and exclusive ownership of the Sample 34 | Code, including all intellectual property rights therein, except with 35 | respect to any third-party material that may be used in or by the 36 | Sample Code. Any such third-party material is licensed under its own 37 | separate terms (such as an open source license) and all use must be in 38 | full accordance with the applicable license. This License does not 39 | grant you permission to use any trade names, trademarks, service 40 | marks, or product names of Cisco. If you provide any feedback to Cisco 41 | regarding the Sample Code, you agree that Cisco, its partners, and its 42 | customers shall be free to use and incorporate such feedback into the 43 | Sample Code, and Cisco products and services, for any purpose, and 44 | without restriction, payment, or additional consideration of any kind. 45 | If you initiate or participate in any litigation against Cisco, its 46 | partners, or its customers (including cross-claims and counter-claims) 47 | alleging that the Sample Code and/or its use infringe any patent, 48 | copyright, or other intellectual property right, then all rights 49 | granted to you under this License shall terminate immediately without 50 | notice. 51 | 52 | 4. LIMITATION OF LIABILITY: CISCO SHALL HAVE NO LIABILITY IN CONNECTION 53 | WITH OR RELATING TO THIS LICENSE OR USE OF THE SAMPLE CODE, FOR 54 | DAMAGES OF ANY KIND, INCLUDING BUT NOT LIMITED TO DIRECT, INCIDENTAL, 55 | AND CONSEQUENTIAL DAMAGES, OR FOR ANY LOSS OF USE, DATA, INFORMATION, 56 | PROFITS, BUSINESS, OR GOODWILL, HOWEVER CAUSED, EVEN IF ADVISED OF THE 57 | POSSIBILITY OF SUCH DAMAGES. 58 | 59 | 5. DISCLAIMER OF WARRANTY: SAMPLE CODE IS INTENDED FOR EXAMPLE PURPOSES 60 | ONLY AND IS PROVIDED BY CISCO "AS IS" WITH ALL FAULTS AND WITHOUT 61 | WARRANTY OR SUPPORT OF ANY KIND. TO THE MAXIMUM EXTENT PERMITTED BY 62 | LAW, ALL EXPRESS AND IMPLIED CONDITIONS, REPRESENTATIONS, AND 63 | WARRANTIES INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTY OR 64 | CONDITION OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON- 65 | INFRINGEMENT, SATISFACTORY QUALITY, NON-INTERFERENCE, AND ACCURACY, 66 | ARE HEREBY EXCLUDED AND EXPRESSLY DISCLAIMED BY CISCO. CISCO DOES NOT 67 | WARRANT THAT THE SAMPLE CODE IS SUITABLE FOR PRODUCTION OR COMMERCIAL 68 | USE, WILL OPERATE PROPERLY, IS ACCURATE OR COMPLETE, OR IS WITHOUT 69 | ERROR OR DEFECT. 70 | 71 | 6. GENERAL: This License shall be governed by and interpreted in 72 | accordance with the laws of the State of California, excluding its 73 | conflict of laws provisions. You agree to comply with all applicable 74 | United States export laws, rules, and regulations. If any provision of 75 | this License is judged illegal, invalid, or otherwise unenforceable, 76 | that provision shall be severed and the rest of the License shall 77 | remain in full force and effect. No failure by Cisco to enforce any of 78 | its rights related to the Sample Code or to a breach of this License 79 | in a particular situation will act as a waiver of such rights. In the 80 | event of any inconsistencies with any other terms, this License shall 81 | take precedence. 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![published](https://static.production.devnetcloud.com/codeexchange/assets/images/devnet-published.svg)](https://developer.cisco.com/codeexchange/github/repo/ciscops/ansible-network-backup) 2 | 3 | # Ansible Network Backup 4 | 5 | This role backs up the configuration of a network device into a git repository. 6 | 7 | The file is backed up to the directory specified in `network_backup_dir`. A git repo is used when 8 | `network_backup_repository` is specified. 9 | 10 | 11 | ## Requirements 12 | 13 | 14 | Currently supports: 15 | * Cisco IOS XE 16 | * Cisco IOS XR 17 | * Cisco NX OS 18 | * Cisco ASA 19 | 20 | ## Role Variables 21 | 22 | 23 | * `network_backup_repository`: The git repository that will be use to store the backups 24 | * _Backups will not be pushed to git when `network_backup_repository` is not specified_ 25 | * `network_backup_dir`: The directory where the backups will be placed. Default: `"{{ playbook_dir }}/backups"` 26 | * _`network_backup_dir` will be created when it does not exist_ 27 | * `git_name`: Set the user's git name when not otherwise specified 28 | * `git_email`: Set the user's git email when not otherwise specified 29 | 30 | ## Dependencies 31 | 32 | None 33 | 34 | ## Cloning 35 | 36 | Adding without the `ansible-` prefix: 37 | 38 | `git clone git@github.com:ciscops/ansible-network-backup.git network-backup` 39 | 40 | Adding as a submodule: 41 | 42 | `git submodule add git@github.com:ciscops/ansible-network-backup.git network-backup` 43 | 44 | ## Example Playbook 45 | 46 | 47 | ```yaml 48 | - hosts: network 49 | gather_facts: no 50 | connection: network_cli 51 | tasks: 52 | - include_role: 53 | name: network-backup 54 | vars: 55 | network_backup_repository: 'git@github.com:repo/backups.git' 56 | ``` 57 | 58 | ## License 59 | 60 | 61 | [CISCO SAMPLE CODE LICENSE](./LICENSE). 62 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | network_backup_dir: "{{ playbook_dir }}/backups" 3 | network_backup_file: "{{ network_backup_dir }}/{{ inventory_hostname }}.cfg" -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # handlers file for net-backup-config -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | galaxy_info: 2 | author: your name 3 | description: your description 4 | company: your company (optional) 5 | 6 | # If the issue tracker for your role is not on github, uncomment the 7 | # next line and provide a value 8 | # issue_tracker_url: http://example.com/issue/tracker 9 | 10 | # Some suggested licenses: 11 | # - BSD (default) 12 | # - MIT 13 | # - GPLv2 14 | # - GPLv3 15 | # - Apache 16 | # - CC-BY 17 | license: license (GPLv2, CC-BY, etc) 18 | 19 | min_ansible_version: 1.2 20 | 21 | # If this a Container Enabled role, provide the minimum Ansible Container version. 22 | # min_ansible_container_version: 23 | 24 | # Optionally specify the branch Galaxy will use when accessing the GitHub 25 | # repo for this role. During role install, if no tags are available, 26 | # Galaxy will use this branch. During import Galaxy will access files on 27 | # this branch. If Travis integration is configured, only notifications for this 28 | # branch will be accepted. Otherwise, in all cases, the repo's default branch 29 | # (usually master) will be used. 30 | #github_branch: 31 | 32 | # 33 | # platforms is a list of platforms, and each platform has a name and a list of versions. 34 | # 35 | # platforms: 36 | # - name: Fedora 37 | # versions: 38 | # - all 39 | # - 25 40 | # - name: SomePlatform 41 | # versions: 42 | # - all 43 | # - 1.0 44 | # - 7 45 | # - 99.99 46 | 47 | galaxy_tags: [] 48 | # List tags for your role here, one per line. A tag is a keyword that describes 49 | # and categorizes the role. Users find roles by searching for tags. Be sure to 50 | # remove the '[]' above, if you add tags to this list. 51 | # 52 | # NOTE: A tag is limited to a single word comprised of alphanumeric characters. 53 | # Maximum 20 tags per role. 54 | 55 | dependencies: [] 56 | # List your role dependencies here, one per line. Be sure to remove the '[]' above, 57 | # if you add dependencies to this list. -------------------------------------------------------------------------------- /tasks/asa-backup.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Backup the config 3 | asa_config: 4 | backup: yes 5 | register: config_output 6 | 7 | - set_fact: 8 | temp_backup_file: "{{ config_output.backup_path }}" 9 | -------------------------------------------------------------------------------- /tasks/ios-backup.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Backup the config 3 | ios_config: 4 | backup: yes 5 | register: config_output 6 | 7 | - set_fact: 8 | temp_backup_file: "{{ config_output.backup_path }}" 9 | -------------------------------------------------------------------------------- /tasks/iosxr-backup.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Backup the config 3 | iosxr_config: 4 | backup: yes 5 | register: config_output 6 | 7 | - set_fact: 8 | temp_backup_file: "{{ config_output.backup_path }}" 9 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Clone the backup repo 3 | git: 4 | repo: "{{ network_backup_repository }}" 5 | dest: "{{ network_backup_dir }}" 6 | accept_hostkey: yes 7 | force: yes 8 | register: clone_result 9 | delegate_to: localhost 10 | run_once: true 11 | when: network_backup_repository is defined 12 | 13 | - include_tasks: "{{ role_path }}/tasks/{{ ansible_network_os }}-backup.yml" 14 | 15 | - name: Copy the temp to the destination 16 | copy: 17 | src: "{{ temp_backup_file }}" 18 | dest: "{{ network_backup_file }}" 19 | register: copy_result 20 | delegate_to: localhost 21 | 22 | - name: Delete the temp file 23 | file: 24 | path: "{{ temp_backup_file }}" 25 | state: absent 26 | changed_when: False 27 | delegate_to: localhost 28 | 29 | - block: 30 | - name: Add any new backups to the repossitory 31 | shell: "git add *.cfg" 32 | args: 33 | chdir: "{{ network_backup_dir }}" 34 | delegate_to: localhost 35 | changed_when: False 36 | run_once: true 37 | 38 | - name: Get the status of the repository 39 | shell: "git status" 40 | args: 41 | chdir: "{{ network_backup_dir }}" 42 | register: status_results 43 | changed_when: False 44 | ignore_errors: yes 45 | delegate_to: localhost 46 | run_once: true 47 | 48 | - name: Commit the changes 49 | shell: "git commit -am 'Changed'" 50 | args: 51 | chdir: "{{ network_backup_dir }}" 52 | when: status_results.stdout is search('Changes') 53 | delegate_to: localhost 54 | run_once: true 55 | environment: 56 | GIT_COMMITTER_NAME: "{{ git_name | default(omit) }}" 57 | GIT_COMMITTER_EMAIL: "{{ git_email | default(omit) }}" 58 | GIT_AUTHOR_NAME: "{{ git_name | default(omit) }}" 59 | GIT_AUTHOR_EMAIL: "{{ git_email | default(omit) }}" 60 | 61 | - name: Push the changes 62 | shell: "git push origin master" 63 | args: 64 | chdir: "{{ network_backup_dir }}" 65 | run_once: true 66 | when: status_results.stdout is search('Changes') 67 | delegate_to: localhost 68 | environment: 69 | GIT_COMMITTER_NAME: "{{ git_name | default(omit) }}" 70 | GIT_COMMITTER_EMAIL: "{{ git_email | default(omit) }}" 71 | GIT_AUTHOR_NAME: "{{ git_name | default(omit) }}" 72 | GIT_AUTHOR_EMAIL: "{{ git_email | default(omit) }}" 73 | when: network_backup_repository is defined 74 | -------------------------------------------------------------------------------- /tasks/nxos-backup.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Backup the config 3 | nxos_config: 4 | backup: yes 5 | register: config_output 6 | 7 | - set_fact: 8 | temp_backup_file: "{{ config_output.backup_path }}" 9 | 10 | - name: Delete the Time from the temp file 11 | replace: 12 | path: "{{ temp_backup_file }}" 13 | regexp: '^!Time.*$' 14 | changed_when: False 15 | delegate_to: localhost 16 | -------------------------------------------------------------------------------- /tests/inventory: -------------------------------------------------------------------------------- 1 | localhost 2 | 3 | -------------------------------------------------------------------------------- /tests/test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: localhost 3 | remote_user: root 4 | roles: 5 | - net-backup-config -------------------------------------------------------------------------------- /vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # vars file for net-backup-config --------------------------------------------------------------------------------