| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: ibm_sv_manage_snapshotpolicy |
| 15 |
short_description: This module manages snapshot policy configuration on IBM Storage Virtualize family systems |
| 16 |
version_added: "1.9.0" |
| 17 |
description: |
| 18 |
- Ansible interface to manage 'mksnapshotpolicy' and 'rmsnapshotpolicy' snapshot policy commands. |
| 19 |
- Snapshot policy is introduced in IBM Storage Virtualize 8.5.1.0. |
| 20 |
options: |
| 21 |
clustername: |
| 22 |
description: |
| 23 |
- The hostname or management IP of the Storage Virtualize system. |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
domain: |
| 27 |
description: |
| 28 |
- Domain for the Storage Virtualize system. |
| 29 |
- Valid when hostname is used for the parameter I(clustername). |
| 30 |
type: str |
| 31 |
username: |
| 32 |
description: |
| 33 |
- REST API username for the Storage Virtualize system. |
| 34 |
- The parameters I(username) and I(password) are required if not using I(token) to authenticate a user. |
| 35 |
type: str |
| 36 |
password: |
| 37 |
description: |
| 38 |
- REST API password for the Storage Virtualize system. |
| 39 |
- The parameters I(username) and I(password) are required if not using I(token) to authenticate a user. |
| 40 |
type: str |
| 41 |
token: |
| 42 |
description: |
| 43 |
- The authentication token to verify a user on the Storage Virtualize system. |
| 44 |
- To generate a token, use the M(ibm.storage_virtualize.ibm_svc_auth) module. |
| 45 |
type: str |
| 46 |
log_path: |
| 47 |
description: |
| 48 |
- Path of debug log file. |
| 49 |
type: str |
| 50 |
state: |
| 51 |
description: |
| 52 |
- Creates (C(present)) or deletes (C(absent)) a snapshot policy. |
| 53 |
- Resume (C(resume)) or suspend (C(suspend)) the snapshot policy, system-wide. |
| 54 |
choices: [ present, absent, suspend, resume ] |
| 55 |
required: true |
| 56 |
type: str |
| 57 |
name: |
| 58 |
description: |
| 59 |
- Specifies a unique name of the snapshot policy. |
| 60 |
- Not applicable when I(state=suspend) or I(state=resume). |
| 61 |
type: str |
| 62 |
backupunit: |
| 63 |
description: |
| 64 |
- Specifies the backup unit in mentioned metric. |
| 65 |
- Applies when I(state=present). |
| 66 |
choices: [ minute, hour, day, week, month ] |
| 67 |
type: str |
| 68 |
backupinterval: |
| 69 |
description: |
| 70 |
- Specifies the backup interval. |
| 71 |
- Applies when I(state=present). |
| 72 |
type: str |
| 73 |
backupstarttime: |
| 74 |
description: |
| 75 |
- Specifies the start time of backup in the format YYMMDDHHMM. |
| 76 |
- Applies when I(state=present). |
| 77 |
type: str |
| 78 |
retentiondays: |
| 79 |
description: |
| 80 |
- Specifies the retention days for the backup. |
| 81 |
- Applies when I(state=present). |
| 82 |
type: str |
| 83 |
removefromvolumegroups: |
| 84 |
description: |
| 85 |
- Specify to remove the volume group association from the snapshot policy. |
| 86 |
- Applies when I(state=absent). |
| 87 |
- This option is allowed only for SecurityAdmin users. |
| 88 |
type: bool |
| 89 |
validate_certs: |
| 90 |
description: |
| 91 |
- Validates certification. |
| 92 |
default: false |
| 93 |
type: bool |
| 94 |
author: |
| 95 |
- Shilpi Jain(@Shilpi-J) |
| 96 |
notes: |
| 97 |
- This module supports C(check_mode). |
| 98 |
''' |
| 99 |
|
| 100 |
EXAMPLES = ''' |
| 101 |
- name: Create snapshot policy |
| 102 |
ibm.storage_virtualize.ibm_sv_manage_snapshotpolicy: |
| 103 |
clustername: "{{ cluster }}" |
| 104 |
username: "{{ username }}" |
| 105 |
password: "{{ password }}" |
| 106 |
name: policy0 |
| 107 |
backupunit: day |
| 108 |
backupinterval: 1 |
| 109 |
backupstarttime: 2102281800 |
| 110 |
retentiondays: 15 |
| 111 |
state: present |
| 112 |
- name: Suspend snapshot policy functionality |
| 113 |
ibm.storage_virtualize.ibm_sv_manage_snapshotpolicy: |
| 114 |
clustername: "{{ cluster }}" |
| 115 |
username: "{{ username }}" |
| 116 |
password: "{{ password }}" |
| 117 |
state: suspend |
| 118 |
- name: Resume snapshot policy functionality |
| 119 |
ibm.storage_virtualize.ibm_sv_manage_snapshotpolicy: |
| 120 |
clustername: "{{ cluster }}" |
| 121 |
username: "{{ username }}" |
| 122 |
password: "{{ password }}" |
| 123 |
state: resume |
| 124 |
- name: Delete snapshot policy |
| 125 |
ibm.storage_virtualize.ibm_sv_manage_snapshotpolicy: |
| 126 |
clustername: "{{ cluster }}" |
| 127 |
username: "{{ username }}" |
| 128 |
password: "{{ password }}" |
| 129 |
name: policy0 |
| 130 |
state: absent |
| 131 |
''' |
| 132 |
|
| 133 |
RETURN = '''#''' |
| 134 |
|
| 135 |
from traceback import format_exc |
| 136 |
from ansible.module_utils.basic import AnsibleModule |
| 137 |
from ansible_collections.ibm.storage_virtualize.plugins.module_utils.ibm_svc_utils import ( |
| 138 |
IBMSVCRestApi, svc_argument_spec, |
| 139 |
get_logger |
| 140 |
) |
| 141 |
from ansible.module_utils._text import to_native |
| 142 |
|
| 143 |
|
| 144 |
class IBMSVCSnapshotPolicy: |
| 145 |
|
| 146 |
def __init__(self): |
| 147 |
argument_spec = svc_argument_spec() |
| 148 |
argument_spec.update( |
| 149 |
dict( |
| 150 |
state=dict( |
| 151 |
type='str', |
| 152 |
required=True, |
| 153 |
choices=['present', 'absent', 'suspend', 'resume'] |
| 154 |
), |
| 155 |
name=dict( |
| 156 |
type='str', |
| 157 |
), |
| 158 |
backupunit=dict( |
| 159 |
type='str', |
| 160 |
choices=['minute', 'hour', 'day', 'week', 'month'], |
| 161 |
), |
| 162 |
backupinterval=dict( |
| 163 |
type='str', |
| 164 |
), |
| 165 |
backupstarttime=dict( |
| 166 |
type='str', |
| 167 |
), |
| 168 |
retentiondays=dict( |
| 169 |
type='str', |
| 170 |
), |
| 171 |
removefromvolumegroups=dict( |
| 172 |
type='bool' |
| 173 |
), |
| 174 |
) |
| 175 |
) |
| 176 |
|
| 177 |
self.module = AnsibleModule(argument_spec=argument_spec, |
| 178 |
supports_check_mode=True) |
| 179 |
|
| 180 |
|
| 181 |
self.name = self.module.params['name'] |
| 182 |
self.state = self.module.params['state'] |
| 183 |
self.backupunit = self.module.params.get('backupunit', '') |
| 184 |
self.backupinterval = self.module.params.get('backupinterval', '') |
| 185 |
self.backupstarttime = self.module.params.get('backupstarttime', '') |
| 186 |
self.retentiondays = self.module.params.get('retentiondays', '') |
| 187 |
self.removefromvolumegroups = self.module.params.get('removefromvolumegroups', False) |
| 188 |
|
| 189 |
self.basic_checks() |
| 190 |
|
| 191 |
|
| 192 |
self.snapshot_policy_details = None |
| 193 |
|
| 194 |
|
| 195 |
self.log_path = self.module.params['log_path'] |
| 196 |
log = get_logger(self.__class__.__name__, self.log_path) |
| 197 |
self.log = log.info |
| 198 |
self.changed = False |
| 199 |
self.msg = '' |
| 200 |
|
| 201 |
self.restapi = IBMSVCRestApi( |
| 202 |
module=self.module, |
| 203 |
clustername=self.module.params['clustername'], |
| 204 |
domain=self.module.params['domain'], |
| 205 |
username=self.module.params['username'], |
| 206 |
password=self.module.params['password'], |
| 207 |
validate_certs=self.module.params['validate_certs'], |
| 208 |
log_path=self.log_path, |
| 209 |
token=self.module.params['token'] |
| 210 |
) |
| 211 |
|
| 212 |
def basic_checks(self): |
| 213 |
if self.state == 'present': |
| 214 |
fields = ['name', 'backupinterval', 'backupstarttime', 'retentiondays', 'backupunit'] |
| 215 |
exists = list(filter(lambda x: not getattr(self, x), fields)) |
| 216 |
|
| 217 |
if any(exists): |
| 218 |
self.module.fail_json( |
| 219 |
msg="State is present but following parameters are missing: {0}".format(', '.join(exists)) |
| 220 |
) |
| 221 |
|
| 222 |
if self.removefromvolumegroups: |
| 223 |
self.module.fail_json( |
| 224 |
msg="`removefromvolumegroups` parameter is not supported when state=present" |
| 225 |
) |
| 226 |
elif self.state == 'absent': |
| 227 |
if not self.name: |
| 228 |
self.module.fail_json(msg="Missing mandatory parameter: name") |
| 229 |
|
| 230 |
fields = ['backupinterval', 'backupstarttime', 'retentiondays', 'backupunit'] |
| 231 |
exists = list(filter(lambda x: getattr(self, x) or getattr(self, x) == '', fields)) |
| 232 |
|
| 233 |
if any(exists): |
| 234 |
self.module.fail_json(msg='{0} should not be passed when state=absent'.format(', '.join(exists))) |
| 235 |
elif self.state in ['suspend', 'resume']: |
| 236 |
fields = ['name', 'backupinterval', 'backupstarttime', 'retentiondays', 'backupunit'] |
| 237 |
exists = list(filter(lambda x: getattr(self, x) or getattr(self, x) == '', fields)) |
| 238 |
|
| 239 |
if any(exists): |
| 240 |
self.module.fail_json(msg='{0} should not be passed when state={1}'.format(', '.join(exists), self.state)) |
| 241 |
|
| 242 |
def policy_exists(self): |
| 243 |
merged_result = {} |
| 244 |
data = self.restapi.svc_obj_info( |
| 245 |
cmd='lssnapshotschedule', |
| 246 |
cmdopts=None, |
| 247 |
cmdargs=[self.name] |
| 248 |
) |
| 249 |
if isinstance(data, list): |
| 250 |
for d in data: |
| 251 |
merged_result.update(d) |
| 252 |
else: |
| 253 |
merged_result = data |
| 254 |
|
| 255 |
self.snapshot_policy_details = merged_result |
| 256 |
|
| 257 |
return merged_result |
| 258 |
|
| 259 |
def create_snapshot_policy(self): |
| 260 |
if self.module.check_mode: |
| 261 |
self.changed = True |
| 262 |
return |
| 263 |
|
| 264 |
cmd = 'mksnapshotpolicy' |
| 265 |
cmdopts = { |
| 266 |
'name': self.name, |
| 267 |
'backupstarttime': self.backupstarttime, |
| 268 |
'backupinterval': self.backupinterval, |
| 269 |
'backupunit': self.backupunit, |
| 270 |
'retentiondays': self.retentiondays |
| 271 |
} |
| 272 |
|
| 273 |
self.restapi.svc_run_command(cmd, cmdopts, cmdargs=None) |
| 274 |
self.log('Snapshot policy (%s) created', self.name) |
| 275 |
self.changed = True |
| 276 |
|
| 277 |
def snapshot_policy_probe(self): |
| 278 |
field_mappings = ( |
| 279 |
('backupinterval', self.snapshot_policy_details['backup_interval']), |
| 280 |
('backupstarttime', self.snapshot_policy_details['backup_start_time']), |
| 281 |
('retentiondays', self.snapshot_policy_details['retention_days']), |
| 282 |
('backupunit', self.snapshot_policy_details['backup_unit']) |
| 283 |
) |
| 284 |
updates = [] |
| 285 |
|
| 286 |
for field, existing_value in field_mappings: |
| 287 |
if field == 'backupstarttime': |
| 288 |
updates.append(existing_value != '{0}00'.format(getattr(self, field))) |
| 289 |
else: |
| 290 |
updates.append(existing_value != getattr(self, field)) |
| 291 |
|
| 292 |
return updates |
| 293 |
|
| 294 |
def delete_snapshot_policy(self): |
| 295 |
if self.module.check_mode: |
| 296 |
self.changed = True |
| 297 |
return |
| 298 |
|
| 299 |
cmd = 'rmsnapshotpolicy' |
| 300 |
cmdargs = [self.name] |
| 301 |
cmdopts = None |
| 302 |
|
| 303 |
if self.removefromvolumegroups: |
| 304 |
cmdopts = { |
| 305 |
'removefromvolumegroups': True |
| 306 |
} |
| 307 |
|
| 308 |
self.restapi.svc_run_command(cmd, cmdopts=cmdopts, cmdargs=cmdargs) |
| 309 |
self.log('Snapshot policy (%s) deleted', self.name) |
| 310 |
self.changed = True |
| 311 |
|
| 312 |
def update_snapshot_scheduler(self): |
| 313 |
if self.module.check_mode: |
| 314 |
self.changed = True |
| 315 |
return |
| 316 |
|
| 317 |
cmd = 'chsystem' |
| 318 |
cmdopts = {'snapshotpolicysuspended': 'yes' if self.state == 'suspend' else 'no'} |
| 319 |
|
| 320 |
self.restapi.svc_run_command(cmd, cmdopts=cmdopts, cmdargs=None) |
| 321 |
self.log('Snapshot scheduler status changed: %s', self.state) |
| 322 |
self.changed = True |
| 323 |
|
| 324 |
def apply(self): |
| 325 |
if self.state in ['resume', 'suspend']: |
| 326 |
self.update_snapshot_scheduler() |
| 327 |
self.msg = 'Snapshot scheduler {0}ed'.format(self.state.rstrip('e')) |
| 328 |
else: |
| 329 |
if self.policy_exists(): |
| 330 |
if self.state == 'present': |
| 331 |
modifications = self.snapshot_policy_probe() |
| 332 |
if any(modifications): |
| 333 |
self.msg = 'Policy modification is not supported in ansible. Please delete and recreate new policy.' |
| 334 |
else: |
| 335 |
self.msg = 'Snapshot policy ({0}) already exists. No modifications done.'.format(self.name) |
| 336 |
else: |
| 337 |
self.delete_snapshot_policy() |
| 338 |
self.msg = 'Snapshot policy ({0}) deleted.'.format(self.name) |
| 339 |
else: |
| 340 |
if self.state == 'absent': |
| 341 |
self.msg = 'Snapshot policy ({0}) does not exist. No modifications done.'.format(self.name) |
| 342 |
else: |
| 343 |
self.create_snapshot_policy() |
| 344 |
self.msg = 'Snapshot policy ({0}) created.'.format(self.name) |
| 345 |
|
| 346 |
if self.module.check_mode: |
| 347 |
self.msg = 'skipping changes due to check mode.' |
| 348 |
|
| 349 |
self.module.exit_json( |
| 350 |
changed=self.changed, |
| 351 |
msg=self.msg |
| 352 |
) |
| 353 |
|
| 354 |
|
| 355 |
def main(): |
| 356 |
v = IBMSVCSnapshotPolicy() |
| 357 |
try: |
| 358 |
v.apply() |
| 359 |
except Exception as e: |
| 360 |
v.log("Exception in apply(): \n%s", format_exc()) |
| 361 |
v.module.fail_json(msg="Module failed. Error [%s]." % to_native(e)) |
| 362 |
|
| 363 |
|
| 364 |
if __name__ == '__main__': |
| 365 |
main() |