| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2017-2026, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ |
| 5 |
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
''' |
| 8 |
na_ontap_iscsi |
| 9 |
''' |
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
|
| 14 |
DOCUMENTATION = ''' |
| 15 |
|
| 16 |
module: na_ontap_iscsi |
| 17 |
short_description: NetApp ONTAP manage iSCSI service |
| 18 |
extends_documentation_fragment: |
| 19 |
- netapp.ontap.netapp.na_ontap |
| 20 |
version_added: 2.6.0 |
| 21 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 22 |
|
| 23 |
description: |
| 24 |
- create, delete, start, stop iSCSI service on SVM. |
| 25 |
|
| 26 |
options: |
| 27 |
|
| 28 |
state: |
| 29 |
description: |
| 30 |
- Whether the service should be present or deleted. |
| 31 |
choices: ['present', 'absent'] |
| 32 |
type: str |
| 33 |
default: present |
| 34 |
|
| 35 |
service_state: |
| 36 |
description: |
| 37 |
- Whether the specified service should running. |
| 38 |
choices: ['started', 'stopped'] |
| 39 |
type: str |
| 40 |
|
| 41 |
vserver: |
| 42 |
required: true |
| 43 |
type: str |
| 44 |
description: |
| 45 |
- The name of the vserver to use. |
| 46 |
|
| 47 |
target_alias: |
| 48 |
type: str |
| 49 |
description: |
| 50 |
- The iSCSI target alias of the iSCSI service. |
| 51 |
- The target alias can contain one (1) to 128 characters and feature any printable character except space (" "). |
| 52 |
- A PATCH request with an empty alias ("") clears the alias. |
| 53 |
- This option is REST only. |
| 54 |
version_added: 22.2.0 |
| 55 |
|
| 56 |
lambda_config: |
| 57 |
description: |
| 58 |
- Configuration parameters for AWS Lambda proxy functionality. |
| 59 |
- These option and suboptions are only supported with REST. |
| 60 |
type: dict |
| 61 |
version_added: 23.5.0 |
| 62 |
suboptions: |
| 63 |
function_name: |
| 64 |
description: |
| 65 |
- The name of the AWS Lambda function to invoke. |
| 66 |
type: str |
| 67 |
required: true |
| 68 |
aws_region: |
| 69 |
description: |
| 70 |
- The name of the AWS region. |
| 71 |
type: str |
| 72 |
required: true |
| 73 |
aws_profile: |
| 74 |
description: |
| 75 |
- The name of the AWS profile to use for authentication. |
| 76 |
type: str |
| 77 |
|
| 78 |
notes: |
| 79 |
- Supports AWS Lambda proxy functionality when using REST. See README for example usage. |
| 80 |
|
| 81 |
''' |
| 82 |
|
| 83 |
EXAMPLES = """ |
| 84 |
- name: Create iscsi service |
| 85 |
netapp.ontap.na_ontap_iscsi: |
| 86 |
state: present |
| 87 |
service_state: started |
| 88 |
vserver: ansibleVServer |
| 89 |
target_alias: ansibleSVM |
| 90 |
hostname: "{{ netapp_hostname }}" |
| 91 |
username: "{{ netapp_username }}" |
| 92 |
password: "{{ netapp_password }}" |
| 93 |
|
| 94 |
- name: Stop Iscsi service |
| 95 |
netapp.ontap.na_ontap_iscsi: |
| 96 |
state: present |
| 97 |
service_state: stopped |
| 98 |
vserver: ansibleVServer |
| 99 |
hostname: "{{ netapp_hostname }}" |
| 100 |
username: "{{ netapp_username }}" |
| 101 |
password: "{{ netapp_password }}" |
| 102 |
|
| 103 |
- name: Delete Iscsi service |
| 104 |
netapp.ontap.na_ontap_iscsi: |
| 105 |
state: absent |
| 106 |
vserver: ansibleVServer |
| 107 |
hostname: "{{ netapp_hostname }}" |
| 108 |
username: "{{ netapp_username }}" |
| 109 |
password: "{{ netapp_password }}" |
| 110 |
""" |
| 111 |
|
| 112 |
RETURN = """ |
| 113 |
|
| 114 |
""" |
| 115 |
|
| 116 |
import traceback |
| 117 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 118 |
from ansible.module_utils.basic import AnsibleModule |
| 119 |
from ansible.module_utils._text import to_native |
| 120 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 121 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 122 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 123 |
|
| 124 |
|
| 125 |
class NetAppOntapISCSI: |
| 126 |
|
| 127 |
def __init__(self): |
| 128 |
|
| 129 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 130 |
self.argument_spec.update(dict( |
| 131 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 132 |
service_state=dict(required=False, type='str', choices=['started', 'stopped'], default=None), |
| 133 |
vserver=dict(required=True, type='str'), |
| 134 |
target_alias=dict(required=False, type='str') |
| 135 |
)) |
| 136 |
self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec()) |
| 137 |
|
| 138 |
self.module = AnsibleModule( |
| 139 |
argument_spec=self.argument_spec, |
| 140 |
required_if=[ |
| 141 |
('use_lambda', True, ['lambda_config']), |
| 142 |
], |
| 143 |
supports_check_mode=True |
| 144 |
) |
| 145 |
self.uuid = None |
| 146 |
self.na_helper = NetAppModule() |
| 147 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 148 |
|
| 149 |
# Set up Rest API |
| 150 |
self.rest_api = OntapRestAPI(self.module) |
| 151 |
self.use_rest = self.rest_api.is_rest() |
| 152 |
self.unsupported_zapi_properties = ['target_alias'] |
| 153 |
if not self.use_rest: |
| 154 |
if self.parameters.get('use_lambda'): |
| 155 |
self.module.fail_json(msg="Error: AWS Lambda proxy for ONTAP APIs is only supported with REST.") |
| 156 |
if not netapp_utils.has_netapp_lib(): |
| 157 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 158 |
for unsupported_zapi_property in self.unsupported_zapi_properties: |
| 159 |
if self.parameters.get(unsupported_zapi_property) is not None: |
| 160 |
msg = "Error: %s option is not supported with ZAPI. It can only be used with REST." % unsupported_zapi_property |
| 161 |
self.module.fail_json(msg=msg) |
| 162 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 163 |
self.safe_strip() |
| 164 |
|
| 165 |
def safe_strip(self): |
| 166 |
""" strip the left and right spaces of string """ |
| 167 |
if 'target_alias' in self.parameters: |
| 168 |
self.parameters['target_alias'] = self.parameters['target_alias'].strip() |
| 169 |
return |
| 170 |
|
| 171 |
def get_iscsi(self): |
| 172 |
""" |
| 173 |
Return details about the iscsi service |
| 174 |
|
| 175 |
:return: Details about the iscsi service |
| 176 |
:rtype: dict |
| 177 |
""" |
| 178 |
if self.use_rest: |
| 179 |
return self.get_iscsi_rest() |
| 180 |
iscsi_info = netapp_utils.zapi.NaElement('iscsi-service-get-iter') |
| 181 |
iscsi_attributes = netapp_utils.zapi.NaElement('iscsi-service-info') |
| 182 |
|
| 183 |
iscsi_attributes.add_new_child('vserver', self.parameters['vserver']) |
| 184 |
|
| 185 |
query = netapp_utils.zapi.NaElement('query') |
| 186 |
query.add_child_elem(iscsi_attributes) |
| 187 |
|
| 188 |
iscsi_info.add_child_elem(query) |
| 189 |
try: |
| 190 |
result = self.server.invoke_successfully(iscsi_info, True) |
| 191 |
except netapp_utils.zapi.NaApiError as e: |
| 192 |
self.module.fail_json(msg="Error finding iscsi service in %s: %s" % (self.parameters['vserver'], to_native(e)), |
| 193 |
exception=traceback.format_exc()) |
| 194 |
return_value = None |
| 195 |
|
| 196 |
if result.get_child_by_name('num-records') and \ |
| 197 |
int(result.get_child_content('num-records')) >= 1: |
| 198 |
|
| 199 |
iscsi = result.get_child_by_name( |
| 200 |
'attributes-list').get_child_by_name('iscsi-service-info') |
| 201 |
if iscsi: |
| 202 |
is_started = 'started' if iscsi.get_child_content('is-available') == 'true' else 'stopped' |
| 203 |
return_value = { |
| 204 |
'service_state': is_started |
| 205 |
} |
| 206 |
return return_value |
| 207 |
|
| 208 |
def create_iscsi_service(self): |
| 209 |
""" |
| 210 |
Create iscsi service and start if requested |
| 211 |
""" |
| 212 |
if self.use_rest: |
| 213 |
return self.create_iscsi_service_rest() |
| 214 |
|
| 215 |
iscsi_service = netapp_utils.zapi.NaElement.create_node_with_children( |
| 216 |
'iscsi-service-create', |
| 217 |
**{'start': 'true' if self.parameters.get('service_state', 'started') == 'started' else 'false' |
| 218 |
}) |
| 219 |
|
| 220 |
try: |
| 221 |
self.server.invoke_successfully(iscsi_service, enable_tunneling=True) |
| 222 |
except netapp_utils.zapi.NaApiError as e: |
| 223 |
self.module.fail_json(msg="Error creating iscsi service: % s" % (to_native(e)), |
| 224 |
exception=traceback.format_exc()) |
| 225 |
|
| 226 |
def delete_iscsi_service(self, current): |
| 227 |
""" |
| 228 |
Delete the iscsi service |
| 229 |
""" |
| 230 |
if self.use_rest: |
| 231 |
return self.delete_iscsi_service_rest(current) |
| 232 |
|
| 233 |
if current['service_state'] == 'started': |
| 234 |
self.stop_iscsi_service() |
| 235 |
|
| 236 |
iscsi_delete = netapp_utils.zapi.NaElement.create_node_with_children('iscsi-service-destroy') |
| 237 |
|
| 238 |
try: |
| 239 |
self.server.invoke_successfully(iscsi_delete, enable_tunneling=True) |
| 240 |
except netapp_utils.zapi.NaApiError as e: |
| 241 |
self.module.fail_json(msg="Error deleting iscsi service on vserver %s: %s" % (self.parameters['vserver'], to_native(e)), |
| 242 |
exception=traceback.format_exc()) |
| 243 |
|
| 244 |
def stop_iscsi_service(self): |
| 245 |
""" |
| 246 |
Stop iscsi service |
| 247 |
""" |
| 248 |
|
| 249 |
iscsi_stop = netapp_utils.zapi.NaElement.create_node_with_children('iscsi-service-stop') |
| 250 |
|
| 251 |
try: |
| 252 |
self.server.invoke_successfully(iscsi_stop, enable_tunneling=True) |
| 253 |
except netapp_utils.zapi.NaApiError as e: |
| 254 |
self.module.fail_json(msg="Error Stopping iscsi service on vserver %s: %s" % (self.parameters['vserver'], to_native(e)), |
| 255 |
exception=traceback.format_exc()) |
| 256 |
|
| 257 |
def start_iscsi_service(self): |
| 258 |
""" |
| 259 |
Start iscsi service |
| 260 |
""" |
| 261 |
iscsi_start = netapp_utils.zapi.NaElement.create_node_with_children( |
| 262 |
'iscsi-service-start') |
| 263 |
|
| 264 |
try: |
| 265 |
self.server.invoke_successfully(iscsi_start, enable_tunneling=True) |
| 266 |
except netapp_utils.zapi.NaApiError as e: |
| 267 |
self.module.fail_json(msg="Error starting iscsi service on vserver %s: %s" % (self.parameters['vserver'], to_native(e)), |
| 268 |
exception=traceback.format_exc()) |
| 269 |
|
| 270 |
def get_iscsi_rest(self): |
| 271 |
api = 'protocols/san/iscsi/services' |
| 272 |
query = {'svm.name': self.parameters['vserver']} |
| 273 |
fields = 'svm,enabled,target.alias' |
| 274 |
record, error = rest_generic.get_one_record(self.rest_api, api, query, fields) |
| 275 |
if error: |
| 276 |
self.module.fail_json(msg="Error finding iscsi service in %s: %s" % (self.parameters['vserver'], error)) |
| 277 |
if record: |
| 278 |
self.uuid = record['svm']['uuid'] |
| 279 |
is_started = 'started' if record['enabled'] else 'stopped' |
| 280 |
return { |
| 281 |
'service_state': is_started, |
| 282 |
'target_alias': "" if self.na_helper.safe_get(record, ['target', 'alias']) is None else record['target']['alias'], |
| 283 |
} |
| 284 |
return None |
| 285 |
|
| 286 |
def create_iscsi_service_rest(self): |
| 287 |
api = 'protocols/san/iscsi/services' |
| 288 |
body = { |
| 289 |
'svm.name': self.parameters['vserver'], |
| 290 |
'enabled': True if self.parameters.get('service_state', 'started') == 'started' else False |
| 291 |
} |
| 292 |
if 'target_alias' in self.parameters: |
| 293 |
body['target.alias'] = self.parameters['target_alias'] |
| 294 |
dummy, error = rest_generic.post_async(self.rest_api, api, body) |
| 295 |
if error: |
| 296 |
self.module.fail_json(msg="Error creating iscsi service: % s" % error) |
| 297 |
|
| 298 |
def delete_iscsi_service_rest(self, current): |
| 299 |
# stop iscsi service before delete. |
| 300 |
if current['service_state'] == 'started': |
| 301 |
self.start_or_stop_iscsi_service_rest('stopped') |
| 302 |
api = 'protocols/san/iscsi/services' |
| 303 |
dummy, error = rest_generic.delete_async(self.rest_api, api, self.uuid) |
| 304 |
if error: |
| 305 |
self.module.fail_json(msg="Error deleting iscsi service on vserver %s: %s" % (self.parameters["vserver"], error)) |
| 306 |
|
| 307 |
def start_or_stop_iscsi_service_rest(self, service_state): |
| 308 |
api = 'protocols/san/iscsi/services' |
| 309 |
enabled = True if service_state == 'started' else False |
| 310 |
dummy, error = rest_generic.patch_async(self.rest_api, api, self.uuid, {'enabled': enabled}) |
| 311 |
if error: |
| 312 |
self.module.fail_json(msg="Error %s iscsi service on vserver %s: %s" % (service_state[0:5] + 'ing', self.parameters["vserver"], error)) |
| 313 |
|
| 314 |
def modify_iscsi_service_state_and_target(self, modify): |
| 315 |
body = {} |
| 316 |
api = 'protocols/san/iscsi/services' |
| 317 |
if 'target_alias' in modify: |
| 318 |
body['target.alias'] = self.parameters['target_alias'] |
| 319 |
dummy, error = rest_generic.patch_async(self.rest_api, api, self.uuid, body) |
| 320 |
if error: |
| 321 |
self.module.fail_json(msg="Error modifying iscsi service target alias on vserver %s: %s" % (self.parameters["vserver"], error)) |
| 322 |
|
| 323 |
def modify_iscsi_service_rest(self, modify, current): |
| 324 |
if self.use_rest: |
| 325 |
if 'service_state' in modify: |
| 326 |
self.start_or_stop_iscsi_service_rest(modify['service_state']) |
| 327 |
if 'target_alias' in modify: |
| 328 |
self.modify_iscsi_service_state_and_target(modify) |
| 329 |
else: |
| 330 |
if 'service_state' in modify: |
| 331 |
if modify['service_state'] == 'started': |
| 332 |
self.start_iscsi_service() |
| 333 |
else: |
| 334 |
self.stop_iscsi_service() |
| 335 |
|
| 336 |
def apply(self): |
| 337 |
current = self.get_iscsi() |
| 338 |
modify = None |
| 339 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 340 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) if cd_action is None else None |
| 341 |
|
| 342 |
if self.na_helper.changed and not self.module.check_mode: |
| 343 |
if cd_action == 'create': |
| 344 |
self.create_iscsi_service() |
| 345 |
elif cd_action == 'delete': |
| 346 |
self.delete_iscsi_service(current) |
| 347 |
elif modify: |
| 348 |
self.modify_iscsi_service_rest(modify, current) |
| 349 |
# TODO: include other details about the lun (size, etc.) |
| 350 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action) |
| 351 |
self.module.exit_json(**result) |
| 352 |
|
| 353 |
|
| 354 |
def main(): |
| 355 |
v = NetAppOntapISCSI() |
| 356 |
v.apply() |
| 357 |
|
| 358 |
|
| 359 |
if __name__ == '__main__': |
| 360 |
main() |