-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcfgback.yml
95 lines (80 loc) · 3.51 KB
/
cfgback.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Playbook to backup running configs from network devices to Git
#
# Gather the config via ansible facts, store it in the local directory {{ config_path }}
# Sanitize config files / remove lines with timestamps in the case od Cisco NX-OS devices
# Commit to git, only if neccessary
# Report all steps to {{ syslog_host }}
#
# You might want to change the vars config_path, syslog_host
# Uncomment the line 'git push -f origin master' if you want to push to a central repo like GitHub/Lab
---
- name: CONFIG BACKUP TO GIT ENABLED DIRECTORY
hosts: all
connection: network_cli
ignore_errors: yes
gather_facts: yes
vars:
config_path: ~/configs
syslog_host: 192.168.1.12
tasks:
- name: SYSLOG MESSAGE - GATHER FACTS ERROR
shell: |
logger -n {{ syslog_host }} -p local0.error --udp Config Backup of "{{ inventory_hostname }} failed, because config could not be read"
when: ansible_facts.net_config is not defined
- name: SAVE CONFIG TO FILE
local_action: copy content={{ ansible_net_config }} dest={{ config_path }}/{{ inventory_hostname }}.txt
register: save_result
when: ansible_facts.net_config is defined
- name: SYSLOG MESSAGE - SAVE CONFIG TO FILE ERROR
shell: |
logger -n {{ syslog_host }} -p local0.error --udp Config Backup of "{{ inventory_hostname }}" SAVE FAILED because "{{ save_result.msg }}"
when: save_result.failed is defined and save_result.failed
- name: SYSLOG MESSAGE - SAVE CONFIG SUCCESSFUL
shell: |
logger -n {{ syslog_host }} -p local0.notice --udp Config Backup of "{{ inventory_hostname }}" successful
when: ansible_facts.net_config is defined and not save_result.failed
- name: REMOVE CISCO NXOS CONFIG LINE WITH TIMESTAMP
lineinfile:
path: "{{ config_path }}/{{ inventory_hostname }}.txt"
state: absent
regexp: '^!Time'
delegate_to: localhost
when: ansible_network_os == 'nxos'
#
# The following tasks will only be executed once
#
- name: GIT - ADD ALL (NEW) FILES AND CHECK IF WORKING DIRECTORY IS DIRTY => FAILED
shell: |
cd {{ config_path }}/
git add --all .
git diff-index --quiet HEAD #Return code = 0 if working directory is clean and 1 if dirty
delegate_to: localhost
register: git_result
run_once: true
- name: SYSLOG MESSAGE - Git Dir clean - Nothing to commit
shell: |
logger -n {{ syslog_host }} -p local0.notice --udp Config Backup - GIT Working Directory {{ config_path }} is clean - Nothing to commit
when: not git_result.failed
delegate_to: localhost
run_once: true
- name: GIT - COMMIT/PUSH ONLY IF WORKING DIRECTORY IS DIRTY
shell: |
cd {{ config_path }}/
git commit -m "Config backup taken $(date +"%Y-%m-%d %H:%M:%S")"
# git push -f origin master
delegate_to: localhost
register: gitcommit_result
run_once: true
when: git_result.failed
- name: SYSLOG MESSAGE - GIT COMMIT/PUSH SUCCESSFUL
shell: |
logger -n {{ syslog_host }} -p local0.notice --udp Config Backup - GIT Commit / Push successful
when: gitcommit_result.failed is defined and not gitcommit_result.failed
delegate_to: localhost
run_once: true
- name: SYSLOG MESSAGE - GIT COMMIT/PUSH ERROR
shell: |
logger -n {{ syslog_host }} -p local0.error --udp Config Backup - GIT Commit / Push FAILED !
when: gitcommit_result.failed is defined and gitcommit_result.failed
delegate_to: localhost
run_once: true