| 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 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 11 |
'status': ['preview'], |
| 12 |
'supported_by': 'certified'} |
| 13 |
|
| 14 |
DOCUMENTATION = ''' |
| 15 |
module: na_ontap_cifs_local_user_modify |
| 16 |
short_description: NetApp ONTAP modify local CIFS user. |
| 17 |
extends_documentation_fragment: |
| 18 |
- netapp.ontap.netapp.na_ontap |
| 19 |
version_added: '21.4.0' |
| 20 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 21 |
|
| 22 |
description: |
| 23 |
- Modify a local CIFS user |
| 24 |
options: |
| 25 |
name: |
| 26 |
description: |
| 27 |
- The name of the local cifs user |
| 28 |
required: true |
| 29 |
type: str |
| 30 |
|
| 31 |
vserver: |
| 32 |
description: |
| 33 |
- the name of the data vserver to use. |
| 34 |
required: true |
| 35 |
type: str |
| 36 |
|
| 37 |
is_account_disabled: |
| 38 |
description: |
| 39 |
- Whether the local cifs user is disabled or not |
| 40 |
type: bool |
| 41 |
|
| 42 |
description: |
| 43 |
description: |
| 44 |
- the description for the local cifs user |
| 45 |
type: str |
| 46 |
|
| 47 |
full_name: |
| 48 |
description: |
| 49 |
- the full name for the local cifs user |
| 50 |
type: str |
| 51 |
''' |
| 52 |
|
| 53 |
EXAMPLES = """ |
| 54 |
- name: Enable local CIFS Administrator account |
| 55 |
na_ontap_cifs_local_user_modify: |
| 56 |
name: BUILTIN\\administrators |
| 57 |
vserver: ansible |
| 58 |
is_account_disabled: false |
| 59 |
username: "{{ netapp_username }}" |
| 60 |
password: "{{ netapp_password }}" |
| 61 |
hostname: "{{ netapp_hostname }}" |
| 62 |
|
| 63 |
- name: Disable local CIFS Administrator account |
| 64 |
na_ontap_cifs_local_user_modify: |
| 65 |
name: BUILTIN\\administrators |
| 66 |
vserver: ansible |
| 67 |
is_account_disabled: true |
| 68 |
username: "{{ netapp_username }}" |
| 69 |
password: "{{ netapp_password }}" |
| 70 |
hostname: "{{ netapp_hostname }}" |
| 71 |
""" |
| 72 |
|
| 73 |
RETURN = """ |
| 74 |
|
| 75 |
""" |
| 76 |
|
| 77 |
import traceback |
| 78 |
|
| 79 |
from ansible.module_utils.basic import AnsibleModule |
| 80 |
from ansible.module_utils._text import to_native |
| 81 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 82 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 83 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 84 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 85 |
|
| 86 |
|
| 87 |
class NetAppOntapCifsLocalUserModify(): |
| 88 |
def __init__(self): |
| 89 |
|
| 90 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 91 |
self.argument_spec.update(dict( |
| 92 |
name=dict(required=True, type='str'), |
| 93 |
vserver=dict(required=True, type='str'), |
| 94 |
is_account_disabled=dict(required=False, type='bool'), |
| 95 |
full_name=dict(required=False, type='str'), |
| 96 |
description=dict(required=False, type='str') |
| 97 |
)) |
| 98 |
|
| 99 |
self.module = AnsibleModule( |
| 100 |
argument_spec=self.argument_spec, |
| 101 |
supports_check_mode=True |
| 102 |
) |
| 103 |
|
| 104 |
self.na_helper = NetAppModule() |
| 105 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 106 |
|
| 107 |
self.rest_api = OntapRestAPI(self.module) |
| 108 |
self.use_rest = self.rest_api.is_rest() |
| 109 |
|
| 110 |
self.module.warn('This module is deprecated and na_ontap_cifs_local_user should be used instead') |
| 111 |
if not self.use_rest: |
| 112 |
if not netapp_utils.has_netapp_lib(): |
| 113 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 114 |
else: |
| 115 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 116 |
|
| 117 |
def get_cifs_local_user(self): |
| 118 |
""" |
| 119 |
Return a CIFS local user |
| 120 |
:return: None if there is no CIFS local user matching |
| 121 |
""" |
| 122 |
return_value = None |
| 123 |
if self.use_rest: |
| 124 |
api = "private/cli/vserver/cifs/users-and-groups/local-user" |
| 125 |
query = { |
| 126 |
'fields': 'user-name,full-name,is-account-disabled,description', |
| 127 |
'user-name': self.parameters['name'], |
| 128 |
'vserver': self.parameters['vserver'] |
| 129 |
} |
| 130 |
record, error = rest_generic.get_one_record(self.rest_api, api, query=query) |
| 131 |
if error: |
| 132 |
self.module.fail_json(msg=error) |
| 133 |
if record: |
| 134 |
return_value = { |
| 135 |
'name': record['user_name'], |
| 136 |
'is_account_disabled': record['is_account_disabled'], |
| 137 |
'vserver': record['vserver'], |
| 138 |
'description': record.get('description', ''), |
| 139 |
'full_name': record.get('full_name', '') |
| 140 |
} |
| 141 |
else: |
| 142 |
cifs_local_user_obj = netapp_utils.zapi.NaElement('cifs-local-user-get-iter') |
| 143 |
cifs_local_user_info = netapp_utils.zapi.NaElement('cifs-local-user') |
| 144 |
cifs_local_user_info.add_new_child('user-name', self.parameters['name']) |
| 145 |
query = netapp_utils.zapi.NaElement('query') |
| 146 |
query.add_child_elem(cifs_local_user_info) |
| 147 |
cifs_local_user_obj.add_child_elem(query) |
| 148 |
try: |
| 149 |
result = self.server.invoke_successfully(cifs_local_user_obj, True) |
| 150 |
except netapp_utils.zapi.NaApiError as error: |
| 151 |
self.module.fail_json(msg='Error getting user %s on vserver %s: %s' % |
| 152 |
(self.parameters['name'], self.parameters['vserver'], to_native(error)), exception=traceback.format_exc()) |
| 153 |
|
| 154 |
if result.get_child_by_name('attributes-list'): |
| 155 |
local_cifs_user_attributes = result['attributes-list']['cifs-local-user'] |
| 156 |
|
| 157 |
return_value = { |
| 158 |
'name': local_cifs_user_attributes['user-name'], |
| 159 |
'is_account_disabled': self.na_helper.get_value_for_bool(from_zapi=True, value=local_cifs_user_attributes['is-account-disabled']), |
| 160 |
'vserver': local_cifs_user_attributes['vserver'], |
| 161 |
'full_name': '', |
| 162 |
'description': '', |
| 163 |
} |
| 164 |
|
| 165 |
if local_cifs_user_attributes['full-name']: |
| 166 |
return_value['full_name'] = local_cifs_user_attributes['full-name'] |
| 167 |
|
| 168 |
if local_cifs_user_attributes['description']: |
| 169 |
return_value['description'] = local_cifs_user_attributes['description'] |
| 170 |
|
| 171 |
return return_value |
| 172 |
|
| 173 |
def modify_cifs_local_user(self, modify): |
| 174 |
""" |
| 175 |
Modifies a local cifs user |
| 176 |
:return: None |
| 177 |
""" |
| 178 |
if self.use_rest: |
| 179 |
api = "private/cli/vserver/cifs/users-and-groups/local-user" |
| 180 |
query = { |
| 181 |
"user-name": self.parameters['name'], |
| 182 |
'vserver': self.parameters['vserver'] |
| 183 |
} |
| 184 |
|
| 185 |
dummy, error = self.rest_api.patch(api, modify, query) |
| 186 |
if error: |
| 187 |
self.module.fail_json(msg=error, modify=modify) |
| 188 |
else: |
| 189 |
cifs_local_user_obj = netapp_utils.zapi.NaElement("cifs-local-user-modify") |
| 190 |
cifs_local_user_obj.add_new_child('user-name', self.parameters['name']) |
| 191 |
cifs_local_user_obj.add_new_child('is-account-disabled', |
| 192 |
self.na_helper.get_value_for_bool(from_zapi=False, value=self.parameters['is_account_disabled'])) |
| 193 |
|
| 194 |
if 'full_name' in self.parameters: |
| 195 |
cifs_local_user_obj.add_new_child('full-name', self.parameters['full_name']) |
| 196 |
|
| 197 |
if 'description' in self.parameters: |
| 198 |
cifs_local_user_obj.add_new_child('description', self.parameters['description']) |
| 199 |
|
| 200 |
try: |
| 201 |
self.server.invoke_successfully(cifs_local_user_obj, True) |
| 202 |
except netapp_utils.zapi.NaApiError as error: |
| 203 |
self.module.fail_json(msg="Error modifying local CIFS user %s on vserver %s: %s" % |
| 204 |
(self.parameters['name'], self.parameters['vserver'], to_native(error)), exception=traceback.format_exc()) |
| 205 |
|
| 206 |
def apply(self): |
| 207 |
current = self.get_cifs_local_user() |
| 208 |
if not current: |
| 209 |
error = "User %s does not exist on vserver %s" % (self.parameters['name'], self.parameters['vserver']) |
| 210 |
self.module.fail_json(msg=error) |
| 211 |
|
| 212 |
if self.use_rest: |
| 213 |
# name is a key, and REST does not allow to change it |
| 214 |
# it should match anyway, but REST may prepend the domain name |
| 215 |
self.parameters['name'] = current['name'] |
| 216 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 217 |
|
| 218 |
if self.na_helper.changed and not self.module.check_mode: |
| 219 |
self.modify_cifs_local_user(modify) |
| 220 |
|
| 221 |
result = netapp_utils.generate_result(self.na_helper.changed, modify=modify) |
| 222 |
self.module.exit_json(**result) |
| 223 |
|
| 224 |
|
| 225 |
def main(): |
| 226 |
""" |
| 227 |
Execute action from playbook |
| 228 |
""" |
| 229 |
command = NetAppOntapCifsLocalUserModify() |
| 230 |
command.apply() |
| 231 |
|
| 232 |
|
| 233 |
if __name__ == '__main__': |
| 234 |
main() |