| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
DOCUMENTATION = ''' |
| 14 |
--- |
| 15 |
module: ibm_svc_vol_map |
| 16 |
short_description: This module manages volume mapping on IBM Storage Virtualize family systems |
| 17 |
description: |
| 18 |
- Ansible interface to manage volume mapping commands |
| 19 |
'mkvdiskhostmap', 'rmvdiskhostmap', 'mkvolumehostclustermap', and 'rmvolumehostclustermap'. |
| 20 |
version_added: "1.0.0" |
| 21 |
options: |
| 22 |
volname: |
| 23 |
description: |
| 24 |
- Specifies the volume name for host or hostcluster mapping. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
host: |
| 28 |
description: |
| 29 |
- Specifies the host name for host mapping. |
| 30 |
- This parameter is required to create or delete a volume-to-host mapping. |
| 31 |
type: str |
| 32 |
hostcluster: |
| 33 |
description: |
| 34 |
- Specifies the name of the host cluster for host mapping. |
| 35 |
- This parameter is required to create or delete a volume-to-hostcluster mapping. |
| 36 |
type: str |
| 37 |
scsi: |
| 38 |
description: |
| 39 |
- Specifies the SCSI logical unit number (LUN) ID to assign to a volume on the specified host or host cluster. |
| 40 |
- Applies when I(state=present). |
| 41 |
type: int |
| 42 |
state: |
| 43 |
description: |
| 44 |
- Creates (C(present)) or removes (C(absent)) a volume mapping. |
| 45 |
choices: [ absent, present ] |
| 46 |
required: true |
| 47 |
type: str |
| 48 |
clustername: |
| 49 |
description: |
| 50 |
- The hostname or management IP or Partition IP of the Storage Virtualize system. |
| 51 |
type: str |
| 52 |
required: true |
| 53 |
domain: |
| 54 |
description: |
| 55 |
- Domain for the Storage Virtualize system. |
| 56 |
- Valid when hostname is used for the parameter I(clustername). |
| 57 |
type: str |
| 58 |
username: |
| 59 |
description: |
| 60 |
- REST API username for the Storage Virtualize system. |
| 61 |
- The parameters I(username) and I(password) are required if not using I(token) to authenticate a user. |
| 62 |
type: str |
| 63 |
password: |
| 64 |
description: |
| 65 |
- REST API password for the Storage Virtualize system. |
| 66 |
- The parameters I(username) and I(password) are required if not using I(token) to authenticate a user. |
| 67 |
type: str |
| 68 |
token: |
| 69 |
description: |
| 70 |
- The authentication token to verify a user on the Storage Virtualize system. |
| 71 |
- To generate a token, use the ibm_svc_auth module. |
| 72 |
type: str |
| 73 |
version_added: '1.5.0' |
| 74 |
log_path: |
| 75 |
description: |
| 76 |
- Path of debug log file. |
| 77 |
type: str |
| 78 |
validate_certs: |
| 79 |
description: |
| 80 |
- Validates certification. |
| 81 |
default: false |
| 82 |
type: bool |
| 83 |
author: |
| 84 |
- Peng Wang(@wangpww) |
| 85 |
notes: |
| 86 |
- This module supports C(check_mode). |
| 87 |
- This module supports logging in via partition IP. |
| 88 |
''' |
| 89 |
|
| 90 |
EXAMPLES = ''' |
| 91 |
- name: Map a volume to a host |
| 92 |
ibm.storage_virtualize.ibm_svc_vol_map: |
| 93 |
clustername: "{{ clustername }}" |
| 94 |
domain: "{{ domain }}" |
| 95 |
username: "{{ username }}" |
| 96 |
password: "{{ password }}" |
| 97 |
log_path: /tmp/playbook.debug |
| 98 |
volname: volume0 |
| 99 |
host: host4test |
| 100 |
scsi: 1 |
| 101 |
state: present |
| 102 |
- name: Unmap a volume from a host |
| 103 |
ibm.storage_virtualize.ibm_svc_vol_map: |
| 104 |
clustername: "{{ clustername }}" |
| 105 |
domain: "{{ domain }}" |
| 106 |
username: "{{ username }}" |
| 107 |
password: "{{ password }}" |
| 108 |
log_path: /tmp/playbook.debug |
| 109 |
volname: volume0 |
| 110 |
host: host4test |
| 111 |
state: absent |
| 112 |
''' |
| 113 |
|
| 114 |
RETURN = '''#''' |
| 115 |
|
| 116 |
from traceback import format_exc |
| 117 |
from ansible.module_utils.basic import AnsibleModule |
| 118 |
from ansible_collections.ibm.storage_virtualize.plugins.module_utils.ibm_svc_utils import IBMSVCRestApi, svc_argument_spec, get_logger |
| 119 |
from ansible.module_utils._text import to_native |
| 120 |
|
| 121 |
|
| 122 |
class IBMSVCvdiskhostmap(object): |
| 123 |
def __init__(self): |
| 124 |
argument_spec = svc_argument_spec() |
| 125 |
|
| 126 |
argument_spec.update( |
| 127 |
dict( |
| 128 |
volname=dict(type='str', required=True), |
| 129 |
host=dict(type='str', required=False), |
| 130 |
state=dict(type='str', required=True, choices=['absent', |
| 131 |
'present']), |
| 132 |
scsi=dict(type='int', required=False), |
| 133 |
hostcluster=dict(type='str', required=False) |
| 134 |
) |
| 135 |
) |
| 136 |
|
| 137 |
self.module = AnsibleModule(argument_spec=argument_spec, |
| 138 |
supports_check_mode=True) |
| 139 |
|
| 140 |
|
| 141 |
log_path = self.module.params['log_path'] |
| 142 |
log = get_logger(self.__class__.__name__, log_path) |
| 143 |
self.log = log.info |
| 144 |
|
| 145 |
|
| 146 |
self.volname = self.module.params['volname'] |
| 147 |
self.state = self.module.params['state'] |
| 148 |
|
| 149 |
|
| 150 |
self.host = self.module.params['host'] |
| 151 |
self.hostcluster = self.module.params['hostcluster'] |
| 152 |
self.scsi = self.module.params['scsi'] |
| 153 |
|
| 154 |
|
| 155 |
if not self.volname: |
| 156 |
self.module.fail_json(msg="Missing mandatory parameter: volname") |
| 157 |
|
| 158 |
self.restapi = IBMSVCRestApi( |
| 159 |
module=self.module, |
| 160 |
clustername=self.module.params['clustername'], |
| 161 |
domain=self.module.params['domain'], |
| 162 |
username=self.module.params['username'], |
| 163 |
password=self.module.params['password'], |
| 164 |
validate_certs=self.module.params['validate_certs'], |
| 165 |
log_path=log_path, |
| 166 |
token=self.module.params['token'] |
| 167 |
) |
| 168 |
|
| 169 |
def get_existing_vdiskhostmap(self): |
| 170 |
merged_result = [] |
| 171 |
|
| 172 |
data = self.restapi.svc_obj_info(cmd='lsvdiskhostmap', cmdopts=None, |
| 173 |
cmdargs=[self.volname]) |
| 174 |
|
| 175 |
if isinstance(data, list): |
| 176 |
for d in data: |
| 177 |
merged_result.append(d) |
| 178 |
elif data: |
| 179 |
merged_result = [data] |
| 180 |
|
| 181 |
return merged_result |
| 182 |
|
| 183 |
|
| 184 |
def vdiskhostmap_probe(self, mdata): |
| 185 |
props = [] |
| 186 |
self.log("vdiskhostmap_probe props='%s'", mdata) |
| 187 |
mapping_exist = False |
| 188 |
for data in mdata: |
| 189 |
if self.host: |
| 190 |
if (self.host == data['host_name']) and (self.volname == data['name']): |
| 191 |
if self.scsi and (self.scsi != int(data['SCSI_id'])): |
| 192 |
self.module.fail_json(msg="Update not supported for parameter: scsi") |
| 193 |
mapping_exist = True |
| 194 |
elif self.hostcluster: |
| 195 |
if (self.hostcluster == data['host_cluster_name']) and (self.volname == data['name']): |
| 196 |
if self.scsi and (self.scsi != int(data['SCSI_id'])): |
| 197 |
self.module.fail_json(msg="Update not supported for parameter: scsi") |
| 198 |
mapping_exist = True |
| 199 |
|
| 200 |
if not mapping_exist: |
| 201 |
props += ["map"] |
| 202 |
|
| 203 |
if props is []: |
| 204 |
props = None |
| 205 |
|
| 206 |
self.log("vdiskhostmap_probe props='%s'", props) |
| 207 |
return props |
| 208 |
|
| 209 |
def vdiskhostmap_create(self): |
| 210 |
if self.module.check_mode: |
| 211 |
self.changed = True |
| 212 |
return |
| 213 |
|
| 214 |
self.log("creating vdiskhostmap '%s' '%s'", self.volname, self.host) |
| 215 |
|
| 216 |
|
| 217 |
cmd = 'mkvdiskhostmap' |
| 218 |
cmdopts = {'force': True} |
| 219 |
cmdopts['host'] = self.host |
| 220 |
cmdopts['scsi'] = self.scsi |
| 221 |
cmdargs = [self.volname] |
| 222 |
|
| 223 |
self.log("creating vdiskhostmap command %s opts %s args %s", |
| 224 |
cmd, cmdopts, cmdargs) |
| 225 |
|
| 226 |
|
| 227 |
result = self.restapi.svc_run_command(cmd, cmdopts, cmdargs) |
| 228 |
self.log("create vdiskhostmap result %s", result) |
| 229 |
|
| 230 |
if 'message' in result: |
| 231 |
self.changed = True |
| 232 |
self.log("create vdiskhostmap result message %s", |
| 233 |
result['message']) |
| 234 |
else: |
| 235 |
self.module.fail_json(msg="Failed to create vdiskhostmap.") |
| 236 |
|
| 237 |
def vdiskhostmap_delete(self): |
| 238 |
if self.module.check_mode: |
| 239 |
self.changed = True |
| 240 |
return |
| 241 |
|
| 242 |
self.log("deleting vdiskhostmap '%s'", self.volname) |
| 243 |
|
| 244 |
cmd = 'rmvdiskhostmap' |
| 245 |
cmdopts = {} |
| 246 |
cmdopts['host'] = self.host |
| 247 |
cmdargs = [self.volname] |
| 248 |
|
| 249 |
self.restapi.svc_run_command(cmd, cmdopts, cmdargs) |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
self.changed = True |
| 254 |
|
| 255 |
def vdiskhostclustermap_create(self): |
| 256 |
if self.module.check_mode: |
| 257 |
self.changed = True |
| 258 |
return |
| 259 |
|
| 260 |
self.log("creating mkvolumehostclustermap '%s' '%s'", self.volname, self.hostcluster) |
| 261 |
|
| 262 |
|
| 263 |
cmd = 'mkvolumehostclustermap' |
| 264 |
cmdopts = {'force': True} |
| 265 |
cmdopts['hostcluster'] = self.hostcluster |
| 266 |
cmdopts['scsi'] = self.scsi |
| 267 |
cmdargs = [self.volname] |
| 268 |
|
| 269 |
self.log("creating vdiskhostmap command %s opts %s args %s", |
| 270 |
cmd, cmdopts, cmdargs) |
| 271 |
|
| 272 |
|
| 273 |
result = self.restapi.svc_run_command(cmd, cmdopts, cmdargs) |
| 274 |
self.log("create vdiskhostmap result %s", result) |
| 275 |
|
| 276 |
if 'message' in result: |
| 277 |
self.changed = True |
| 278 |
self.log("create vdiskhostmap result message %s", |
| 279 |
result['message']) |
| 280 |
else: |
| 281 |
self.module.fail_json(msg="Failed to create vdiskhostmap.") |
| 282 |
|
| 283 |
def vdiskhostclustermap_delete(self): |
| 284 |
if self.module.check_mode: |
| 285 |
self.changed = True |
| 286 |
return |
| 287 |
|
| 288 |
self.log("deleting vdiskhostclustermap '%s'", self.volname) |
| 289 |
|
| 290 |
cmd = 'rmvolumehostclustermap' |
| 291 |
cmdopts = {} |
| 292 |
cmdopts['hostcluster'] = self.hostcluster |
| 293 |
cmdargs = [self.volname] |
| 294 |
|
| 295 |
self.restapi.svc_run_command(cmd, cmdopts, cmdargs) |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
self.changed = True |
| 300 |
|
| 301 |
def apply(self): |
| 302 |
changed = False |
| 303 |
msg = None |
| 304 |
|
| 305 |
|
| 306 |
if not self.volname: |
| 307 |
self.module.fail_json(msg="You must pass in " |
| 308 |
"volname to the module.") |
| 309 |
|
| 310 |
|
| 311 |
if (self.host and self.hostcluster): |
| 312 |
self.module.fail_json(msg="Either use host or hostcluster") |
| 313 |
elif (not self.host and not self.hostcluster): |
| 314 |
self.module.fail_json(msg="Missing parameter: host or hostcluster") |
| 315 |
|
| 316 |
vdiskmap_data = self.get_existing_vdiskhostmap() |
| 317 |
self.log("volume mapping data is : '%s'", vdiskmap_data) |
| 318 |
|
| 319 |
if vdiskmap_data: |
| 320 |
if self.state == 'absent': |
| 321 |
self.log("vdiskmap exists, " |
| 322 |
"and requested state is 'absent'") |
| 323 |
changed = True |
| 324 |
elif self.state == 'present': |
| 325 |
probe_data = self.vdiskhostmap_probe(vdiskmap_data) |
| 326 |
if probe_data: |
| 327 |
self.log("vdiskmap does not exist, but requested state is 'present'") |
| 328 |
changed = True |
| 329 |
else: |
| 330 |
if self.state == 'present': |
| 331 |
self.log("vdiskmap does not exist, " |
| 332 |
"but requested state is 'present'") |
| 333 |
changed = True |
| 334 |
|
| 335 |
if changed: |
| 336 |
if self.state == 'present': |
| 337 |
if self.host: |
| 338 |
self.vdiskhostmap_create() |
| 339 |
msg = "Vdiskhostmap %s %s has been created." % ( |
| 340 |
self.volname, self.host) |
| 341 |
elif self.hostcluster: |
| 342 |
self.vdiskhostclustermap_create() |
| 343 |
msg = "Vdiskhostclustermap %s %s has been created." % ( |
| 344 |
self.volname, self.hostcluster) |
| 345 |
elif self.state == 'absent': |
| 346 |
if self.host: |
| 347 |
self.vdiskhostmap_delete() |
| 348 |
msg = "vdiskhostmap [%s] has been deleted." % self.volname |
| 349 |
elif self.hostcluster: |
| 350 |
self.vdiskhostclustermap_delete() |
| 351 |
msg = "vdiskhostclustermap [%s] has been deleted." % self.volname |
| 352 |
|
| 353 |
if self.module.check_mode: |
| 354 |
msg = 'skipping changes due to check mode' |
| 355 |
else: |
| 356 |
self.log("exiting with no changes") |
| 357 |
if self.state == 'absent': |
| 358 |
msg = "Volume mapping [%s] did not exist." % self.volname |
| 359 |
else: |
| 360 |
msg = "Volume mapping [%s] already exists." % self.volname |
| 361 |
|
| 362 |
self.module.exit_json(msg=msg, changed=changed) |
| 363 |
|
| 364 |
|
| 365 |
def main(): |
| 366 |
v = IBMSVCvdiskhostmap() |
| 367 |
try: |
| 368 |
v.apply() |
| 369 |
except Exception as e: |
| 370 |
v.log("Exception in apply(): \n%s", format_exc()) |
| 371 |
v.module.fail_json(msg="Module failed. Error [%s]." % to_native(e)) |
| 372 |
|
| 373 |
|
| 374 |
if __name__ == '__main__': |
| 375 |
main() |