| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2017-2025, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
''' |
| 7 |
na_ontap_cluster |
| 8 |
''' |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
|
| 14 |
DOCUMENTATION = ''' |
| 15 |
module: na_ontap_cluster |
| 16 |
short_description: NetApp ONTAP cluster - create a cluster and add/remove nodes. |
| 17 |
extends_documentation_fragment: |
| 18 |
- netapp.ontap.netapp.na_ontap |
| 19 |
version_added: 2.6.0 |
| 20 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 21 |
description: |
| 22 |
- Create ONTAP cluster. |
| 23 |
- Add or remove cluster nodes using cluster_ip_address. |
| 24 |
- Adding a node requires ONTAP 9.3 or better. |
| 25 |
- Removing a node requires ONTAP 9.4 or better. |
| 26 |
options: |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Whether the specified cluster should exist (deleting a cluster is not supported). |
| 30 |
- Whether the node identified by its cluster_ip_address should be in the cluster or not. |
| 31 |
choices: ['present', 'absent'] |
| 32 |
type: str |
| 33 |
default: present |
| 34 |
cluster_name: |
| 35 |
description: |
| 36 |
- The name of the cluster to manage. |
| 37 |
type: str |
| 38 |
cluster_ip_address: |
| 39 |
description: |
| 40 |
- intra cluster IP address of the node to be added or removed. |
| 41 |
type: str |
| 42 |
single_node_cluster: |
| 43 |
description: |
| 44 |
- Whether the cluster is a single node cluster. Ignored for 9.3 or older versions. |
| 45 |
- If present, it was observed that 'Cluster' interfaces were deleted, whatever the value with ZAPI. |
| 46 |
version_added: 19.11.0 |
| 47 |
type: bool |
| 48 |
cluster_location: |
| 49 |
description: |
| 50 |
- Cluster location, only relevant if performing a modify action. |
| 51 |
version_added: 19.11.0 |
| 52 |
type: str |
| 53 |
cluster_contact: |
| 54 |
description: |
| 55 |
- Cluster contact, only relevant if performing a modify action. |
| 56 |
version_added: 19.11.0 |
| 57 |
type: str |
| 58 |
node_name: |
| 59 |
description: |
| 60 |
- Name of the node to be added or removed from the cluster. |
| 61 |
- Be aware that when adding a node, '-' are converted to '_' by the ONTAP backend. |
| 62 |
- When creating a cluster, C(node_name) is ignored. |
| 63 |
- When adding a node using C(cluster_ip_address), C(node_name) is optional. |
| 64 |
- When used to remove a node, C(cluster_ip_address) and C(node_name) are mutually exclusive. |
| 65 |
version_added: 20.9.0 |
| 66 |
type: str |
| 67 |
time_out: |
| 68 |
description: |
| 69 |
- time to wait for cluster creation in seconds. |
| 70 |
- Error out if task is not completed in defined time. |
| 71 |
- if 0, the request is asynchronous. |
| 72 |
- default is set to 3 minutes. |
| 73 |
default: 180 |
| 74 |
type: int |
| 75 |
version_added: 21.1.0 |
| 76 |
force: |
| 77 |
description: |
| 78 |
- forcibly remove a node that is down and cannot be brought online to remove its shared resources. |
| 79 |
default: false |
| 80 |
type: bool |
| 81 |
version_added: 21.13.0 |
| 82 |
timezone: |
| 83 |
description: timezone for the cluster. Only supported by REST. |
| 84 |
type: dict |
| 85 |
version_added: 21.24.0 |
| 86 |
suboptions: |
| 87 |
name: |
| 88 |
type: str |
| 89 |
description: |
| 90 |
- The timezone name must be |
| 91 |
- A geographic region, usually expressed as area/location |
| 92 |
- Greenwich Mean Time (GMT) or the difference in hours from GMT |
| 93 |
- A valid alias; that is, a term defined by the standard to refer to a geographic region or GMT |
| 94 |
- A system-specific or other term not associated with a geographic region or GMT |
| 95 |
- "full list of supported alias can be found here: https://library.netapp.com/ecmdocs/ECMP1155590/html/GUID-D3B8A525-67A2-4BEE-99DB-EF52D6744B5F.html" |
| 96 |
- Only supported by REST |
| 97 |
certificate: |
| 98 |
description: |
| 99 |
- Certificate used by cluster and node management interfaces for TLS connection requests. |
| 100 |
- Only supported with REST and requires ONTAP 9.10 or later. |
| 101 |
type: dict |
| 102 |
version_added: 22.9.0 |
| 103 |
suboptions: |
| 104 |
uuid: |
| 105 |
type: str |
| 106 |
description: |
| 107 |
- Certificate UUID. |
| 108 |
notes: |
| 109 |
- supports REST and ZAPI |
| 110 |
''' |
| 111 |
|
| 112 |
EXAMPLES = """ |
| 113 |
- name: Create cluster |
| 114 |
netapp.ontap.na_ontap_cluster: |
| 115 |
state: present |
| 116 |
cluster_name: new_cluster |
| 117 |
time_out: 0 |
| 118 |
hostname: "{{ netapp_hostname }}" |
| 119 |
username: "{{ netapp_username }}" |
| 120 |
password: "{{ netapp_password }}" |
| 121 |
|
| 122 |
- name: Add node to cluster (Join cluster) |
| 123 |
netapp.ontap.na_ontap_cluster: |
| 124 |
state: present |
| 125 |
cluster_ip_address: 10.10.10.10 |
| 126 |
hostname: "{{ netapp_hostname }}" |
| 127 |
username: "{{ netapp_username }}" |
| 128 |
password: "{{ netapp_password }}" |
| 129 |
|
| 130 |
- name: Add node to cluster (Join cluster) |
| 131 |
netapp.ontap.na_ontap_cluster: |
| 132 |
state: present |
| 133 |
cluster_ip_address: 10.10.10.10 |
| 134 |
node_name: my_preferred_node_name |
| 135 |
hostname: "{{ netapp_hostname }}" |
| 136 |
username: "{{ netapp_username }}" |
| 137 |
password: "{{ netapp_password }}" |
| 138 |
|
| 139 |
- name: Create a 2 node cluster in one call |
| 140 |
netapp.ontap.na_ontap_cluster: |
| 141 |
state: present |
| 142 |
cluster_name: new_cluster |
| 143 |
cluster_ip_address: 10.10.10.10 |
| 144 |
hostname: "{{ netapp_hostname }}" |
| 145 |
username: "{{ netapp_username }}" |
| 146 |
password: "{{ netapp_password }}" |
| 147 |
|
| 148 |
- name: Remove node from cluster |
| 149 |
netapp.ontap.na_ontap_cluster: |
| 150 |
state: absent |
| 151 |
cluster_ip_address: 10.10.10.10 |
| 152 |
hostname: "{{ netapp_hostname }}" |
| 153 |
username: "{{ netapp_username }}" |
| 154 |
password: "{{ netapp_password }}" |
| 155 |
|
| 156 |
- name: Remove node from cluster |
| 157 |
netapp.ontap.na_ontap_cluster: |
| 158 |
state: absent |
| 159 |
node_name: node002 |
| 160 |
hostname: "{{ netapp_hostname }}" |
| 161 |
username: "{{ netapp_username }}" |
| 162 |
password: "{{ netapp_password }}" |
| 163 |
|
| 164 |
- name: Modify cluster |
| 165 |
netapp.ontap.na_ontap_cluster: |
| 166 |
state: present |
| 167 |
cluster_contact: testing |
| 168 |
cluster_location: testing |
| 169 |
cluster_name: "{{ netapp_cluster}}" |
| 170 |
hostname: "{{ netapp_hostname }}" |
| 171 |
username: "{{ netapp_username }}" |
| 172 |
password: "{{ netapp_password }}" |
| 173 |
|
| 174 |
- name: Updating the cluster-wide web services configuration |
| 175 |
netapp.ontap.na_ontap_cluster: |
| 176 |
state: present |
| 177 |
cluster_contact: testing |
| 178 |
cluster_location: testing |
| 179 |
certificate: |
| 180 |
uuid: 7f2f332c-933e-11ee-ab1c-005056b397ff |
| 181 |
cluster_name: "{{ netapp_cluster}}" |
| 182 |
hostname: "{{ netapp_hostname }}" |
| 183 |
username: "{{ netapp_username }}" |
| 184 |
password: "{{ netapp_password }}" |
| 185 |
""" |
| 186 |
|
| 187 |
RETURN = """ |
| 188 |
""" |
| 189 |
|
| 190 |
import time |
| 191 |
import traceback |
| 192 |
|
| 193 |
from ansible.module_utils.basic import AnsibleModule |
| 194 |
from ansible.module_utils._text import to_native |
| 195 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 196 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 197 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 198 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 199 |
|
| 200 |
|
| 201 |
class NetAppONTAPCluster: |
| 202 |
""" |
| 203 |
object initialize and class methods |
| 204 |
""" |
| 205 |
def __init__(self): |
| 206 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 207 |
self.argument_spec.update(dict( |
| 208 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 209 |
cluster_name=dict(required=False, type='str'), |
| 210 |
cluster_ip_address=dict(required=False, type='str'), |
| 211 |
cluster_location=dict(required=False, type='str'), |
| 212 |
cluster_contact=dict(required=False, type='str'), |
| 213 |
certificate=dict(required=False, type='dict', options=dict( |
| 214 |
uuid=dict(required=False, type='str') |
| 215 |
)), |
| 216 |
force=dict(required=False, type='bool', default=False), |
| 217 |
single_node_cluster=dict(required=False, type='bool'), |
| 218 |
node_name=dict(required=False, type='str'), |
| 219 |
time_out=dict(required=False, type='int', default=180), |
| 220 |
timezone=dict(required=False, type='dict', options=dict( |
| 221 |
name=dict(type='str') |
| 222 |
)) |
| 223 |
)) |
| 224 |
|
| 225 |
self.module = AnsibleModule( |
| 226 |
argument_spec=self.argument_spec, |
| 227 |
supports_check_mode=True |
| 228 |
) |
| 229 |
|
| 230 |
self.na_helper = NetAppModule() |
| 231 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 232 |
self.warnings = [] |
| 233 |
# cached, so that we don't call the REST API more than once |
| 234 |
self.node_records = None |
| 235 |
|
| 236 |
self.rest_api = OntapRestAPI(self.module) |
| 237 |
partially_supported_rest_properties = [['certificate', (9, 10, 1)]] |
| 238 |
self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, None, partially_supported_rest_properties) |
| 239 |
|
| 240 |
if self.parameters['state'] == 'absent' and self.parameters.get('node_name') is not None and self.parameters.get('cluster_ip_address') is not None: |
| 241 |
msg = 'when state is "absent", parameters are mutually exclusive: cluster_ip_address|node_name' |
| 242 |
self.module.fail_json(msg=msg) |
| 243 |
|
| 244 |
if self.parameters.get('node_name') is not None and '-' in self.parameters.get('node_name'): |
| 245 |
self.warnings.append('ONTAP ZAPI converts "-" to "_", node_name: %s may be changed or not matched' % self.parameters.get('node_name')) |
| 246 |
|
| 247 |
if self.use_rest and self.parameters['state'] == 'absent' and not self.rest_api.meets_rest_minimum_version(True, 9, 7, 0): |
| 248 |
self.module.warn('switching back to ZAPI as DELETE is not supported on 9.6') |
| 249 |
self.use_rest = False |
| 250 |
if not self.use_rest: |
| 251 |
if self.na_helper.safe_get(self.parameters, ['timezone', 'name']): |
| 252 |
self.module.fail_json(msg='Timezone is only supported with REST') |
| 253 |
if self.na_helper.safe_get(self.parameters, ['certificate', 'uuid']): |
| 254 |
self.module.fail_json(msg='Certificate is only supported with REST') |
| 255 |
if not netapp_utils.has_netapp_lib(): |
| 256 |
self.module.fail_json(msg="the python NetApp-Lib module is required") |
| 257 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module) |
| 258 |
|
| 259 |
def get_cluster_identity_rest(self): |
| 260 |
''' get cluster information, but the cluster may not exist yet |
| 261 |
return: |
| 262 |
None if the cluster cannot be reached |
| 263 |
a dictionary of attributes |
| 264 |
''' |
| 265 |
record, error = rest_generic.get_one_record(self.rest_api, 'cluster', fields='contact,location,name,timezone') |
| 266 |
if error: |
| 267 |
if 'are available in precluster.' in error: |
| 268 |
# assuming precluster state |
| 269 |
return None |
| 270 |
self.module.fail_json(msg='Error fetching cluster identity info: %s' % to_native(error), |
| 271 |
exception=traceback.format_exc()) |
| 272 |
if record: |
| 273 |
cluster_info = { |
| 274 |
'cluster_contact': record.get('contact'), |
| 275 |
'cluster_location': record.get('location'), |
| 276 |
'cluster_name': record.get('name'), |
| 277 |
'timezone': self.na_helper.safe_get(record, ['timezone']) |
| 278 |
} |
| 279 |
if self.parameters.get('certificate') is not None: |
| 280 |
web_service_record = self.get_web_services() |
| 281 |
cluster_info.update(web_service_record) |
| 282 |
if cluster_info: |
| 283 |
return cluster_info |
| 284 |
return None |
| 285 |
|
| 286 |
def get_cluster_identity(self, ignore_error=True): |
| 287 |
''' get cluster information, but the cluster may not exist yet |
| 288 |
return: |
| 289 |
None if the cluster cannot be reached |
| 290 |
a dictionary of attributes |
| 291 |
''' |
| 292 |
if self.use_rest: |
| 293 |
return self.get_cluster_identity_rest() |
| 294 |
|
| 295 |
zapi = netapp_utils.zapi.NaElement('cluster-identity-get') |
| 296 |
try: |
| 297 |
result = self.server.invoke_successfully(zapi, enable_tunneling=True) |
| 298 |
except netapp_utils.zapi.NaApiError as error: |
| 299 |
if ignore_error: |
| 300 |
return None |
| 301 |
self.module.fail_json(msg='Error fetching cluster identity info: %s' % to_native(error), |
| 302 |
exception=traceback.format_exc()) |
| 303 |
cluster_identity = {} |
| 304 |
if result.get_child_by_name('attributes'): |
| 305 |
identity_info = result.get_child_by_name('attributes').get_child_by_name('cluster-identity-info') |
| 306 |
if identity_info: |
| 307 |
cluster_identity['cluster_contact'] = identity_info.get_child_content('cluster-contact') |
| 308 |
cluster_identity['cluster_location'] = identity_info.get_child_content('cluster-location') |
| 309 |
cluster_identity['cluster_name'] = identity_info.get_child_content('cluster-name') |
| 310 |
return cluster_identity |
| 311 |
return None |
| 312 |
|
| 313 |
def get_cluster_nodes_rest(self): |
| 314 |
''' get cluster node names, but the cluster may not exist yet |
| 315 |
return: |
| 316 |
None if the cluster cannot be reached |
| 317 |
a list of nodes |
| 318 |
''' |
| 319 |
if self.node_records is None: |
| 320 |
records, error = rest_generic.get_0_or_more_records(self.rest_api, 'cluster/nodes', fields='name,uuid,cluster_interfaces') |
| 321 |
if error: |
| 322 |
self.module.fail_json(msg='Error fetching cluster node info: %s' % to_native(error), |
| 323 |
exception=traceback.format_exc()) |
| 324 |
self.node_records = records or [] |
| 325 |
return self.node_records |
| 326 |
|
| 327 |
def get_cluster_node_names_rest(self): |
| 328 |
''' get cluster node names, but the cluster may not exist yet |
| 329 |
return: |
| 330 |
None if the cluster cannot be reached |
| 331 |
a list of nodes |
| 332 |
''' |
| 333 |
records = self.get_cluster_nodes_rest() |
| 334 |
return [record['name'] for record in records] |
| 335 |
|
| 336 |
def get_cluster_nodes(self, ignore_error=True): |
| 337 |
''' get cluster node names, but the cluster may not exist yet |
| 338 |
return: |
| 339 |
None if the cluster cannot be reached |
| 340 |
a list of nodes |
| 341 |
''' |
| 342 |
if self.use_rest: |
| 343 |
return self.get_cluster_node_names_rest() |
| 344 |
|
| 345 |
zapi = netapp_utils.zapi.NaElement('cluster-node-get-iter') |
| 346 |
try: |
| 347 |
result = self.server.invoke_successfully(zapi, enable_tunneling=True) |
| 348 |
except netapp_utils.zapi.NaApiError as error: |
| 349 |
if ignore_error: |
| 350 |
return None |
| 351 |
self.module.fail_json(msg='Error fetching cluster node info: %s' % to_native(error), |
| 352 |
exception=traceback.format_exc()) |
| 353 |
if result.get_child_by_name('attributes-list'): |
| 354 |
cluster_nodes = [] |
| 355 |
for node_info in result.get_child_by_name('attributes-list').get_children(): |
| 356 |
node_name = node_info.get_child_content('node-name') |
| 357 |
if node_name is not None: |
| 358 |
cluster_nodes.append(node_name) |
| 359 |
return cluster_nodes |
| 360 |
return None |
| 361 |
|
| 362 |
def get_cluster_ip_addresses_rest(self, cluster_ip_address): |
| 363 |
''' get list of IP addresses for this cluster |
| 364 |
return: |
| 365 |
a list of dictionaries |
| 366 |
''' |
| 367 |
if_infos = [] |
| 368 |
records = self.get_cluster_nodes_rest() |
| 369 |
for record in records: |
| 370 |
for interface in record.get('cluster_interfaces', []): |
| 371 |
ip_address = self.na_helper.safe_get(interface, ['ip', 'address']) |
| 372 |
if cluster_ip_address is None or ip_address == cluster_ip_address: |
| 373 |
if_info = { |
| 374 |
'address': ip_address, |
| 375 |
'home_node': record['name'], |
| 376 |
} |
| 377 |
if_infos.append(if_info) |
| 378 |
return if_infos |
| 379 |
|
| 380 |
def get_cluster_ip_addresses(self, cluster_ip_address, ignore_error=True): |
| 381 |
''' get list of IP addresses for this cluster |
| 382 |
return: |
| 383 |
a list of dictionaries |
| 384 |
''' |
| 385 |
if_infos = [] |
| 386 |
zapi = netapp_utils.zapi.NaElement('net-interface-get-iter') |
| 387 |
if cluster_ip_address is not None: |
| 388 |
query = netapp_utils.zapi.NaElement('query') |
| 389 |
net_info = netapp_utils.zapi.NaElement('net-interface-info') |
| 390 |
net_info.add_new_child('address', cluster_ip_address) |
| 391 |
query.add_child_elem(net_info) |
| 392 |
zapi.add_child_elem(query) |
| 393 |
|
| 394 |
try: |
| 395 |
result = self.server.invoke_successfully(zapi, enable_tunneling=True) |
| 396 |
except netapp_utils.zapi.NaApiError as error: |
| 397 |
if ignore_error: |
| 398 |
return if_infos |
| 399 |
self.module.fail_json(msg='Error getting IP addresses: %s' % to_native(error), |
| 400 |
exception=traceback.format_exc()) |
| 401 |
|
| 402 |
if result.get_child_by_name('attributes-list'): |
| 403 |
for net_info in result.get_child_by_name('attributes-list').get_children(): |
| 404 |
if net_info: |
| 405 |
if_info = {'address': net_info.get_child_content('address')} |
| 406 |
if_info['home_node'] = net_info.get_child_content('home-node') |
| 407 |
if_infos.append(if_info) |
| 408 |
return if_infos |
| 409 |
|
| 410 |
def get_cluster_ip_address(self, cluster_ip_address, ignore_error=True): |
| 411 |
''' get node information if it is discoverable |
| 412 |
return: |
| 413 |
None if the cluster cannot be reached |
| 414 |
a dictionary of attributes |
| 415 |
''' |
| 416 |
if cluster_ip_address is None: |
| 417 |
return None |
| 418 |
if self.use_rest: |
| 419 |
nodes = self.get_cluster_ip_addresses_rest(cluster_ip_address) |
| 420 |
else: |
| 421 |
nodes = self.get_cluster_ip_addresses(cluster_ip_address, ignore_error=ignore_error) |
| 422 |
return nodes if len(nodes) > 0 else None |
| 423 |
|
| 424 |
def create_cluster_body(self, modify=None, nodes=None): |
| 425 |
body = {} |
| 426 |
params = modify if modify is not None else self.parameters |
| 427 |
for (param_key, rest_key) in { |
| 428 |
'cluster_contact': 'contact', |
| 429 |
'cluster_location': 'location', |
| 430 |
'cluster_name': 'name', |
| 431 |
'single_node_cluster': 'single_node_cluster', |
| 432 |
'timezone': 'timezone' |
| 433 |
}.items(): |
| 434 |
if param_key in params: |
| 435 |
body[rest_key] = params[param_key] |
| 436 |
if nodes: |
| 437 |
body['nodes'] = nodes |
| 438 |
return body |
| 439 |
|
| 440 |
def create_node_body(self): |
| 441 |
node = {} |
| 442 |
for (param_key, rest_key) in { |
| 443 |
'cluster_ip_address': 'cluster_interface.ip.address', |
| 444 |
'cluster_location': 'location', |
| 445 |
'node_name': 'name' |
| 446 |
}.items(): |
| 447 |
if param_key in self.parameters: |
| 448 |
node[rest_key] = self.parameters[param_key] |
| 449 |
return node |
| 450 |
|
| 451 |
def create_nodes(self): |
| 452 |
node = self.create_node_body() |
| 453 |
return [node] if node else None |
| 454 |
|
| 455 |
def create_cluster_rest(self, older_api=False): |
| 456 |
""" |
| 457 |
Create a cluster |
| 458 |
""" |
| 459 |
query = None |
| 460 |
body = self.create_cluster_body(nodes=self.create_nodes()) |
| 461 |
if 'single_node_cluster' in body: |
| 462 |
query = {'single_node_cluster': body.pop('single_node_cluster')} |
| 463 |
dummy, error = rest_generic.post_async(self.rest_api, 'cluster', body, query, job_timeout=120) |
| 464 |
if error: |
| 465 |
self.module.fail_json(msg='Error creating cluster %s: %s' |
| 466 |
% (self.parameters['cluster_name'], to_native(error)), |
| 467 |
exception=traceback.format_exc()) |
| 468 |
|
| 469 |
def create_cluster(self, older_api=False): |
| 470 |
""" |
| 471 |
Create a cluster |
| 472 |
""" |
| 473 |
if self.use_rest: |
| 474 |
return self.create_cluster_rest() |
| 475 |
|
| 476 |
# Note: cannot use node_name here: |
| 477 |
# 13001:The "-node-names" parameter must be used with either the "-node-uuids" or the "-cluster-ips" parameters. |
| 478 |
options = {'cluster-name': self.parameters['cluster_name']} |
| 479 |
if not older_api and self.parameters.get('single_node_cluster') is not None: |
| 480 |
options['single-node-cluster'] = str(self.parameters['single_node_cluster']).lower() |
| 481 |
cluster_create = netapp_utils.zapi.NaElement.create_node_with_children( |
| 482 |
'cluster-create', **options) |
| 483 |
try: |
| 484 |
self.server.invoke_successfully(cluster_create, |
| 485 |
enable_tunneling=True) |
| 486 |
except netapp_utils.zapi.NaApiError as error: |
| 487 |
if error.message == "Extra input: single-node-cluster" and not older_api: |
| 488 |
return self.create_cluster(older_api=True) |
| 489 |
# Error 36503 denotes node already being used. |
| 490 |
if to_native(error.code) == "36503": |
| 491 |
return False |
| 492 |
self.module.fail_json(msg='Error creating cluster %s: %s' |
| 493 |
% (self.parameters['cluster_name'], to_native(error)), |
| 494 |
exception=traceback.format_exc()) |
| 495 |
return True |
| 496 |
|
| 497 |
def add_node_rest(self): |
| 498 |
""" |
| 499 |
Add a node to an existing cluster |
| 500 |
""" |
| 501 |
body = self.create_node_body() |
| 502 |
dummy, error = rest_generic.post_async(self.rest_api, 'cluster/nodes', body, job_timeout=120) |
| 503 |
if error: |
| 504 |
self.module.fail_json(msg='Error adding node with ip %s: %s' |
| 505 |
% (self.parameters.get('cluster_ip_address'), to_native(error)), |
| 506 |
exception=traceback.format_exc()) |
| 507 |
|
| 508 |
def add_node(self, older_api=False): |
| 509 |
""" |
| 510 |
Add a node to an existing cluster |
| 511 |
9.2 and 9.3 do not support cluster-ips so fallback to node-ip |
| 512 |
""" |
| 513 |
if self.use_rest: |
| 514 |
return self.add_node_rest() |
| 515 |
|
| 516 |
if self.parameters.get('cluster_ip_address') is None: |
| 517 |
return False |
| 518 |
cluster_add_node = netapp_utils.zapi.NaElement('cluster-add-node') |
| 519 |
if older_api: |
| 520 |
cluster_add_node.add_new_child('node-ip', self.parameters.get('cluster_ip_address')) |
| 521 |
else: |
| 522 |
cluster_ips = netapp_utils.zapi.NaElement.create_node_with_children('cluster-ips', **{'ip-address': self.parameters.get('cluster_ip_address')}) |
| 523 |
cluster_add_node.add_child_elem(cluster_ips) |
| 524 |
if self.parameters.get('node_name') is not None: |
| 525 |
node_names = netapp_utils.zapi.NaElement.create_node_with_children('node-names', **{'string': self.parameters.get('node_name')}) |
| 526 |
cluster_add_node.add_child_elem(node_names) |
| 527 |
|
| 528 |
try: |
| 529 |
self.server.invoke_successfully(cluster_add_node, enable_tunneling=True) |
| 530 |
except netapp_utils.zapi.NaApiError as error: |
| 531 |
if error.message == "Extra input: cluster-ips" and not older_api: |
| 532 |
return self.add_node(older_api=True) |
| 533 |
# skip if error says no failed operations to retry. |
| 534 |
if to_native(error) == "NetApp API failed. Reason - 13001:There are no failed \"cluster create\" or \"cluster add-node\" operations to retry.": |
| 535 |
return False |
| 536 |
self.module.fail_json(msg='Error adding node with ip %s: %s' |
| 537 |
% (self.parameters.get('cluster_ip_address'), to_native(error)), |
| 538 |
exception=traceback.format_exc()) |
| 539 |
return True |
| 540 |
|
| 541 |
def get_uuid_from_ip(self, ip_address): |
| 542 |
for node in self.get_cluster_nodes_rest(): |
| 543 |
if ip_address in (interface['ip']['address'] for interface in node['cluster_interfaces']): |
| 544 |
return node['uuid'] |
| 545 |
return None |
| 546 |
|
| 547 |
def get_uuid_from_name(self, node_name): |
| 548 |
for node in self.get_cluster_nodes_rest(): |
| 549 |
if node_name == node['name']: |
| 550 |
return node['uuid'] |
| 551 |
return None |
| 552 |
|
| 553 |
def get_uuid(self): |
| 554 |
if self.parameters.get('cluster_ip_address') is not None: |
| 555 |
from_node = self.parameters['cluster_ip_address'] |
| 556 |
uuid = self.get_uuid_from_ip(from_node) |
| 557 |
elif self.parameters.get('node_name') is not None: |
| 558 |
from_node = self.parameters['node_name'] |
| 559 |
uuid = self.get_uuid_from_name(from_node) |
| 560 |
else: |
| 561 |
# Unexpected, for delete one of cluster_ip_address, node_name is required. |
| 562 |
uuid = None |
| 563 |
if uuid is None: |
| 564 |
self.module.fail_json(msg='Internal error, cannot find UUID in %s: for %s or %s' |
| 565 |
% (self.get_cluster_nodes_rest(), self.parameters['cluster_ip_address'], self.parameters.get('node_name') is not None), |
| 566 |
exception=traceback.format_exc()) |
| 567 |
return uuid, from_node |
| 568 |
|
| 569 |
def get_web_services(self): |
| 570 |
record, error = rest_generic.get_one_record(self.rest_api, 'cluster/web', fields='certificate') |
| 571 |
if error: |
| 572 |
self.module.fail_json(msg='Error fetching cluster web service config: %s' % to_native(error), |
| 573 |
exception=traceback.format_exc()) |
| 574 |
if record: |
| 575 |
return record |
| 576 |
return None |
| 577 |
|
| 578 |
def modify_web_services(self): |
| 579 |
body = { |
| 580 |
'certificate': { |
| 581 |
'uuid': self.parameters['certificate']['uuid'] |
| 582 |
} |
| 583 |
} |
| 584 |
dummy, error = rest_generic.patch_async(self.rest_api, 'cluster/web', None, body) |
| 585 |
if error: |
| 586 |
self.module.fail_json(msg='Error modifying cluster web service config for %s: %s' |
| 587 |
% (self.parameters['cluster_name'], to_native(error)), |
| 588 |
exception=traceback.format_exc()) |
| 589 |
|
| 590 |
def remove_node_rest(self): |
| 591 |
""" |
| 592 |
Remove a node from an existing cluster |
| 593 |
""" |
| 594 |
uuid, from_node = self.get_uuid() |
| 595 |
query = {'force': True} if self.parameters.get('force') else None |
| 596 |
dummy, error = rest_generic.delete_async(self.rest_api, 'cluster/nodes', uuid, query, job_timeout=120) |
| 597 |
if error: |
| 598 |
self.module.fail_json(msg='Error removing node with %s: %s' |
| 599 |
% (from_node, to_native(error)), exception=traceback.format_exc()) |
| 600 |
|
| 601 |
def remove_node(self): |
| 602 |
""" |
| 603 |
Remove a node from an existing cluster |
| 604 |
""" |
| 605 |
if self.use_rest: |
| 606 |
return self.remove_node_rest() |
| 607 |
|
| 608 |
cluster_remove_node = netapp_utils.zapi.NaElement('cluster-remove-node') |
| 609 |
from_node = '' |
| 610 |
# cluster-ip and node-name are mutually exclusive: |
| 611 |
# 13115:Element "cluster-ip" within "cluster-remove-node" has been excluded by another element. |
| 612 |
if self.parameters.get('cluster_ip_address') is not None: |
| 613 |
cluster_remove_node.add_new_child('cluster-ip', self.parameters.get('cluster_ip_address')) |
| 614 |
from_node = 'IP: %s' % self.parameters.get('cluster_ip_address') |
| 615 |
elif self.parameters.get('node_name') is not None: |
| 616 |
cluster_remove_node.add_new_child('node', self.parameters.get('node_name')) |
| 617 |
from_node = 'name: %s' % self.parameters.get('node_name') |
| 618 |
if self.parameters.get('force'): |
| 619 |
cluster_remove_node.add_new_child('force', 'true') |
| 620 |
|
| 621 |
try: |
| 622 |
self.server.invoke_successfully(cluster_remove_node, enable_tunneling=True) |
| 623 |
except netapp_utils.zapi.NaApiError as error: |
| 624 |
if error.message == "Unable to find API: cluster-remove-node": |
| 625 |
msg = 'Error: ZAPI is not available. Removing a node requires ONTAP 9.4 or newer.' |
| 626 |
self.module.fail_json(msg=msg) |
| 627 |
self.module.fail_json(msg='Error removing node with %s: %s' |
| 628 |
% (from_node, to_native(error)), exception=traceback.format_exc()) |
| 629 |
|
| 630 |
def modify_cluster_identity_rest(self, modify): |
| 631 |
""" |
| 632 |
Modifies the cluster identity |
| 633 |
""" |
| 634 |
if 'certificate' in modify: |
| 635 |
self.modify_web_services() |
| 636 |
body = self.create_cluster_body(modify) |
| 637 |
dummy, error = rest_generic.patch_async(self.rest_api, 'cluster', None, body) |
| 638 |
if error: |
| 639 |
self.module.fail_json(msg='Error modifying cluster identity details %s: %s' |
| 640 |
% (self.parameters['cluster_name'], to_native(error)), |
| 641 |
exception=traceback.format_exc()) |
| 642 |
|
| 643 |
def modify_cluster_identity(self, modify): |
| 644 |
""" |
| 645 |
Modifies the cluster identity |
| 646 |
""" |
| 647 |
if self.use_rest: |
| 648 |
return self.modify_cluster_identity_rest(modify) |
| 649 |
|
| 650 |
cluster_modify = netapp_utils.zapi.NaElement('cluster-identity-modify') |
| 651 |
if modify.get('cluster_name') is not None: |
| 652 |
cluster_modify.add_new_child("cluster-name", modify.get('cluster_name')) |
| 653 |
if modify.get('cluster_location') is not None: |
| 654 |
cluster_modify.add_new_child("cluster-location", modify.get('cluster_location')) |
| 655 |
if modify.get('cluster_contact') is not None: |
| 656 |
cluster_modify.add_new_child("cluster-contact", modify.get('cluster_contact')) |
| 657 |
|
| 658 |
try: |
| 659 |
self.server.invoke_successfully(cluster_modify, |
| 660 |
enable_tunneling=True) |
| 661 |
except netapp_utils.zapi.NaApiError as error: |
| 662 |
self.module.fail_json(msg='Error modifying cluster idetity details %s: %s' |
| 663 |
% (self.parameters['cluster_name'], to_native(error)), |
| 664 |
exception=traceback.format_exc()) |
| 665 |
|
| 666 |
def cluster_create_wait(self): |
| 667 |
""" |
| 668 |
Wait whilst cluster creation completes |
| 669 |
""" |
| 670 |
if self.use_rest: |
| 671 |
# wait is part of post_async for REST |
| 672 |
return |
| 673 |
|
| 674 |
cluster_wait = netapp_utils.zapi.NaElement('cluster-create-join-progress-get') |
| 675 |
is_complete = False |
| 676 |
status = '' |
| 677 |
retries = self.parameters['time_out'] |
| 678 |
errors = [] |
| 679 |
while not is_complete and status not in ('failed', 'success') and retries > 0: |
| 680 |
retries = retries - 10 |
| 681 |
time.sleep(10) |
| 682 |
try: |
| 683 |
result = self.server.invoke_successfully(cluster_wait, enable_tunneling=True) |
| 684 |
except netapp_utils.zapi.NaApiError as error: |
| 685 |
# collecting errors, and retrying |
| 686 |
errors.append(repr(error)) |
| 687 |
continue |
| 688 |
|
| 689 |
clus_progress = result.get_child_by_name('attributes') |
| 690 |
result = clus_progress.get_child_by_name('cluster-create-join-progress-info') |
| 691 |
is_complete = self.na_helper.get_value_for_bool(from_zapi=True, |
| 692 |
value=result.get_child_content('is-complete')) |
| 693 |
status = result.get_child_content('status') |
| 694 |
|
| 695 |
if self.parameters['time_out'] == 0: |
| 696 |
is_complete = True |
| 697 |
if not is_complete and status != 'success': |
| 698 |
current_status_message = result.get_child_content('current-status-message') |
| 699 |
errors.append('Failed to confirm cluster creation %s: %s' % (self.parameters.get('cluster_name'), current_status_message)) |
| 700 |
if retries <= 0: |
| 701 |
errors.append("Timeout after %s seconds" % self.parameters['time_out']) |
| 702 |
self.module.fail_json(msg='Error creating cluster %s: %s' |
| 703 |
% (self.parameters['cluster_name'], str(errors))) |
| 704 |
|
| 705 |
return is_complete |
| 706 |
|
| 707 |
def node_add_wait(self): |
| 708 |
""" |
| 709 |
Wait whilst node is being added to the existing cluster |
| 710 |
""" |
| 711 |
if self.use_rest: |
| 712 |
# wait is part of post_async for REST |
| 713 |
return |
| 714 |
|
| 715 |
cluster_node_status = netapp_utils.zapi.NaElement('cluster-add-node-status-get-iter') |
| 716 |
node_status_info = netapp_utils.zapi.NaElement('cluster-create-add-node-status-info') |
| 717 |
node_status_info.add_new_child('cluster-ip', self.parameters.get('cluster_ip_address')) |
| 718 |
query = netapp_utils.zapi.NaElement('query') |
| 719 |
query.add_child_elem(node_status_info) |
| 720 |
cluster_node_status.add_child_elem(query) |
| 721 |
|
| 722 |
is_complete = None |
| 723 |
failure_msg = None |
| 724 |
retries = self.parameters['time_out'] |
| 725 |
errors = [] |
| 726 |
while is_complete != 'success' and is_complete != 'failure' and retries > 0: |
| 727 |
retries = retries - 10 |
| 728 |
time.sleep(10) |
| 729 |
try: |
| 730 |
result = self.server.invoke_successfully(cluster_node_status, enable_tunneling=True) |
| 731 |
except netapp_utils.zapi.NaApiError as error: |
| 732 |
if error.message == "Unable to find API: cluster-add-node-status-get-iter": |
| 733 |
# This API is not supported for 9.3 or earlier releases, just wait a bit |
| 734 |
time.sleep(60) |
| 735 |
return |
| 736 |
# collecting errors, and retrying |
| 737 |
errors.append(repr(error)) |
| 738 |
continue |
| 739 |
|
| 740 |
attributes_list = result.get_child_by_name('attributes-list') |
| 741 |
join_progress = attributes_list.get_child_by_name('cluster-create-add-node-status-info') |
| 742 |
is_complete = join_progress.get_child_content('status') |
| 743 |
failure_msg = join_progress.get_child_content('failure-msg') |
| 744 |
|
| 745 |
if self.parameters['time_out'] == 0: |
| 746 |
is_complete = 'success' |
| 747 |
if is_complete != 'success': |
| 748 |
if 'Node is already in a cluster' in failure_msg: |
| 749 |
return |
| 750 |
elif retries <= 0: |
| 751 |
errors.append("Timeout after %s seconds" % self.parameters['time_out']) |
| 752 |
if failure_msg: |
| 753 |
errors.append(failure_msg) |
| 754 |
self.module.fail_json(msg='Error adding node with ip address %s: %s' |
| 755 |
% (self.parameters['cluster_ip_address'], str(errors))) |
| 756 |
|
| 757 |
def node_remove_wait(self): |
| 758 |
''' wait for node name or clister IP address to disappear ''' |
| 759 |
if self.use_rest: |
| 760 |
# wait is part of delete_async for REST |
| 761 |
return |
| 762 |
|
| 763 |
node_name = self.parameters.get('node_name') |
| 764 |
node_ip = self.parameters.get('cluster_ip_address') |
| 765 |
retries = self.parameters['time_out'] |
| 766 |
while retries > 0: |
| 767 |
retries = retries - 10 |
| 768 |
if node_name is not None and node_name not in self.get_cluster_nodes(): |
| 769 |
return |
| 770 |
if node_ip is not None and self.get_cluster_ip_address(node_ip) is None: |
| 771 |
return |
| 772 |
time.sleep(10) |
| 773 |
self.module.fail_json(msg='Timeout waiting for node to be removed from cluster.') |
| 774 |
|
| 775 |
def get_cluster_action(self, cluster_identity): |
| 776 |
cluster_action = None |
| 777 |
if self.parameters.get('cluster_name') is not None: |
| 778 |
cluster_action = self.na_helper.get_cd_action(cluster_identity, self.parameters) |
| 779 |
if cluster_action == 'delete': |
| 780 |
# delete only applies to node |
| 781 |
cluster_action = None |
| 782 |
self.na_helper.changed = False |
| 783 |
return cluster_action |
| 784 |
|
| 785 |
def get_node_action(self): |
| 786 |
node_action = None |
| 787 |
if self.parameters.get('cluster_ip_address') is not None: |
| 788 |
existing_interfaces = self.get_cluster_ip_address(self.parameters.get('cluster_ip_address')) |
| 789 |
if self.parameters.get('state') == 'present': |
| 790 |
node_action = 'add_node' if existing_interfaces is None else None |
| 791 |
else: |
| 792 |
node_action = 'remove_node' if existing_interfaces is not None else None |
| 793 |
if self.parameters.get('node_name') is not None and self.parameters['state'] == 'absent': |
| 794 |
nodes = self.get_cluster_nodes() |
| 795 |
if self.parameters.get('node_name') in nodes: |
| 796 |
node_action = 'remove_node' |
| 797 |
if node_action is not None: |
| 798 |
self.na_helper.changed = True |
| 799 |
return node_action |
| 800 |
|
| 801 |
def apply(self): |
| 802 |
""" |
| 803 |
Apply action to cluster |
| 804 |
""" |
| 805 |
cluster_identity = self.get_cluster_identity(ignore_error=True) |
| 806 |
cluster_action = self.get_cluster_action(cluster_identity) |
| 807 |
node_action = self.get_node_action() |
| 808 |
modify = self.na_helper.get_modified_attributes(cluster_identity, self.parameters) |
| 809 |
|
| 810 |
if not self.module.check_mode: |
| 811 |
if cluster_action == 'create' and self.create_cluster(): |
| 812 |
self.cluster_create_wait() |
| 813 |
if node_action == 'add_node': |
| 814 |
if self.add_node(): |
| 815 |
self.node_add_wait() |
| 816 |
elif node_action == 'remove_node': |
| 817 |
self.remove_node() |
| 818 |
self.node_remove_wait() |
| 819 |
if modify: |
| 820 |
self.modify_cluster_identity(modify) |
| 821 |
|
| 822 |
results = {'changed': self.na_helper.changed} |
| 823 |
if self.warnings: |
| 824 |
results['warnings'] = self.warnings |
| 825 |
if netapp_utils.has_feature(self.module, 'show_modified'): |
| 826 |
results['modify'] = modify |
| 827 |
self.module.exit_json(**results) |
| 828 |
|
| 829 |
|
| 830 |
def main(): |
| 831 |
""" |
| 832 |
Create object and call apply |
| 833 |
""" |
| 834 |
cluster_obj = NetAppONTAPCluster() |
| 835 |
cluster_obj.apply() |
| 836 |
|
| 837 |
|
| 838 |
if __name__ == '__main__': |
| 839 |
main() |