-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.yml
120 lines (103 loc) · 3.8 KB
/
backup.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
- name: Backup NetScaler configuration
hosts: netscalers
connection: local
tasks:
##### SAVE RUNNING CONFIGURATION
- name: Save running configuration
uri:
url: "http://{{ansible_host}}/nitro/v1/config/nsconfig?action=save"
method: POST
status_code: 200
headers:
X-NITRO-USER: "{{ns.user}}"
X-NITRO-PASS: "{{ns.pass}}"
body_format: json
body:
nsconfig: {}
##### CONSTRUCT BACKUP FILENAME FROM DATE OF LOCALHOST
- name: Get time from localhost
connection: local
run_once: True
setup:
- name: Construct backup filename - step 1
set_fact:
bkp_date: "{{ansible_facts.date_time.year}}{{ansible_facts.date_time.month}}{{ansible_facts.date_time.day}}"
bkp_time: "{{ansible_facts.date_time.hour}}{{ansible_facts.date_time.minute}}{{ansible_facts.date_time.second}}"
- name: Construct backup filename - step 2
set_fact:
bkp_filename: "{{bkp_date}}_{{bkp_time}}_{{inventory_hostname}}_full.auto"
##### CREATE & CHECK BACKUP
- name: Create backup
uri:
url: "http://{{ansible_host}}/nitro/v1/config/systembackup?action=create"
method: POST
status_code: 200
return_content: yes
headers:
X-NITRO-USER: "{{ ns.user }}"
X-NITRO-PASS: "{{ ns.pass }}"
body_format: json
body:
systembackup:
filename: "{{bkp_filename}}"
level: "full"
- name: Check that backup file exists on NS
register: result
uri:
url: "http://{{ansible_host}}/nitro/v1/config/systembackup/{{bkp_filename}}.tgz"
method: GET
status_code: 200
return_content: yes
headers:
X-NITRO-USER: "{{ ns.user }}"
X-NITRO-PASS: "{{ ns.pass }}"
- name: Get backup file size on NS
set_fact:
bkp_filesize: "{{result.json.systembackup[0].size}}"
- name: Check that backup file size is ok (>{{bkp.filesize_min}}KB)
assert:
that: "{{bkp_filesize}} > {{bkp.filesize_min}}"
- name: Copy backup file from NS to SCP server
connection: ssh_citrix_adc
remote_user: "{{ns.user}}"
vars:
ansible_python_interpreter: /var/python/bin/python
shell: "scp -i {{ns.privkey_path}} {{bkp.ns_path}}/{{bkp_filename}}.tgz {{bkp.user}}@{{bkp.server}}:{{bkp.server_path}}"
- name: Check copied filesize on SCP server
register: result
delegate_to: "{{bkp.server}}"
run_once: True
remote_user: "{{bkp.user}}"
stat:
path: "{{bkp.server_path}}/{{bkp_filename}}.tgz"
- name: Check that copied file size is ok (={{bkp_filesize}}KB)
assert:
that: "{{(result.stat.size / 1024) | round}} == {{bkp_filesize}}"
##### DELETE OLD BACKUP FILES
- name: Get list of existing backup files
register: result
uri:
url: "http://{{ansible_host}}/nitro/v1/config/systembackup/"
method: GET
status_code: 200
return_content: yes
headers:
X-NITRO-USER: "{{ ns.user }}"
X-NITRO-PASS: "{{ ns.pass }}"
- name: Count existing backup files
set_fact:
bkp_files: "{{result.json.systembackup | length}}"
- name: Delete old backup files (only automatically generated which end in '.auto.tgz')
loop: "{{range(0,(bkp_files|int) - (bkp.files_max|int)) | list}}"
when:
- (bkp_files|int) > (bkp.files_max|int)
- result.json.systembackup[item].filename is search(".auto.tgz")
uri:
url: "http://{{ansible_host}}/nitro/v1/config/systembackup/{{result.json.systembackup[item].filename}}"
method: DELETE
status_code: 200
return_content: yes
headers:
X-NITRO-USER: "{{ ns.user }}"
X-NITRO-PASS: "{{ ns.pass }}"