Debian Codegraph @

ansible_collections/cisco/mso/tests/integration/targets/mso_backup/tasks/main.yml

raw
1 # Test code for the MSO modules
2 # Copyright: (c) 2020, Shreyas Srish (@shrsr) <[email protected]>
3 # Copyright: (c) 2023, Sabari Jaganathan (@sajagana) <[email protected]>
4
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 - name: Test that we have an ACI MultiSite host, username and password
8 ansible.builtin.fail:
9 msg: 'Please define the following variables: mso_hostname, mso_username and mso_password.'
10 when: mso_hostname is not defined or mso_username is not defined or mso_password is not defined
11
12 # CLEAN ENVIRONMENT
13 - name: Set vars
14 ansible.builtin.set_fact:
15 mso_info: &mso_info
16 host: '{{ mso_hostname }}'
17 username: '{{ mso_username }}'
18 password: '{{ mso_password }}'
19 validate_certs: '{{ mso_validate_certs | default(false) }}'
20 use_ssl: '{{ mso_use_ssl | default(true) }}'
21 use_proxy: '{{ mso_use_proxy | default(true) }}'
22 output_level: '{{ mso_output_level | default("debug") }}'
23
24 - name: Query MSO version
25 cisco.mso.mso_version:
26 <<: *mso_info
27 state: query
28 register: version
29
30 - name: Run tests to restore a clean backup as initial task for CI
31 include_tasks: restore_backup.yml
32
33 - name: Execute tasks only for MSO version < 5.0
34 when: version.current.version is version('5.0', '<')
35 block:
36
37 - name: Clear tenant
38 cisco.mso.mso_tenant: &tenant1_absent
39 <<: *mso_info
40 tenant: Tenant1
41 display_name: Test_Tenant
42 state: absent
43
44 - name: Ensure remote location is absent
45 cisco.mso.mso_remote_location: &remote_location_absent
46 <<: *mso_info
47 remote_location: ansible_test
48 remote_protocol: scp
49 remote_host: '{{ mso_remote_location }}'
50 remote_path: '{{ mso_remote_location_path | default("/tmp") }}'
51 authentication_type: password
52 remote_username: '{{ mso_remote_location_user | default(mso_username) }}'
53 remote_password: '{{ mso_remote_location_password | default(mso_password) }}'
54 state: absent
55 ignore_errors: true
56
57 - name: Ensure remote location is present
58 cisco.mso.mso_remote_location:
59 <<: *remote_location_absent
60 state: present
61
62 - name: Query all backups
63 cisco.mso.mso_backup: &query_all_backups
64 <<: *mso_info
65 state: query
66 register: query_ansibleBackup_for_delete
67
68 - name: Remove all backups
69 cisco.mso.mso_backup:
70 <<: *mso_info
71 backup_id: '{{ item.id }}'
72 state: absent
73 loop: '{{ query_ansibleBackup_for_delete.current | list | sort(attribute="name", reverse=True) }}'
74
75 # Create local backups - Only for version < 3.2
76 - name: Execute tasks only for MSO version < 3.2
77 when: version.current.version is version('3.2', '<')
78 block:
79 - name: Create local ansibleBackup1 in check mode
80 cisco.mso.mso_backup:
81 <<: *mso_info
82 backup: ansibleBackup1
83 description: via Ansible
84 location_type: local
85 state: present
86 register: cm_add_ansibleBackup1
87 check_mode: true
88
89 - name: Verify local cm_add_ansibleBackup1
90 ansible.builtin.assert:
91 that:
92 - cm_add_ansibleBackup1 is changed
93
94 - name: Create local ansibleBackup1 in normal mode
95 cisco.mso.mso_backup:
96 <<: *mso_info
97 backup: ansibleBackup1
98 description: via Ansible
99 location_type: local
100 state: present
101 register: nm_add_ansibleBackup1
102
103 - name: Query ansibleBackup1 to check if it exists
104 cisco.mso.mso_backup:
105 <<: *mso_info
106 backup: ansibleBackup1
107 state: query
108 register: check_ansibleBackup1
109 until:
110 - check_ansibleBackup1.current is defined
111 - check_ansibleBackup1.current[0] is defined
112 - check_ansibleBackup1.current[0].status.statusType == "success"
113 retries: 50
114 delay: 10
115
116 - name: Verify nm_add_ansibleBackup1
117 ansible.builtin.assert:
118 that:
119 - nm_add_ansibleBackup1 is changed
120 - check_ansibleBackup1.current[0].backupEntry.metadata.name is match("ansibleBackup1_[0-9a-zA-Z]*")
121
122 - name: Create local ansibleBackup3 in normal mode
123 cisco.mso.mso_backup:
124 <<: *mso_info
125 backup: ansibleBackup3
126 description: via Ansible
127 location_type: local
128 state: present
129 register: nm_add_ansibleBackup3
130
131 - name: Query ansibleBackup3 to check if it exists
132 cisco.mso.mso_backup:
133 <<: *mso_info
134 backup: ansibleBackup3
135 state: query
136 register: query_ansibleBackup3
137 until:
138 - query_ansibleBackup3.current is defined
139 - query_ansibleBackup3.current[0] is defined
140 - query_ansibleBackup3.current[0].status.statusType == "success"
141 - query_ansibleBackup3.current[0].backupEntry.metadata.name is match ("ansibleBackup3_[0-9a-zA-Z]*")
142 retries: 50
143 delay: 10
144
145 - name: Assertions check for create local ansibleBackup3 in normal mode
146 ansible.builtin.assert:
147 that:
148 - query_ansibleBackup3 is not changed
149 - query_ansibleBackup3.current.0.status.statusType == "success"
150 - query_ansibleBackup3.current.0.backupEntry.metadata.name is match ("ansibleBackup3_[0-9a-zA-Z]*")
151 - query_ansibleBackup3.current.0.location.locationType == "local"
152
153 # Creating duplicate backups with the name of ansibleBackup3 - to validate "Multiple backups with same name found" error message
154 - name: Create local ansibleBackup3 in normal mode again
155 cisco.mso.mso_backup:
156 <<: *mso_info
157 backup: ansibleBackup3
158 description: via Ansible
159 location_type: local
160 state: present
161 register: nm_add_ansibleBackup3_again
162
163 - name: Query duplicate ansibleBackup3 to check if it exists
164 cisco.mso.mso_backup:
165 <<: *mso_info
166 backup: ansibleBackup3
167 state: query
168 register: query_ansibleBackup3_again
169 until:
170 - query_ansibleBackup3_again.current is defined
171 - query_ansibleBackup3_again.current[1] is defined
172 - query_ansibleBackup3_again.current[1].status.statusType == "success"
173 - query_ansibleBackup3_again.current[1].backupEntry.metadata.name is match ("ansibleBackup3_[0-9a-zA-Z]*")
174 retries: 50
175 delay: 10
176
177 - name: Assertions check for create local ansibleBackup3 in normal mode again
178 ansible.builtin.assert:
179 that:
180 - query_ansibleBackup3_again is not changed
181 - query_ansibleBackup3_again.current.1.status.statusType == "success"
182 - query_ansibleBackup3_again.current.1.backupEntry.metadata.name is match ("ansibleBackup3_[0-9a-zA-Z]*")
183 - query_ansibleBackup3_again.current.1.location.locationType == "local"
184
185 # Move Local backup to Remote - Only for version < 3.2
186 - name: Execute tasks only for MSO version < 3.2
187 when: version.current.version is version('3.2', '<')
188 block:
189 - name: Move ansibleBackup1 from local to remote location in check mode
190 cisco.mso.mso_backup: &move_ab1_cm
191 <<: *mso_info
192 remote_location: ansible_test
193 remote_path: ""
194 backup: ansibleBackup1
195 description: Local to Remote via Ansible
196 state: move
197 check_mode: true
198 register: move_ab1_cm
199
200 - name: Move ansibleBackup1 from local to remote location in normal mode
201 cisco.mso.mso_backup:
202 <<: *move_ab1_cm
203 register: move_ab1_nm
204
205 - name: Move a non existent backup from local location to remote
206 cisco.mso.mso_backup:
207 <<: *mso_info
208 backup: non_existent_backup
209 remote_location: ansible_test
210 remote_path: ""
211 state: move
212 register: move_non_existent_backup
213 ignore_errors: true
214
215 - name: Move a ansibleBackup3 from local location to remote - check mode
216 cisco.mso.mso_backup:
217 <<: *mso_info
218 backup: ansibleBackup3
219 remote_location: ansible_test
220 remote_path: ""
221 state: move
222 register: move_backup_cm_ab3
223 ignore_errors: true
224
225 - name: Move a ansibleBackup3 from local location to remote - normal mode
226 cisco.mso.mso_backup:
227 <<: *mso_info
228 backup: ansibleBackup3
229 remote_location: ansible_test
230 remote_path: ""
231 state: move
232 register: move_backup_nm_ab3
233 ignore_errors: true
234
235 - name: Set regex match statement for move backup assertion
236 ansible.builtin.set_fact:
237 error_regex_match_move_backup: "Multiple backups with same name found. Existing backups with similar names: ansibleBackup3_[0-9]*, ansibleBackup3_[0-9]*"
238
239 - name: Assertions check for move a backup from local location to remote location
240 ansible.builtin.assert:
241 that:
242 - move_ab1_cm is changed
243 - move_ab1_nm is changed
244 - move_non_existent_backup.msg is match("Backup 'non_existent_backup' does not exist")
245 - move_backup_cm_ab3.msg is match(error_regex_match_move_backup)
246 - move_backup_nm_ab3.msg is match(error_regex_match_move_backup)
247
248 # Create Remote backup - For all version
249 - name: Create ansibleBackupRemote1 in check mode
250 cisco.mso.mso_backup:
251 <<: *mso_info
252 backup: ansibleBackupRemote1
253 description: Remote via Ansible
254 location_type: remote
255 remote_location: ansible_test
256 remote_path: "tmp"
257 state: present
258 check_mode: true
259 register: cm_add_ansibleBackupRemote1
260
261 - name: Create ansibleBackupRemote1 in normal mode
262 cisco.mso.mso_backup:
263 <<: *mso_info
264 backup: ansibleBackupRemote1
265 description: Remote via Ansible
266 location_type: remote
267 remote_location: ansible_test
268 remote_path: "tmp"
269 state: present
270 register: nm_add_ansibleBackupRemote1
271
272 - name: Query ansibleBackupRemote1 to check if it exists
273 cisco.mso.mso_backup:
274 <<: *mso_info
275 backup: ansibleBackupRemote1
276 state: query
277 register: query_ansibleBackupRemote1
278 until:
279 - query_ansibleBackupRemote1.current is defined
280 - query_ansibleBackupRemote1.current[0] is defined
281 - query_ansibleBackupRemote1.current[0].status.statusType == "success"
282 retries: 50
283 delay: 10
284
285 - name: Assertions check for the backups present on the remote location
286 ansible.builtin.assert:
287 that:
288 - cm_add_ansibleBackupRemote1 is changed
289 - nm_add_ansibleBackupRemote1 is changed
290 - query_ansibleBackupRemote1.current.0.backupEntry.metadata.name is match ("ansibleBackupRemote1_[0-9a-zA-Z]*")
291
292 # Remove additional backups - to handle "Multiple backups with same name" during the download
293 - name: Query all backups to handle "Multiple backups with same name" during the download
294 cisco.mso.mso_backup:
295 <<: *mso_info
296 state: query
297 register: query_ansibleBackup_for_delete
298
299 - name: Delete all backups except for one to handle "Multiple backups with same name" during the download
300 cisco.mso.mso_backup:
301 <<: *mso_info
302 backup_id: '{{ item.id }}'
303 state: absent
304 loop: '{{ query_ansibleBackup_for_delete.current[0:-1] | list | sort(attribute="name", reverse=True) }}'
305 when: query_ansibleBackup_for_delete.current | length >= 2
306
307 # Download Backup
308 - name: Create a directory if it does not exist
309 ansible.builtin.file:
310 path: './{{mso_hostname}}'
311 state: directory
312 mode: '0755'
313
314 - name: Download non existent backup
315 cisco.mso.mso_backup:
316 <<: *mso_info
317 backup: non_existent_backup
318 destination: './{{mso_hostname}}'
319 state: download
320 ignore_errors: true
321 register: download_non_existent_backup
322
323 - name: Assertions check for non existing backup
324 ansible.builtin.assert:
325 that:
326 - download_non_existent_backup.msg is match ("Backup 'non_existent_backup' does not exist")
327
328 # Adding generic block to allow tests to pass for versions above NDO4.1
329 - name: Execute tasks only for MSO version < 4.0
330 when: version.current.version is version('4.0', '<')
331 block:
332
333 - name: Download ansibleBackupRemote1 in check mode
334 cisco.mso.mso_backup: &download_ab1_cm
335 <<: *mso_info
336 backup: ansibleBackupRemote1
337 destination: './{{mso_hostname}}'
338 state: download
339 check_mode: true
340 register: download_ab1_cm
341
342 - name: Download ansibleBackupRemote1 in normal mode
343 cisco.mso.mso_backup:
344 <<: *download_ab1_cm
345 register: download_ab1_nm
346
347 - name: Assertions check for download a backup form MSO/NDO
348 ansible.builtin.assert:
349 that:
350 - download_ab1_cm is changed
351 - download_ab1_nm is changed
352
353 # Find Backup
354 - name: Find backup
355 ansible.builtin.find:
356 paths: './{{mso_hostname}}'
357 patterns: "*.tar.gz"
358 register: backup_match
359
360 # Upload a backup from local machine to local location only for MSO - version < 3.2
361 - name: Execute tasks only for MSO version < 3.2
362 when: version.current.version is version('3.2', '<')
363 block:
364 - name: Upload a backup from local machine to local location in check mode
365 cisco.mso.mso_backup: &upload_backup_to_local_cm
366 <<: *mso_info
367 backup: "{{ backup_match.files[0].path }}"
368 state: upload
369 register: upload_backup_to_local_cm
370 check_mode: true
371
372 - name: Upload a backup from local machine to local location in normal mode
373 cisco.mso.mso_backup:
374 <<: *upload_backup_to_local_cm
375 register: upload_backup_to_local_nm
376
377 - name: Upload a non existent backup from local machine to local location
378 cisco.mso.mso_backup:
379 <<: *mso_info
380 backup: non_existent_backup
381 state: upload
382 register: upload_non_existent_backup
383 ignore_errors: true
384
385 - name: Assertions check for the upload a backup form local machine to local location
386 ansible.builtin.assert:
387 that:
388 - upload_backup_to_local_cm is changed
389 - upload_backup_to_local_nm is changed
390 - "{{ 'Upload failed due to' in upload_non_existent_backup.msg }}"
391
392 # Upload backup to remote location for all platforms
393 # To Support NDO Backup upload
394 - name: Check present working directory with Shell command
395 ansible.builtin.shell:
396 "pwd"
397 register: present_working_directory
398
399 - name: Upload an existing backup from local machine to remote location - check mode
400 cisco.mso.mso_backup: &upload_backup_cm
401 <<: *mso_info
402 backup: "{{present_working_directory.stdout}}/{{ backup_match.files[0].path }}"
403 location_type: remote
404 remote_location: ansible_test
405 remote_path: "tmp"
406 state: upload
407 check_mode: true
408 register: upload_backup_cm
409
410 - name: Upload an existing backup from local machine to remote location - normal mode
411 cisco.mso.mso_backup:
412 <<: *upload_backup_cm
413 register: upload_backup_nm
414 ignore_errors: true
415
416 # Block to handle "Backup already existing issue"
417 - name: Upload backup from local machine to remote again
418 when:
419 - upload_backup_nm is not changed
420 - upload_backup_nm.current == []
421 block:
422 - name: Query all backups - clear existing backups before uploading - to handle "Backup already existing issue"
423 cisco.mso.mso_backup:
424 <<: *mso_info
425 state: query
426 register: query_ansibleBackup_for_delete
427
428 - name: Delete all backups from MSO/NDO before uploading the backup form local machine
429 cisco.mso.mso_backup:
430 <<: *mso_info
431 backup_id: '{{ item.id }}'
432 state: absent
433 loop: '{{ query_ansibleBackup_for_delete.current | list | sort(attribute="name", reverse=True) }}'
434
435 - name: Upload an existing backup from local machine to remote location - normal mode - after removing existing backups
436 cisco.mso.mso_backup:
437 <<: *upload_backup_cm
438 register: upload_backup_nm
439
440 - name: Upload a non existent backup from local location
441 cisco.mso.mso_backup:
442 <<: *mso_info
443 backup: non_existent_backup
444 location_type: remote
445 remote_location: ansible_test
446 remote_path: "tmp"
447 state: upload
448 register: upload_non_existent_backup
449 ignore_errors: true
450
451 - name: Assertions check for the upload a backup form local machine to remote location
452 ansible.builtin.assert:
453 that:
454 - upload_non_existent_backup is not changed
455 - "{{ 'Upload failed due to' in upload_non_existent_backup.msg }}"
456 - upload_backup_cm is changed
457 - upload_backup_cm.current == {}
458 - upload_backup_nm is changed
459 - upload_backup_nm.current != {}
460
461 - name: Set Uploaded Backup Name for MSO
462 when:
463 - version.current.version is version('2.2.4e', '!=')
464 - version.current.version is version('3.2', '<')
465 ansible.builtin.set_fact:
466 uploaded_backup_name: '{{ upload_backup_nm.current.backupEntry.metadata.name.split("_") | first }}'
467
468 - name: Set Uploaded Backup Name for NDO
469 when:
470 - version.current.version is version('2.2.4e', '!=')
471 - version.current.version is version('3.2', '>=')
472 ansible.builtin.set_fact:
473 uploaded_backup_name: '{{ upload_backup_nm.current.body.backupEntry.metadata.name.split("_") | first }}'
474
475 - name: Restore backup check
476 when: version.current.version is version('2.2.4e', '!=')
477 block:
478 - name: Restore non existent backup
479 cisco.mso.mso_backup:
480 <<: *mso_info
481 backup: non_existent_backup
482 state: restore
483 timeout: 1000
484 ignore_errors: true
485 register: restore_non_existent_backup
486
487 - name: Add a new tenant
488 cisco.mso.mso_tenant:
489 <<: *tenant1_absent
490 state: present
491 register: tenant1_present
492
493 - name: Restore {{ uploaded_backup_name }} in check mode
494 cisco.mso.mso_backup: &restore_backup_cm
495 <<: *mso_info
496 backup: '{{ uploaded_backup_name }}'
497 state: restore
498 timeout: 1000
499 check_mode: true
500 register: restore_backup_cm
501
502 - name: Restore {{ uploaded_backup_name }} in normal mode
503 cisco.mso.mso_backup:
504 <<: *restore_backup_cm
505 register: restore_backup_nm
506
507 - name: Query Tenant1 after restoring the backup
508 cisco.mso.mso_tenant:
509 <<: *tenant1_absent
510 state: query
511 register: query_tenant1
512
513 - name: Restore {{ uploaded_backup_name }} in normal mode - idempotency check
514 cisco.mso.mso_backup:
515 <<: *restore_backup_cm
516 register: idm_restore_backup_nm
517
518 - name: Assertions check for the backup restore
519 ansible.builtin.assert:
520 that:
521 - restore_non_existent_backup.msg is match ("Backup 'non_existent_backup' does not exist")
522 - restore_backup_cm is changed
523 - restore_backup_nm is changed
524 - idm_restore_backup_nm is changed
525 - tenant1_present.current != {}
526 - query_tenant1 is not changed
527 - query_tenant1.current == {}
528
529 # Cleaning part
530 # Remove all other backups
531 - name: Query all backups before deleting
532 cisco.mso.mso_backup:
533 <<: *query_all_backups
534 state: query
535 register: query_all_backups_bf_delete
536
537 - name: Ensure backups does not exists
538 cisco.mso.mso_backup:
539 <<: *mso_info
540 backup_id: '{{ item.id }}'
541 state: absent
542 loop: '{{ query_all_backups_bf_delete.current | list | sort(attribute="name", reverse=True) }}'
543
544 - name: Query all backups after deleting - to ensure all are absent
545 cisco.mso.mso_backup:
546 <<: *query_all_backups
547 register: query_all_backups_af_delete
548
549 - name: Assertions check for the query and remove all backups
550 ansible.builtin.assert:
551 that:
552 - query_all_backups_bf_delete is not changed
553 - query_all_backups_af_delete is not changed
554 - query_all_backups_bf_delete.current | selectattr("name", "match", "^ansibleBackup.*") | list | length != 0
555 - query_all_backups_af_delete.current | selectattr("name", "match", "^ansibleBackup.*") | list | length == 0
556
557 # Clear Remote Location
558 - name: Ensure remote location is absent
559 cisco.mso.mso_remote_location:
560 <<: *remote_location_absent
561
562 # Remove Tenant1
563 - name: Ensure Tenant1 is absent
564 cisco.mso.mso_tenant:
565 <<: *tenant1_absent
566 state: absent