| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018-2025, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
|
| 11 |
module: na_ontap_disks |
| 12 |
|
| 13 |
short_description: NetApp ONTAP Assign disks to nodes |
| 14 |
extends_documentation_fragment: |
| 15 |
- netapp.ontap.netapp.na_ontap |
| 16 |
version_added: 2.7.0 |
| 17 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 18 |
|
| 19 |
description: |
| 20 |
- Assign disks to a node. |
| 21 |
- Disk autoassign must be turned off before using this module to prevent the disks being reassigned automatically by the cluster. |
| 22 |
- This can be done through na_ontap_disk_options or via the cli "disk option modify -node <node_name> -autoassign off". |
| 23 |
- If min_spares is not specified min_spares default is 1 if SSD or 2 for any other disk type. |
| 24 |
- If disk_count is not specified all unassigned disks will be assigned to the node specified. |
| 25 |
|
| 26 |
options: |
| 27 |
node: |
| 28 |
required: true |
| 29 |
type: str |
| 30 |
description: |
| 31 |
- The node that we want to assign/unassign disks. |
| 32 |
|
| 33 |
disk_count: |
| 34 |
description: |
| 35 |
- Total number of disks a node should own. |
| 36 |
type: int |
| 37 |
version_added: 2.9.0 |
| 38 |
|
| 39 |
disk_type: |
| 40 |
description: |
| 41 |
- Assign specified type of disk (or set of disks). |
| 42 |
type: str |
| 43 |
choices: ['ATA', 'BSAS', 'FCAL', 'FSAS', 'LUN', 'MSATA', 'SAS', 'SSD', 'SSD_NVM', 'VMDISK', 'unknown'] |
| 44 |
version_added: 20.6.0 |
| 45 |
|
| 46 |
min_spares: |
| 47 |
description: |
| 48 |
- Minimum spares required per type for the node. |
| 49 |
type: int |
| 50 |
version_added: 21.7.0 |
| 51 |
|
| 52 |
''' |
| 53 |
|
| 54 |
EXAMPLES = """ |
| 55 |
- name: Assign specified total disks to node |
| 56 |
netapp.ontap.na_ontap_disks: |
| 57 |
node: node1 |
| 58 |
disk_count: 56 |
| 59 |
disk_type: VMDISK |
| 60 |
min_spares: 2 |
| 61 |
username: "{{ netapp_username }}" |
| 62 |
password: "{{ netapp_password }}" |
| 63 |
hostname: "{{ netapp_hostname }}" |
| 64 |
|
| 65 |
- name: Assign all unassigned disks to node1 |
| 66 |
netapp.ontap.na_ontap_disks: |
| 67 |
node: node1 |
| 68 |
username: "{{ netapp_username }}" |
| 69 |
password: "{{ netapp_password }}" |
| 70 |
hostname: "{{ netapp_hostname }}" |
| 71 |
""" |
| 72 |
|
| 73 |
RETURN = """ |
| 74 |
|
| 75 |
""" |
| 76 |
import traceback |
| 77 |
|
| 78 |
from ansible.module_utils.basic import AnsibleModule |
| 79 |
from ansible.module_utils._text import to_native |
| 80 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 81 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 82 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 83 |
import ansible_collections.netapp.ontap.plugins.module_utils.rest_response_helpers as rrh |
| 84 |
|
| 85 |
|
| 86 |
class NetAppOntapDisks(): |
| 87 |
''' object initialize and class methods ''' |
| 88 |
|
| 89 |
def __init__(self): |
| 90 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 91 |
self.argument_spec.update(dict( |
| 92 |
node=dict(required=True, type='str'), |
| 93 |
disk_count=dict(required=False, type='int'), |
| 94 |
disk_type=dict(required=False, type='str', choices=['ATA', 'BSAS', 'FCAL', 'FSAS', 'LUN', 'MSATA', 'SAS', 'SSD', 'SSD_NVM', 'VMDISK', 'unknown']), |
| 95 |
min_spares=dict(required=False, type='int') |
| 96 |
)) |
| 97 |
|
| 98 |
self.module = AnsibleModule( |
| 99 |
argument_spec=self.argument_spec, |
| 100 |
supports_check_mode=True |
| 101 |
) |
| 102 |
|
| 103 |
self.na_helper = NetAppModule() |
| 104 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 105 |
|
| 106 |
# If min_spares is not specified min_spares is 1 if SSD, min_spares is 2 for any other disk type. |
| 107 |
self.parameters['min_spares'] = 1 if self.parameters.get('disk_type') in ('SSD', 'SSD_NVM') else 2 |
| 108 |
|
| 109 |
self.rest_api = OntapRestAPI(self.module) |
| 110 |
self.use_rest = self.rest_api.is_rest() |
| 111 |
|
| 112 |
if not self.use_rest: |
| 113 |
if not netapp_utils.has_netapp_lib(): |
| 114 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 115 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module) |
| 116 |
|
| 117 |
def get_disks(self, container_type, node=None): |
| 118 |
""" |
| 119 |
Check for owned disks, unassigned disks or spare disks. |
| 120 |
Return: list of disks or an empty list |
| 121 |
""" |
| 122 |
if self.use_rest: |
| 123 |
api = "storage/disks" |
| 124 |
if container_type == 'owned': |
| 125 |
query = { |
| 126 |
'home_node.name': node, |
| 127 |
'container_type': '!unassigned', |
| 128 |
'fields': 'name' |
| 129 |
} |
| 130 |
if container_type == 'unassigned': |
| 131 |
query = { |
| 132 |
'container_type': 'unassigned', |
| 133 |
'fields': 'name' |
| 134 |
} |
| 135 |
if container_type == 'spare': |
| 136 |
query = { |
| 137 |
'home_node.name': node, |
| 138 |
'container_type': 'spare', |
| 139 |
'fields': 'name' |
| 140 |
} |
| 141 |
if 'disk_type' in self.parameters: |
| 142 |
query['type'] = self.parameters['disk_type'] |
| 143 |
|
| 144 |
message, error = self.rest_api.get(api, query) |
| 145 |
records, error = rrh.check_for_0_or_more_records(api, message, error) |
| 146 |
|
| 147 |
if error: |
| 148 |
self.module.fail_json(msg=error) |
| 149 |
|
| 150 |
return records if records else list() |
| 151 |
|
| 152 |
else: |
| 153 |
disk_iter = netapp_utils.zapi.NaElement('storage-disk-get-iter') |
| 154 |
disk_storage_info = netapp_utils.zapi.NaElement('storage-disk-info') |
| 155 |
|
| 156 |
if container_type == 'owned': |
| 157 |
disk_ownership_info = netapp_utils.zapi.NaElement('disk-ownership-info') |
| 158 |
disk_ownership_info.add_new_child('home-node-name', self.parameters['node']) |
| 159 |
disk_storage_info.add_child_elem(disk_ownership_info) |
| 160 |
if container_type == 'unassigned': |
| 161 |
disk_raid_info = netapp_utils.zapi.NaElement('disk-raid-info') |
| 162 |
disk_raid_info.add_new_child('container-type', 'unassigned') |
| 163 |
disk_storage_info.add_child_elem(disk_raid_info) |
| 164 |
|
| 165 |
disk_query = netapp_utils.zapi.NaElement('query') |
| 166 |
|
| 167 |
if 'disk_type' in self.parameters and container_type in ('unassigned', 'owned'): |
| 168 |
disk_inventory_info = netapp_utils.zapi.NaElement('disk-inventory-info') |
| 169 |
disk_inventory_info.add_new_child('disk-type', self.parameters['disk_type']) |
| 170 |
disk_query.add_child_elem(disk_inventory_info) |
| 171 |
|
| 172 |
if container_type == 'spare': |
| 173 |
disk_ownership_info = netapp_utils.zapi.NaElement('disk-ownership-info') |
| 174 |
disk_raid_info = netapp_utils.zapi.NaElement('disk-raid-info') |
| 175 |
disk_ownership_info.add_new_child('owner-node-name', node) |
| 176 |
if 'disk_type' in self.parameters: |
| 177 |
disk_inventory_info = netapp_utils.zapi.NaElement('disk-inventory-info') |
| 178 |
disk_inventory_info.add_new_child('disk-type', self.parameters['disk_type']) |
| 179 |
disk_storage_info.add_child_elem(disk_inventory_info) |
| 180 |
|
| 181 |
disk_raid_info.add_new_child('container-type', 'spare') |
| 182 |
disk_storage_info.add_child_elem(disk_ownership_info) |
| 183 |
disk_storage_info.add_child_elem(disk_raid_info) |
| 184 |
|
| 185 |
disk_query.add_child_elem(disk_storage_info) |
| 186 |
disk_iter.add_child_elem(disk_query) |
| 187 |
|
| 188 |
try: |
| 189 |
result = self.server.invoke_successfully(disk_iter, True) |
| 190 |
except netapp_utils.zapi.NaApiError as error: |
| 191 |
self.module.fail_json(msg='Error getting disk information: %s' % (to_native(error)), |
| 192 |
exception=traceback.format_exc()) |
| 193 |
|
| 194 |
disks = [] |
| 195 |
|
| 196 |
if result.get_child_by_name('attributes-list'): |
| 197 |
attributes_list = result.get_child_by_name('attributes-list') |
| 198 |
storage_disk_info_attributes = attributes_list.get_children() |
| 199 |
|
| 200 |
for disk in storage_disk_info_attributes: |
| 201 |
disk_inventory_info = disk.get_child_by_name('disk-inventory-info') |
| 202 |
disk_name = disk_inventory_info.get_child_content('disk-cluster-name') |
| 203 |
disks.append(disk_name) |
| 204 |
|
| 205 |
return disks |
| 206 |
|
| 207 |
def get_partner_node_name(self): |
| 208 |
""" |
| 209 |
return: partner_node_name, str |
| 210 |
""" |
| 211 |
if self.use_rest: |
| 212 |
api = "/cluster/nodes" |
| 213 |
query = { |
| 214 |
'ha.partners.name': self.parameters['node'] |
| 215 |
} |
| 216 |
message, error = self.rest_api.get(api, query) |
| 217 |
records, error = rrh.check_for_0_or_more_records(api, message, error) |
| 218 |
if error: |
| 219 |
self.module.fail_json(msg=error) |
| 220 |
|
| 221 |
return records[0]['name'] if records else None |
| 222 |
|
| 223 |
else: |
| 224 |
partner_name = None |
| 225 |
cf_status = netapp_utils.zapi.NaElement('cf-status') |
| 226 |
cf_status.add_new_child('node', self.parameters['node']) |
| 227 |
|
| 228 |
try: |
| 229 |
result = self.server.invoke_successfully(cf_status, True) |
| 230 |
|
| 231 |
if result.get_child_by_name('partner-name'): |
| 232 |
partner_name = result.get_child_content('partner-name') |
| 233 |
|
| 234 |
except netapp_utils.zapi.NaApiError as error: |
| 235 |
self.module.fail_json(msg='Error getting partner name for node %s: %s' % (self.parameters['node'], to_native(error)), |
| 236 |
exception=traceback.format_exc()) |
| 237 |
|
| 238 |
return partner_name |
| 239 |
|
| 240 |
def disk_assign(self, needed_disks): |
| 241 |
""" |
| 242 |
Assign disks to node |
| 243 |
""" |
| 244 |
if self.use_rest: |
| 245 |
api = "private/cli/storage/disk/assign" |
| 246 |
if needed_disks > 0: |
| 247 |
body = { |
| 248 |
'owner': self.parameters['node'], |
| 249 |
'count': needed_disks |
| 250 |
} |
| 251 |
if 'disk_type' in self.parameters: |
| 252 |
body['type'] = self.parameters['disk_type'] |
| 253 |
else: |
| 254 |
body = { |
| 255 |
'node': self.parameters['node'], |
| 256 |
'all': True |
| 257 |
} |
| 258 |
|
| 259 |
dummy, error = self.rest_api.post(api, body) |
| 260 |
if error: |
| 261 |
self.module.fail_json(msg=error) |
| 262 |
|
| 263 |
else: |
| 264 |
if needed_disks > 0: |
| 265 |
assign_disk = netapp_utils.zapi.NaElement.create_node_with_children( |
| 266 |
'disk-sanown-assign', **{'owner': self.parameters['node'], |
| 267 |
'disk-count': str(needed_disks)}) |
| 268 |
if 'disk_type' in self.parameters: |
| 269 |
assign_disk.add_new_child('disk-type', self.parameters['disk_type']) |
| 270 |
else: |
| 271 |
assign_disk = netapp_utils.zapi.NaElement.create_node_with_children( |
| 272 |
'disk-sanown-assign', **{'node-name': self.parameters['node'], |
| 273 |
'all': 'true'}) |
| 274 |
|
| 275 |
try: |
| 276 |
self.server.invoke_successfully(assign_disk, |
| 277 |
enable_tunneling=True) |
| 278 |
return True |
| 279 |
except netapp_utils.zapi.NaApiError as error: |
| 280 |
self.module.fail_json(msg='Error assigning disks %s' % (to_native(error)), |
| 281 |
exception=traceback.format_exc()) |
| 282 |
|
| 283 |
def disk_unassign(self, disks): |
| 284 |
""" |
| 285 |
Unassign disks. |
| 286 |
Disk autoassign must be turned off when removing ownership of a disk |
| 287 |
""" |
| 288 |
if self.use_rest: |
| 289 |
api = "private/cli/storage/disk/removeowner" |
| 290 |
for disk in disks: # api requires 1 disk to be removed at a time. |
| 291 |
body = { |
| 292 |
'disk': disk['name'] |
| 293 |
} |
| 294 |
dummy, error = self.rest_api.post(api, body) |
| 295 |
if error: |
| 296 |
self.module.fail_json(msg=error) |
| 297 |
|
| 298 |
else: |
| 299 |
unassign_partitions = netapp_utils.zapi.NaElement('disk-sanown-remove-ownership') |
| 300 |
disk_list = netapp_utils.zapi.NaElement('disk-list') |
| 301 |
|
| 302 |
for disk in disks: |
| 303 |
disk_list.add_new_child('disk-name', disk) |
| 304 |
|
| 305 |
unassign_partitions.add_child_elem(disk_list) |
| 306 |
|
| 307 |
try: |
| 308 |
self.server.invoke_successfully(unassign_partitions, enable_tunneling=True) |
| 309 |
|
| 310 |
except netapp_utils.zapi.NaApiError as error: |
| 311 |
self.module.fail_json(msg='Error unassigning disks %s' % to_native(error)) |
| 312 |
return True |
| 313 |
|
| 314 |
def apply(self): |
| 315 |
'''Apply action to disks''' |
| 316 |
changed = False |
| 317 |
|
| 318 |
owned_disks = self.get_disks(container_type='owned', node=self.parameters['node']) |
| 319 |
owned_disks_count = len(owned_disks) |
| 320 |
unassigned_disks = self.get_disks(container_type='unassigned') |
| 321 |
owned_spare_disks = self.get_disks(container_type='spare', node=self.parameters['node']) |
| 322 |
|
| 323 |
needed_disks = None |
| 324 |
unassign = { |
| 325 |
'spare_disks': None, |
| 326 |
'unassign_disks': None |
| 327 |
} |
| 328 |
|
| 329 |
# unassign disks if more disks are currently owned than requested. |
| 330 |
if 'disk_count' in self.parameters: |
| 331 |
if self.parameters['disk_count'] < owned_disks_count: |
| 332 |
unassign_disks_count = owned_disks_count - self.parameters['disk_count'] |
| 333 |
# check to make sure we will have sufficient spares after the removal. |
| 334 |
if unassign_disks_count > (len(owned_spare_disks) - self.parameters['min_spares']): |
| 335 |
self.module.fail_json(msg="disk removal would leave less than %s spares" % self.parameters['min_spares']) |
| 336 |
# unassign disks. |
| 337 |
unassign = { |
| 338 |
'spare_disks': owned_spare_disks, |
| 339 |
'unassign_disks': unassign_disks_count |
| 340 |
} |
| 341 |
|
| 342 |
# take spare disks from partner so they can be reassigned to the desired node. |
| 343 |
elif self.parameters['disk_count'] > (owned_disks_count + len(unassigned_disks)): |
| 344 |
required_disks_count = self.parameters['disk_count'] - (owned_disks_count + len(unassigned_disks)) |
| 345 |
partner_node_name = self.get_partner_node_name() |
| 346 |
partner_spare_disks = self.get_disks(container_type='spare', node=partner_node_name) |
| 347 |
|
| 348 |
if required_disks_count > (len(partner_spare_disks) - self.parameters['min_spares']): |
| 349 |
self.module.fail_json(msg="not enough disks available") |
| 350 |
else: |
| 351 |
unassign = { |
| 352 |
'spare_disks': partner_spare_disks, |
| 353 |
'unassign_disks': required_disks_count |
| 354 |
} |
| 355 |
|
| 356 |
# assign disks to node. |
| 357 |
if self.parameters['disk_count'] > owned_disks_count: |
| 358 |
needed_disks = self.parameters['disk_count'] - owned_disks_count |
| 359 |
|
| 360 |
else: |
| 361 |
if len(unassigned_disks) >= 1: |
| 362 |
# assign all unassigned disks to node |
| 363 |
needed_disks = 0 |
| 364 |
|
| 365 |
# unassign |
| 366 |
if unassign['spare_disks'] and unassign['unassign_disks']: |
| 367 |
if not self.module.check_mode: |
| 368 |
self.disk_unassign(unassign['spare_disks'][0:unassign['unassign_disks']]) |
| 369 |
changed = True |
| 370 |
# assign |
| 371 |
if needed_disks is not None: |
| 372 |
if not self.module.check_mode: |
| 373 |
self.disk_assign(needed_disks) |
| 374 |
changed = True |
| 375 |
|
| 376 |
self.module.exit_json(changed=changed) |
| 377 |
|
| 378 |
|
| 379 |
def main(): |
| 380 |
''' Create object and call apply ''' |
| 381 |
obj_aggr = NetAppOntapDisks() |
| 382 |
obj_aggr.apply() |
| 383 |
|
| 384 |
|
| 385 |
if __name__ == '__main__': |
| 386 |
main() |