| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2021-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 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
module: na_ontap_disk_options |
| 12 |
short_description: NetApp ONTAP modify storage disk options |
| 13 |
extends_documentation_fragment: |
| 14 |
- netapp.ontap.netapp.na_ontap_rest |
| 15 |
version_added: '21.4.0' |
| 16 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 17 |
|
| 18 |
description: |
| 19 |
- Modify a nodes storage disk options |
| 20 |
- Requires ONTAP 9.6 or greater |
| 21 |
options: |
| 22 |
node: |
| 23 |
description: |
| 24 |
- The node to modify a disk option for |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
|
| 28 |
bkg_firmware_update: |
| 29 |
description: |
| 30 |
- Whether or not background disk firmware updates should be enabled |
| 31 |
type: bool |
| 32 |
|
| 33 |
autocopy: |
| 34 |
description: |
| 35 |
- Whether or not disk auto copies should be enabled |
| 36 |
type: bool |
| 37 |
|
| 38 |
autoassign: |
| 39 |
description: |
| 40 |
- Whether or not disks should be automatically assigned to a node |
| 41 |
type: bool |
| 42 |
|
| 43 |
autoassign_policy: |
| 44 |
description: |
| 45 |
- the auto assign policy to use |
| 46 |
type: str |
| 47 |
choices: ['default', 'bay', 'shelf', 'stack'] |
| 48 |
''' |
| 49 |
|
| 50 |
EXAMPLES = """ |
| 51 |
- name: Enable Disk Auto Assign |
| 52 |
netapp.ontap.na_ontap_disk_options: |
| 53 |
node: node1 |
| 54 |
autoassign: true |
| 55 |
username: "{{ netapp_username }}" |
| 56 |
password: "{{ netapp_password }}" |
| 57 |
hostname: "{{ netapp_hostname }}" |
| 58 |
|
| 59 |
- name: Disable Disk Auto Assign |
| 60 |
netapp.ontap.na_ontap_disk_options: |
| 61 |
node: node1 |
| 62 |
autoassign: false |
| 63 |
username: "{{ netapp_username }}" |
| 64 |
password: "{{ netapp_password }}" |
| 65 |
hostname: "{{ netapp_hostname }}" |
| 66 |
""" |
| 67 |
|
| 68 |
RETURN = """ |
| 69 |
|
| 70 |
""" |
| 71 |
|
| 72 |
from ansible.module_utils.basic import AnsibleModule |
| 73 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 74 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 75 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 76 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 77 |
|
| 78 |
|
| 79 |
class NetAppOntapDiskOptions: |
| 80 |
def __init__(self): |
| 81 |
|
| 82 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 83 |
self.argument_spec.update(dict( |
| 84 |
node=dict(required=True, type='str'), |
| 85 |
bkg_firmware_update=dict(required=False, type='bool'), |
| 86 |
autocopy=dict(required=False, type='bool'), |
| 87 |
autoassign=dict(required=False, type='bool'), |
| 88 |
autoassign_policy=dict(required=False, type='str', choices=['default', 'bay', 'shelf', 'stack']) |
| 89 |
)) |
| 90 |
|
| 91 |
self.module = AnsibleModule( |
| 92 |
argument_spec=self.argument_spec, |
| 93 |
supports_check_mode=True |
| 94 |
) |
| 95 |
|
| 96 |
self.na_helper = NetAppModule() |
| 97 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 98 |
|
| 99 |
self.rest_api = OntapRestAPI(self.module) |
| 100 |
self.use_rest = self.rest_api.is_rest() |
| 101 |
|
| 102 |
if not self.use_rest: |
| 103 |
self.module.fail_json(msg=self.rest_api.requires_ontap_version('na_ontap_disk_options', '9.6')) |
| 104 |
|
| 105 |
def convert_to_bool(self, adict, key): |
| 106 |
"""burt1468160 - 9.8 returns True/False, but 9.10.1 returns 'on'/'off' """ |
| 107 |
value = adict[key] |
| 108 |
if isinstance(value, bool): |
| 109 |
return value |
| 110 |
if value in ('on', 'off'): |
| 111 |
return value == 'on' |
| 112 |
self.module.fail_json(msg='Unexpected value for field %s: %s' % (key, value)) |
| 113 |
|
| 114 |
def get_disk_options(self): |
| 115 |
""" |
| 116 |
Return a the current storage disk options for the node |
| 117 |
:return: a dict of storage disk options |
| 118 |
""" |
| 119 |
api = "private/cli/storage/disk/option" |
| 120 |
query = { |
| 121 |
'fields': 'node,autoassign,bkg-firmware-update,autocopy,autoassign-policy', |
| 122 |
'node': self.parameters['node'] |
| 123 |
} |
| 124 |
record, error = rest_generic.get_one_record(self.rest_api, api, query) |
| 125 |
|
| 126 |
if error: |
| 127 |
self.module.fail_json(msg='Error %s' % error) |
| 128 |
if record is None: |
| 129 |
self.module.fail_json(msg='Error on GET %s, no record.' % api) |
| 130 |
return { |
| 131 |
'node': record['node'], |
| 132 |
'bkg_firmware_update': self.convert_to_bool(record, 'bkg_firmware_update'), |
| 133 |
'autocopy': self.convert_to_bool(record, 'autocopy'), |
| 134 |
'autoassign': self.convert_to_bool(record, 'autoassign'), |
| 135 |
'autoassign_policy': record['autoassign_policy'] |
| 136 |
} |
| 137 |
|
| 138 |
def modify_disk_options(self, modify): |
| 139 |
""" |
| 140 |
Modifies a nodes disk options |
| 141 |
:return: None |
| 142 |
""" |
| 143 |
|
| 144 |
api = "private/cli/storage/disk/option" |
| 145 |
query = { |
| 146 |
'node': self.parameters['node'] |
| 147 |
} |
| 148 |
|
| 149 |
dummy, error = rest_generic.patch_async(self.rest_api, api, None, modify, query) |
| 150 |
if error: |
| 151 |
self.module.fail_json(msg='Error %s' % error) |
| 152 |
|
| 153 |
def apply(self): |
| 154 |
|
| 155 |
current = self.get_disk_options() |
| 156 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 157 |
|
| 158 |
if self.na_helper.changed and not self.module.check_mode: |
| 159 |
self.modify_disk_options(modify) |
| 160 |
|
| 161 |
result = netapp_utils.generate_result(self.na_helper.changed, modify=modify) |
| 162 |
self.module.exit_json(**result) |
| 163 |
|
| 164 |
|
| 165 |
def main(): |
| 166 |
""" |
| 167 |
Execute action from playbook |
| 168 |
""" |
| 169 |
command = NetAppOntapDiskOptions() |
| 170 |
command.apply() |
| 171 |
|
| 172 |
|
| 173 |
if __name__ == '__main__': |
| 174 |
main() |