Created
April 30, 2020 17:20
-
-
Save zoredache/10c8388cab2a02853a6816f0b90c2706 to your computer and use it in GitHub Desktop.
ansible switch_backup.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: switches | |
gather_facts: no | |
vars: | |
track_changes: yes | |
data_dir: "data/switches" | |
tasks: | |
- name: create data directory if needed | |
file: | |
path: "{{ item }}" | |
state: directory | |
delegate_to: localhost | |
run_once: yes | |
loop: | |
- "{{ data_dir }}" | |
- name: Create git repo w/etckeeper | |
command: "etckeeper init -d {{ data_dir }}" | |
args: | |
creates: "{{ data_dir }}/.git" | |
when: track_changes|default(false) | |
delegate_to: localhost | |
run_once: yes | |
- name: Git pull changes | |
changed_when: false | |
command: git pull --rebase --autostash origin | |
args: | |
chdir: "{{ data_dir }}" | |
delegate_to: localhost | |
run_once: yes | |
- name: Etckeeper Saving changes | |
changed_when: false | |
shell: | | |
set -euo pipefail | |
etckeeper unclean -d {{ data_dir }} || exit 0 | |
etckeeper commit -d {{ data_dir }} "saving uncommitted changes prior to ansible run" | |
args: | |
chdir: "{{ data_dir }}" | |
executable: /bin/bash | |
when: track_changes|default(false) | |
delegate_to: localhost | |
run_once: yes | |
- name: run commands to gather information | |
aruba_command: | |
commands: | |
- show system | |
- show config | |
- show running-config | |
register: switch_config | |
- name: save collected infromation in data directory | |
copy: | |
content: "{{ switch_config.stdout[item.index] }}" | |
dest: "{{ item.filename }}" | |
loop_control: | |
label: "{{ item.name }}" | |
loop: | |
- name: show system | |
index: 0 | |
filename: "{{ data_dir }}/{{ inventory_hostname }}_system" | |
- name: show config | |
index: 1 | |
filename: "{{ data_dir }}/{{ inventory_hostname }}_config" | |
- name: show running-config | |
index: 2 | |
filename: "{{ data_dir }}/{{ inventory_hostname }}_running_config" | |
- name: Etckeeper Saving changes | |
changed_when: false | |
shell: | | |
set -euo pipefail | |
etckeeper unclean -d {{ data_dir }} || exit 0 | |
etckeeper commit -d {{ data_dir }} "saving uncommitted changes after ansible run" | |
args: | |
chdir: "{{ data_dir }}" | |
executable: /bin/bash | |
when: track_changes|default(false) | |
delegate_to: localhost | |
run_once: yes | |
- name: Git push changes | |
changed_when: false | |
command: git push origin | |
args: | |
chdir: "{{ data_dir }}" | |
delegate_to: localhost | |
run_once: yes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment