| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2021-2024, 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_partitions |
| 12 |
|
| 13 |
short_description: NetApp ONTAP Assign partitions and disks to nodes. |
| 14 |
extends_documentation_fragment: |
| 15 |
- netapp.ontap.netapp.na_ontap_rest |
| 16 |
version_added: 21.8.0 |
| 17 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 18 |
|
| 19 |
description: |
| 20 |
- Assign the specified number of partitions or disks eligible for partitioning to a node. |
| 21 |
- There is some overlap between this module and the na_ontap_disks module. |
| 22 |
- If you don't have ADP v1 or v2 then you should be using the na_ontap_disks module to assign whole disks. |
| 23 |
- Partitions/disks are added in the following order |
| 24 |
- 1. Any unassigned partitions are added. |
| 25 |
- 2. Any unassigned disks of the correct type are added and will be partitioned when added to an aggregate if required. |
| 26 |
- 3. Any spare partner partitions will be re-assigned. |
| 27 |
- 4. Any partner spare disks will be re-assigned and be partitioned when added to an aggregate. |
| 28 |
- If you specify a partition_count less than the current number of partitions, then spare partitions will be unassigned. |
| 29 |
- If a previously partitioned disk has the partitions removed, and even if it is "slow zeroed" the system \ |
| 30 |
will consider it a shared partitioned disk rather than a spare. |
| 31 |
- In a root-data-data configuration (ADPv2) if you specify data1 as the partition_type then only P1 partitions will be counted. |
| 32 |
- Disk autoassign must be turned off before using this module to prevent the disks being reassigned automatically by the cluster. |
| 33 |
- This can be done through na_ontap_disk_options or via the cli "disk option modify -node <node_name> -autoassign off". |
| 34 |
|
| 35 |
options: |
| 36 |
node: |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
description: |
| 40 |
- Specifies the node that the partitions and disks should be assigned to. |
| 41 |
|
| 42 |
partition_count: |
| 43 |
required: true |
| 44 |
type: int |
| 45 |
description: |
| 46 |
- Total number of partitions that should be assigned to the owner. |
| 47 |
|
| 48 |
disk_type: |
| 49 |
required: true |
| 50 |
choices: ["ATA", "BSAS", "FCAL", "FSAS", "LUN", "MSATA", "SAS", "SSD", "SSD_NVM", "VMDISK", "unknown"] |
| 51 |
type: str |
| 52 |
description: |
| 53 |
- The type of disk that the partitions that should use. |
| 54 |
|
| 55 |
partition_type: |
| 56 |
required: true |
| 57 |
choices: ["data", "root", "data1", "data2"] |
| 58 |
type: str |
| 59 |
description: |
| 60 |
- The type of partiton being assigned either root, data, data1 or data2, |
| 61 |
|
| 62 |
partitioning_method: |
| 63 |
required: true |
| 64 |
choices: ["root_data", "root_data1_data2"] |
| 65 |
type: str |
| 66 |
description: |
| 67 |
- The type of partiton method being used, either root_data or root_data1_data2. |
| 68 |
|
| 69 |
min_spares: |
| 70 |
description: |
| 71 |
- Minimum spares disks or partitions required per type for the node. |
| 72 |
type: int |
| 73 |
|
| 74 |
''' |
| 75 |
|
| 76 |
EXAMPLES = """ |
| 77 |
- name: Assign specified total partitions to node cluster-01 |
| 78 |
netapp.ontap.na_ontap_partitions: |
| 79 |
node: cluster-01 |
| 80 |
partition_count: 56 |
| 81 |
disk_type: SSD |
| 82 |
partition_type: data1 |
| 83 |
partition_method: root_data1_data2 |
| 84 |
hostname: "{{ netapp_hostname }}" |
| 85 |
username: "{{ netapp_username }}" |
| 86 |
password: "{{ netapp_password }}" |
| 87 |
""" |
| 88 |
|
| 89 |
RETURN = """ |
| 90 |
""" |
| 91 |
|
| 92 |
from ansible.module_utils.basic import AnsibleModule |
| 93 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 94 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 95 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 96 |
import ansible_collections.netapp.ontap.plugins.module_utils.rest_response_helpers as rrh |
| 97 |
|
| 98 |
|
| 99 |
class NetAppOntapPartitions(): |
| 100 |
''' object initialize and class methods ''' |
| 101 |
|
| 102 |
def __init__(self): |
| 103 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 104 |
self.argument_spec.update(dict( |
| 105 |
node=dict(required=True, type='str'), |
| 106 |
partition_count=dict(required=True, type='int'), |
| 107 |
disk_type=dict(required=True, type='str', choices=['ATA', 'BSAS', 'FCAL', 'FSAS', 'LUN', 'MSATA', 'SAS', 'SSD', 'SSD_NVM', 'VMDISK', 'unknown']), |
| 108 |
partition_type=dict(required=True, type='str', choices=['data1', 'data2', 'data', 'root']), |
| 109 |
partitioning_method=dict(required=True, type='str', choices=['root_data1_data2', 'root_data']), |
| 110 |
min_spares=dict(required=False, type='int') |
| 111 |
)) |
| 112 |
|
| 113 |
self.module = AnsibleModule( |
| 114 |
argument_spec=self.argument_spec, |
| 115 |
supports_check_mode=True |
| 116 |
) |
| 117 |
|
| 118 |
self.na_helper = NetAppModule() |
| 119 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 120 |
|
| 121 |
# If min_spares is not specified min_spares is 1 if SSD, min_spares is 2 for any other disk type. |
| 122 |
if 'min_spares' not in self.parameters: |
| 123 |
if self.parameters['disk_type'] in ('SSD', 'SSD_NVM'): |
| 124 |
self.parameters['min_spares'] = 1 |
| 125 |
else: |
| 126 |
self.parameters['min_spares'] = 2 |
| 127 |
|
| 128 |
self.rest_api = OntapRestAPI(self.module) |
| 129 |
self.use_rest = self.rest_api.is_rest() |
| 130 |
|
| 131 |
if not self.use_rest: |
| 132 |
self.module.fail_json(msg=self.rest_api.requires_ontap_version('na_ontap_partitions', '9.6')) |
| 133 |
|
| 134 |
def get_disks(self, container_type, node=None): |
| 135 |
""" |
| 136 |
Check for owned disks, unassigned disks or spare disks. |
| 137 |
Return: list of disks or an empty list |
| 138 |
""" |
| 139 |
api = 'storage/disks' |
| 140 |
|
| 141 |
if container_type == 'unassigned': |
| 142 |
query = { |
| 143 |
'container_type': 'unassigned', |
| 144 |
'type': self.parameters['disk_type'], |
| 145 |
'fields': 'name' |
| 146 |
} |
| 147 |
if container_type == 'spare': |
| 148 |
query = { |
| 149 |
'home_node.name': node, |
| 150 |
'container_type': 'spare', |
| 151 |
'type': self.parameters['disk_type'], |
| 152 |
'fields': 'name' |
| 153 |
} |
| 154 |
message, error = self.rest_api.get(api, query) |
| 155 |
records, error = rrh.check_for_0_or_more_records(api, message, error) |
| 156 |
|
| 157 |
if error: |
| 158 |
self.module.fail_json(msg=error) |
| 159 |
|
| 160 |
return records if records else list() |
| 161 |
|
| 162 |
def get_partitions(self, container_type, node=None): |
| 163 |
""" |
| 164 |
Get partitions info |
| 165 |
Return: list of partitions of a specified container type or None. |
| 166 |
""" |
| 167 |
api = 'private/cli/storage/disk/partition' |
| 168 |
|
| 169 |
query = {} |
| 170 |
|
| 171 |
if container_type == 'spare': |
| 172 |
query = { |
| 173 |
'fields': 'partition,container-type,disk-type,partitioning-method,home-node-name,is-root,owner-node-name', |
| 174 |
'home-node-name': node, |
| 175 |
'disk-type': self.parameters['disk_type'], |
| 176 |
'container-type': 'spare', |
| 177 |
'partitioning-method': self.parameters['partitioning_method'] |
| 178 |
} |
| 179 |
if container_type == 'unassigned': |
| 180 |
query = { |
| 181 |
'fields': 'partition,container-type,disk-type,partitioning-method,home-node-name,is-root,owner-node-name', |
| 182 |
'nodelist': node, |
| 183 |
'disk-type': self.parameters['disk_type'], |
| 184 |
'container-type': 'unassigned' |
| 185 |
} |
| 186 |
if container_type == 'owner': |
| 187 |
query = { |
| 188 |
'fields': 'partition,container-type,disk-type,partitioning-method,home-node-name,is-root,owner-node-name', |
| 189 |
'home-node-name': node, |
| 190 |
'disk-type': self.parameters['disk_type'], |
| 191 |
'partitioning-method': self.parameters['partitioning_method'] |
| 192 |
} |
| 193 |
|
| 194 |
if self.parameters['partition_type'] == 'root': |
| 195 |
query['is-root'] = True |
| 196 |
else: |
| 197 |
query['is-root'] = False |
| 198 |
|
| 199 |
message, error = self.rest_api.get(api, query) |
| 200 |
records, error = rrh.check_for_0_or_more_records(api, message, error) |
| 201 |
if error: |
| 202 |
self.module.fail_json(msg=error) |
| 203 |
|
| 204 |
if records: |
| 205 |
if self.parameters['partitioning_method'] == 'root_data1_data2': |
| 206 |
# get just the P1 or P2 partitions |
| 207 |
data_partitions = [] |
| 208 |
for record in records: |
| 209 |
if self.parameters['partition_type'] == 'data1' and record['partition'].endswith('P1'): |
| 210 |
data_partitions.append(record) |
| 211 |
elif self.parameters['partition_type'] == 'data2' and record['partition'].endswith('P2'): |
| 212 |
data_partitions.append(record) |
| 213 |
return data_partitions |
| 214 |
|
| 215 |
return records |
| 216 |
else: |
| 217 |
return list() |
| 218 |
|
| 219 |
def get_partner_node_name(self): |
| 220 |
""" |
| 221 |
return: partner_node_name, str |
| 222 |
""" |
| 223 |
api = 'cluster/nodes' |
| 224 |
query = { |
| 225 |
'ha.partners.name': self.parameters['node'] |
| 226 |
} |
| 227 |
message, error = self.rest_api.get(api, query) |
| 228 |
records, error = rrh.check_for_0_or_more_records(api, message, error) |
| 229 |
|
| 230 |
if error: |
| 231 |
self.module.fail_json(msg=error) |
| 232 |
|
| 233 |
return records[0]['name'] if records else None |
| 234 |
|
| 235 |
def assign_disks(self, disks): |
| 236 |
""" |
| 237 |
Assign disks to node |
| 238 |
""" |
| 239 |
api = 'private/cli/storage/disk/assign' |
| 240 |
for disk in disks: |
| 241 |
body = { |
| 242 |
'owner': self.parameters['node'], |
| 243 |
'disk': disk['name'] |
| 244 |
} |
| 245 |
|
| 246 |
dummy, error = self.rest_api.post(api, body) |
| 247 |
if error: |
| 248 |
self.module.fail_json(msg=error) |
| 249 |
|
| 250 |
def unassign_disks(self, disks): |
| 251 |
""" |
| 252 |
Unassign disks. |
| 253 |
Disk autoassign must be turned off when removing ownership of a disk |
| 254 |
""" |
| 255 |
api = 'private/cli/storage/disk/removeowner' |
| 256 |
for disk in disks: # api requires 1 disk to be removed at a time. |
| 257 |
body = { |
| 258 |
'disk': disk['name'] |
| 259 |
} |
| 260 |
dummy, error = self.rest_api.post(api, body) |
| 261 |
if error: |
| 262 |
self.module.fail_json(msg=error) |
| 263 |
|
| 264 |
def assign_partitions(self, required_partitions): |
| 265 |
""" |
| 266 |
Assign partitions to node |
| 267 |
""" |
| 268 |
api = 'private/cli/storage/disk/partition/assign' |
| 269 |
for required_partition in required_partitions: |
| 270 |
body = { |
| 271 |
'owner': self.parameters['node'], |
| 272 |
'partition': required_partition['partition'] |
| 273 |
} |
| 274 |
|
| 275 |
dummy, error = self.rest_api.post(api, body) |
| 276 |
if error: |
| 277 |
self.module.fail_json(msg=error) |
| 278 |
|
| 279 |
def unassign_partitions(self, required_partitions): |
| 280 |
""" |
| 281 |
Unassign partitions from node |
| 282 |
""" |
| 283 |
api = 'private/cli/storage/disk/partition/removeowner' |
| 284 |
for required_partition in required_partitions: |
| 285 |
body = { |
| 286 |
'partition': required_partition['partition'] |
| 287 |
} |
| 288 |
|
| 289 |
dummy, error = self.rest_api.post(api, body) |
| 290 |
if error: |
| 291 |
self.module.fail_json(msg=error) |
| 292 |
|
| 293 |
def determine_assignment(self, owned_partitions, own_spare_disks): |
| 294 |
""" |
| 295 |
Determine which action to take |
| 296 |
return: dict containing lists of the disks/partitions to be assigned/reassigned |
| 297 |
""" |
| 298 |
# build dict of partitions and disks to be unassigned/assigned |
| 299 |
assignment = { |
| 300 |
'required_unassigned_partitions': [], |
| 301 |
'required_partner_spare_disks': [], |
| 302 |
'required_partner_spare_partitions': [], |
| 303 |
'required_unassigned_disks': [] |
| 304 |
} |
| 305 |
|
| 306 |
unassigned_partitions = self.get_partitions(container_type='owned', node=self.parameters['node']) |
| 307 |
required_partitions = self.parameters['partition_count'] - (len(owned_partitions) + len(own_spare_disks)) |
| 308 |
|
| 309 |
# are there enough unassigned partitions to meet the requirement? |
| 310 |
if required_partitions > len(unassigned_partitions): |
| 311 |
assignment['required_unassigned_partitions'] = unassigned_partitions |
| 312 |
# check for unassigned disks |
| 313 |
unassigned_disks = self.get_disks(container_type='spare') |
| 314 |
required_unassigned_disks = required_partitions - len(unassigned_partitions) |
| 315 |
if required_unassigned_disks > len(unassigned_disks): |
| 316 |
assignment['required_unassigned_disks'] = unassigned_disks |
| 317 |
# not enough unassigned disks |
| 318 |
required_partner_spare_partitions = required_unassigned_disks - len(unassigned_disks) |
| 319 |
partner_node_name = self.get_partner_node_name() |
| 320 |
if partner_node_name: |
| 321 |
partner_spare_partitions = self.get_partitions(container_type='spare', node=partner_node_name) |
| 322 |
partner_spare_disks = self.get_disks(container_type='spare', node=partner_node_name) |
| 323 |
else: |
| 324 |
partner_spare_partitions = [] |
| 325 |
partner_spare_disks = [] |
| 326 |
# check for partner spare partitions |
| 327 |
if required_partner_spare_partitions <= (len(partner_spare_partitions) - self.parameters['min_spares']): |
| 328 |
# we have enough spare partitions and dont need any disks |
| 329 |
assignment['required_partner_spare_partitions'] = partner_spare_partitions[0:required_partner_spare_partitions] |
| 330 |
else: |
| 331 |
# we don't know if we can take all of the spare partitions as we don't know how many spare disks there are |
| 332 |
# spare partions != spare disks |
| 333 |
if len(partner_spare_disks) >= self.parameters['min_spares']: |
| 334 |
# we have enough spare disks so can use all spare partitions if required |
| 335 |
if required_partner_spare_partitions <= len(partner_spare_partitions): |
| 336 |
# now we know we have spare disks we can take as may spare partitions as we need |
| 337 |
assignment['required_partner_spare_partitions'] = partner_spare_partitions[0:required_partner_spare_partitions] |
| 338 |
else: |
| 339 |
# we need to take some spare disks as well as using any spare partitions |
| 340 |
required_partner_spare_disks = required_partner_spare_partitions - len(partner_spare_partitions) |
| 341 |
required_partner_spare_partitions_count = required_partner_spare_partitions - required_partner_spare_disks |
| 342 |
assignment['required_partner_spare_partitions'] = partner_spare_partitions[0:required_partner_spare_partitions_count] |
| 343 |
if required_partner_spare_disks > len(partner_spare_disks) - self.parameters['min_spares']: |
| 344 |
self.module.fail_json(msg='Not enough partner spare disks or partner spare partitions remain to fulfill the request') |
| 345 |
else: |
| 346 |
assignment['required_partner_spare_disks'] = partner_spare_disks[0:required_partner_spare_disks] |
| 347 |
else: |
| 348 |
self.module.fail_json(msg='Not enough partner spare disks or partner spare partitions remain to fulfill the request') |
| 349 |
else: |
| 350 |
assignment['required_unassigned_disks'] = unassigned_disks[0:required_unassigned_disks] |
| 351 |
else: |
| 352 |
assignment['required_unassigned_partitions'] = unassigned_partitions[0:required_partitions] |
| 353 |
|
| 354 |
return assignment |
| 355 |
|
| 356 |
def apply(self): |
| 357 |
'''Apply action to partitions''' |
| 358 |
changed = False |
| 359 |
|
| 360 |
owned_partitions = self.get_partitions(container_type='owned', node=self.parameters['node']) |
| 361 |
own_spare_disks = self.get_disks(container_type='spare', node=self.parameters['node']) |
| 362 |
|
| 363 |
# are the required partitions more than the currently owned partitions and spare disks, if so we need to assign |
| 364 |
if self.parameters['partition_count'] > (len(own_spare_disks) + len(owned_partitions)): |
| 365 |
# which action needs to be taken |
| 366 |
assignment = self.determine_assignment(owned_partitions=owned_partitions, own_spare_disks=own_spare_disks) |
| 367 |
|
| 368 |
# now that we have calculated where the partitions and disks come from we can take action |
| 369 |
if len(assignment['required_unassigned_partitions']) > 0: |
| 370 |
changed = True |
| 371 |
if not self.module.check_mode: |
| 372 |
self.assign_partitions(assignment['required_unassigned_partitions']) |
| 373 |
|
| 374 |
if len(assignment['required_unassigned_disks']) > 0: |
| 375 |
changed = True |
| 376 |
if not self.module.check_mode: |
| 377 |
self.assign_disks(assignment['required_unassigned_disks']) |
| 378 |
|
| 379 |
if len(assignment['required_partner_spare_partitions']) > 0: |
| 380 |
changed = True |
| 381 |
if not self.module.check_mode: |
| 382 |
self.unassign_partitions(assignment['required_partner_spare_partitions']) |
| 383 |
self.assign_partitions(assignment['required_partner_spare_partitions']) |
| 384 |
|
| 385 |
if len(assignment['required_partner_spare_disks']) > 0: |
| 386 |
changed = True |
| 387 |
if not self.module.check_mode: |
| 388 |
self.unassign_disks(assignment['required_partner_spare_disks']) |
| 389 |
self.assign_disks(assignment['required_partner_spare_disks']) |
| 390 |
|
| 391 |
# unassign |
| 392 |
elif self.parameters['partition_count'] < len(owned_partitions): |
| 393 |
spare_partitions = self.get_partitions(container_type='spare', node=self.parameters['node']) |
| 394 |
unassign_partitions = len(owned_partitions) - self.parameters['partition_count'] |
| 395 |
|
| 396 |
if unassign_partitions > len(spare_partitions): |
| 397 |
self.module.fail_json(msg='Not enough spare partitions exist fulfill the partition unassignment request') |
| 398 |
elif (len(spare_partitions) - unassign_partitions + len(own_spare_disks)) < self.parameters['min_spares']: |
| 399 |
self.module.fail_json(msg='Unassignment of specified partitions would leave node with less than the minimum number of spares') |
| 400 |
else: |
| 401 |
changed = True |
| 402 |
if not self.module.check_mode: |
| 403 |
self.unassign_partitions(spare_partitions[0:unassign_partitions]) |
| 404 |
|
| 405 |
self.module.exit_json(changed=changed) |
| 406 |
|
| 407 |
|
| 408 |
def main(): |
| 409 |
''' Create object and call apply ''' |
| 410 |
obj_aggr = NetAppOntapPartitions() |
| 411 |
obj_aggr.apply() |
| 412 |
|
| 413 |
|
| 414 |
if __name__ == '__main__': |
| 415 |
main() |