-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuclalib_vmware_snapshot.yml
125 lines (113 loc) · 4.52 KB
/
uclalib_vmware_snapshot.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
121
122
123
124
125
---
###############################################################################
# Playbook for VMware Guest Snapshots
# -------------------------------------
#
# This playbook can create or remove snapshots for VMware guests.
# Supported paramters:
# rhel_inventory_host_group (default: unattended_yum_update)
# The group (or host) to snapshot
# snapshot_prefix (default: rhel-update)
# Prefix name to apply
# remove_snapshot (default: false)
# Controls the creation or removal of the snapshot
# The default of false will create a snapshot
# days_to_keep_snapshots (default: 7)
# When removing a snapshot, save al snapshots more recent than this in days
# The default is one week
#
###############################################################################
- name: uclalib_vmware_snapshot.yml
hosts: "{{ rhel_inventory_host_group | default('unattended_yum_update') }}"
gather_facts: false
vars:
vcenter_hostname: vc.library.ucla.edu
vsphere_datacenter: "Library Information Technology"
vsphere_domain: "ad.library.ucla.edu"
vsphere_user: "{{ vmware_user | default }}"
vsphere_password: "{{ vmware_password | default }}"
vsphere_folder: "/"
snapshot_prefix: "rhel-update"
remove_snapshot: false
# for python3
# date_format_snapshot: "%Y-%m-%dT%H:%M:%S.%f%z"
# for python2, assuming UTC
date_format_snapshot: "%Y-%m-%dT%H:%M:%S.%f+00:00"
date_format_ansible: "%Y-%m-%dT%H:%M:%SZ"
days_to_keep_snapshots: 7
tasks:
- name: Gathering Facts
setup:
fact_path: omit
gather_subset: "!all,virtual"
- name: Use standard python
set_fact:
ansible_python_interpreter: /usr/libexec/platform-python
- name: Set vSphere username (implicit domain)
set_fact:
vsphere_username: "{{ vsphere_user + '@' + vsphere_domain }}"
when: vsphere_user is not search("@")
- name: Set vSphere username (explicit domain)
set_fact:
vsphere_username: "{{ vsphere_user }}"
when: vsphere_user is search("@")
# snapshot creation/deletion block
- block:
- name: Create Snapshots
vmware_guest_snapshot:
datacenter: "{{ vsphere_datacenter }}"
state: present
folder: "{{ vsphere_folder }}"
hostname: "{{ vcenter_hostname }}"
password: "{{ vsphere_password }}"
name: "{{ ansible_hostname }}"
quiesce: true
snapshot_name: "{{ snapshot_prefix }}-{{ ansible_date_time.date }}"
username: "{{ vsphere_username }}"
validate_certs: false
connection: local
when:
- not remove_snapshot|bool
# remove snapshot block
- block:
- name: Get Snapshots
vmware_guest_snapshot_info:
datacenter: "{{ vsphere_datacenter }}"
folder: "{{ vsphere_folder }}"
hostname: "{{ vcenter_hostname }}"
password: "{{ vsphere_password }}"
name: "{{ ansible_hostname }}"
username: "{{ vsphere_username }}"
validate_certs: false
connection: local
register: snapshots
- name: Remove outdated snapshots
vmware_guest_snapshot:
datacenter: "{{ vsphere_datacenter }}"
state: absent
folder: "{{ vsphere_folder }}"
hostname: "{{ vcenter_hostname }}"
password: "{{ vsphere_password }}"
name: "{{ ansible_hostname }}"
snapshot_name: "{{ item.name }}"
username: "{{ vsphere_username }}"
validate_certs: false
connection: local
when:
- snapshots.guest_snapshots.snapshots is defined
# subtract seconds in epoch of snapshot creation from now
# if it is greater than X days, remove the snapshot
- ( (ansible_date_time.iso8601
| to_datetime(date_format_ansible ))
- (item.creation_time
| to_datetime(date_format_snapshot))
).total_seconds()
> days_to_keep_snapshots|int * 86400
- item.name | regex_search( "^" + snapshot_prefix + "-" )
loop: "{{ snapshots.guest_snapshots.snapshots }}"
# end remove snapshot block
when:
- remove_snapshot | bool
# end snapshot creation/deletion block
when:
- ansible_virtualization_type == "VMware"