| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2019-2026, 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 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 12 |
short_description: "NetApp ONTAP Manage name service switch" |
| 13 |
description: |
| 14 |
- Create/Delete/Modify Name Service Switch. |
| 15 |
- Deleting name service switch not supported in REST. |
| 16 |
extends_documentation_fragment: |
| 17 |
- netapp.ontap.netapp.na_ontap |
| 18 |
module: na_ontap_name_service_switch |
| 19 |
options: |
| 20 |
state: |
| 21 |
choices: ['present', 'absent'] |
| 22 |
description: |
| 23 |
- Whether the specified ns-switch should exist or not. |
| 24 |
default: present |
| 25 |
type: str |
| 26 |
vserver: |
| 27 |
description: |
| 28 |
- Name of the vserver to use. |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
database_type: |
| 32 |
description: |
| 33 |
- Name services switch database. |
| 34 |
choices: ['hosts','group', 'passwd', 'netgroup', 'namemap'] |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
sources: |
| 38 |
description: |
| 39 |
- Type of sources. |
| 40 |
- Possible values include files,dns,ldap,nis. |
| 41 |
type: list |
| 42 |
elements: str |
| 43 |
lambda_config: |
| 44 |
description: |
| 45 |
- Configuration parameters for AWS Lambda proxy functionality. |
| 46 |
- These option and suboptions are only supported with REST. |
| 47 |
type: dict |
| 48 |
version_added: 23.5.0 |
| 49 |
suboptions: |
| 50 |
function_name: |
| 51 |
description: |
| 52 |
- The name of the AWS Lambda function to invoke. |
| 53 |
type: str |
| 54 |
required: true |
| 55 |
aws_region: |
| 56 |
description: |
| 57 |
- The name of the AWS region. |
| 58 |
type: str |
| 59 |
required: true |
| 60 |
aws_profile: |
| 61 |
description: |
| 62 |
- The name of the AWS profile to use for authentication. |
| 63 |
type: str |
| 64 |
|
| 65 |
notes: |
| 66 |
- Supports AWS Lambda proxy functionality when using REST. See the README file for examples. |
| 67 |
''' |
| 68 |
|
| 69 |
EXAMPLES = """ |
| 70 |
- name: Create name service database |
| 71 |
netapp.ontap.na_ontap_name_service_switch: |
| 72 |
state: present |
| 73 |
database_type: namemap |
| 74 |
sources: files,ldap |
| 75 |
vserver: "{{ Vserver name }}" |
| 76 |
username: "{{ netapp_username }}" |
| 77 |
password: "{{ netapp_password }}" |
| 78 |
hostname: "{{ netapp_hostname }}" |
| 79 |
|
| 80 |
- name: Modify name service database sources |
| 81 |
netapp.ontap.na_ontap_name_service_switch: |
| 82 |
state: present |
| 83 |
database_type: namemap |
| 84 |
sources: files |
| 85 |
vserver: "{{ Vserver name }}" |
| 86 |
username: "{{ netapp_username }}" |
| 87 |
password: "{{ netapp_password }}" |
| 88 |
hostname: "{{ netapp_hostname }}" |
| 89 |
""" |
| 90 |
|
| 91 |
RETURN = """ |
| 92 |
""" |
| 93 |
|
| 94 |
import traceback |
| 95 |
from ansible.module_utils.basic import AnsibleModule |
| 96 |
from ansible.module_utils._text import to_native |
| 97 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 98 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 99 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic, rest_vserver |
| 100 |
|
| 101 |
|
| 102 |
class NetAppONTAPNsswitch: |
| 103 |
""" |
| 104 |
Class with NVMe service methods |
| 105 |
""" |
| 106 |
|
| 107 |
def __init__(self): |
| 108 |
|
| 109 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 110 |
self.argument_spec.update(dict( |
| 111 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 112 |
vserver=dict(required=True, type='str'), |
| 113 |
database_type=dict(required=True, type='str', choices=['hosts', 'group', 'passwd', 'netgroup', 'namemap']), |
| 114 |
sources=dict(required=False, type='list', elements='str') |
| 115 |
)) |
| 116 |
|
| 117 |
self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec()) |
| 118 |
|
| 119 |
self.module = AnsibleModule( |
| 120 |
argument_spec=self.argument_spec, |
| 121 |
supports_check_mode=True, |
| 122 |
required_if=[ |
| 123 |
['use_lambda', True, ('lambda_config',)] |
| 124 |
], |
| 125 |
) |
| 126 |
|
| 127 |
self.na_helper = NetAppModule() |
| 128 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 129 |
if self.parameters.get('sources') is not None: |
| 130 |
self.parameters['sources'] = [source.strip() for source in self.parameters['sources']] |
| 131 |
if '' in self.parameters['sources']: |
| 132 |
self.module.fail_json(msg="Error: Invalid value '' specified for sources") |
| 133 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 134 |
self.use_rest = self.rest_api.is_rest() |
| 135 |
self.svm_uuid = None |
| 136 |
if not self.use_rest: |
| 137 |
if self.parameters.get('use_lambda'): |
| 138 |
self.module.fail_json(msg="Error: AWS Lambda proxy for ONTAP APIs is only supported with REST.") |
| 139 |
if not netapp_utils.has_netapp_lib(): |
| 140 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 141 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 142 |
|
| 143 |
def get_name_service_switch(self): |
| 144 |
""" |
| 145 |
get current name service switch config |
| 146 |
:return: dict of current name service switch |
| 147 |
""" |
| 148 |
if self.use_rest: |
| 149 |
return self.get_name_service_switch_rest() |
| 150 |
nss_iter = netapp_utils.zapi.NaElement('nameservice-nsswitch-get-iter') |
| 151 |
nss_info = netapp_utils.zapi.NaElement('namservice-nsswitch-config-info') |
| 152 |
db_type = netapp_utils.zapi.NaElement('nameservice-database') |
| 153 |
db_type.set_content(self.parameters['database_type']) |
| 154 |
query = netapp_utils.zapi.NaElement('query') |
| 155 |
nss_info.add_child_elem(db_type) |
| 156 |
query.add_child_elem(nss_info) |
| 157 |
nss_iter.add_child_elem(query) |
| 158 |
try: |
| 159 |
result = self.server.invoke_successfully(nss_iter, True) |
| 160 |
except netapp_utils.zapi.NaApiError as error: |
| 161 |
self.module.fail_json(msg='Error fetching name service switch info for %s: %s' % |
| 162 |
(self.parameters['vserver'], to_native(error)), exception=traceback.format_exc()) |
| 163 |
return_value = None |
| 164 |
if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) == 1: |
| 165 |
nss_sources = result.get_child_by_name('attributes-list').get_child_by_name( |
| 166 |
'namservice-nsswitch-config-info').get_child_by_name('nameservice-sources') |
| 167 |
# nameservice-sources will not present in result if the value is '-' |
| 168 |
if nss_sources: |
| 169 |
sources = [sources.get_content() for sources in nss_sources.get_children()] |
| 170 |
return_value = {'sources': sources} |
| 171 |
else: |
| 172 |
return_value = {'sources': []} |
| 173 |
return return_value |
| 174 |
|
| 175 |
def create_name_service_switch(self): |
| 176 |
""" |
| 177 |
create name service switch config |
| 178 |
:return: None |
| 179 |
""" |
| 180 |
nss_create = netapp_utils.zapi.NaElement('nameservice-nsswitch-create') |
| 181 |
nss_create.add_new_child('nameservice-database', self.parameters['database_type']) |
| 182 |
nss_sources = netapp_utils.zapi.NaElement('nameservice-sources') |
| 183 |
nss_create.add_child_elem(nss_sources) |
| 184 |
for source in self.parameters['sources']: |
| 185 |
nss_sources.add_new_child('nss-source-type', source) |
| 186 |
try: |
| 187 |
self.server.invoke_successfully(nss_create, |
| 188 |
enable_tunneling=True) |
| 189 |
except netapp_utils.zapi.NaApiError as error: |
| 190 |
self.module.fail_json(msg='Error on creating name service switch config on vserver %s: %s' |
| 191 |
% (self.parameters['vserver'], to_native(error)), |
| 192 |
exception=traceback.format_exc()) |
| 193 |
|
| 194 |
def delete_name_service_switch(self): |
| 195 |
""" |
| 196 |
delete name service switch |
| 197 |
:return: None |
| 198 |
""" |
| 199 |
nss_delete = netapp_utils.zapi.NaElement.create_node_with_children( |
| 200 |
'nameservice-nsswitch-destroy', **{'nameservice-database': self.parameters['database_type']}) |
| 201 |
try: |
| 202 |
self.server.invoke_successfully(nss_delete, |
| 203 |
enable_tunneling=True) |
| 204 |
except netapp_utils.zapi.NaApiError as error: |
| 205 |
self.module.fail_json(msg='Error on deleting name service switch config on vserver %s: %s' |
| 206 |
% (self.parameters['vserver'], to_native(error)), |
| 207 |
exception=traceback.format_exc()) |
| 208 |
|
| 209 |
def modify_name_service_switch(self, modify): |
| 210 |
""" |
| 211 |
modify name service switch |
| 212 |
:param modify: dict of modify attributes |
| 213 |
:return: None |
| 214 |
""" |
| 215 |
if self.use_rest: |
| 216 |
return self.modify_name_service_switch_rest() |
| 217 |
nss_modify = netapp_utils.zapi.NaElement('nameservice-nsswitch-modify') |
| 218 |
nss_modify.add_new_child('nameservice-database', self.parameters['database_type']) |
| 219 |
nss_sources = netapp_utils.zapi.NaElement('nameservice-sources') |
| 220 |
nss_modify.add_child_elem(nss_sources) |
| 221 |
if 'sources' in modify: |
| 222 |
for source in self.parameters['sources']: |
| 223 |
nss_sources.add_new_child('nss-source-type', source) |
| 224 |
try: |
| 225 |
self.server.invoke_successfully(nss_modify, enable_tunneling=True) |
| 226 |
except netapp_utils.zapi.NaApiError as error: |
| 227 |
self.module.fail_json(msg='Error on modifying name service switch config on vserver %s: %s' |
| 228 |
% (self.parameters['vserver'], to_native(error)), |
| 229 |
exception=traceback.format_exc()) |
| 230 |
|
| 231 |
def get_name_service_switch_rest(self): |
| 232 |
record, error = rest_vserver.get_vserver(self.rest_api, self.parameters['vserver'], 'nsswitch,uuid') |
| 233 |
if error: |
| 234 |
self.module.fail_json(msg='Error fetching name service switch info for %s: %s' % |
| 235 |
(self.parameters['vserver'], to_native(error))) |
| 236 |
if not record: |
| 237 |
self.module.fail_json(msg="Error: Specified vserver %s not found" % self.parameters['vserver']) |
| 238 |
self.svm_uuid = record['uuid'] |
| 239 |
# if database type is already deleted by ZAPI call, REST will not have the database key. |
| 240 |
# setting it to [] help to set the value in REST patch call. |
| 241 |
database_type = self.na_helper.safe_get(record, ['nsswitch', self.parameters['database_type']]) |
| 242 |
return {'sources': database_type if database_type else []} |
| 243 |
|
| 244 |
def modify_name_service_switch_rest(self): |
| 245 |
api = 'svm/svms' |
| 246 |
body = { |
| 247 |
'nsswitch': { |
| 248 |
self.parameters['database_type']: self.parameters['sources'] |
| 249 |
} |
| 250 |
} |
| 251 |
dummy, error = rest_generic.patch_async(self.rest_api, api, self.svm_uuid, body) |
| 252 |
if error: |
| 253 |
self.module.fail_json(msg='Error on modifying name service switch config on vserver %s: %s' |
| 254 |
% (self.parameters['vserver'], to_native(error))) |
| 255 |
|
| 256 |
def apply(self): |
| 257 |
current = self.get_name_service_switch() |
| 258 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 259 |
if cd_action == 'delete' and self.use_rest: |
| 260 |
self.module.fail_json(msg="Error: deleting name service switch not supported in REST.") |
| 261 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 262 |
if self.na_helper.changed and not self.module.check_mode: |
| 263 |
if cd_action == 'create': |
| 264 |
self.create_name_service_switch() |
| 265 |
elif cd_action == 'delete': |
| 266 |
self.delete_name_service_switch() |
| 267 |
elif modify: |
| 268 |
self.modify_name_service_switch(modify) |
| 269 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify) |
| 270 |
self.module.exit_json(**result) |
| 271 |
|
| 272 |
|
| 273 |
def main(): |
| 274 |
'''Applyoperations from playbook''' |
| 275 |
nss = NetAppONTAPNsswitch() |
| 276 |
nss.apply() |
| 277 |
|
| 278 |
|
| 279 |
if __name__ == '__main__': |
| 280 |
main() |