| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2022-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 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
module: na_ontap_cifs_local_user |
| 12 |
short_description: NetApp ONTAP local CIFS user. |
| 13 |
extends_documentation_fragment: |
| 14 |
- netapp.ontap.netapp.na_ontap |
| 15 |
version_added: '22.2.0' |
| 16 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 17 |
|
| 18 |
description: |
| 19 |
- Create/Modify/Delete a local CIFS user |
| 20 |
options: |
| 21 |
state: |
| 22 |
choices: ['present', 'absent'] |
| 23 |
description: |
| 24 |
- Whether the specified CIFS share should exist or not. |
| 25 |
type: str |
| 26 |
default: present |
| 27 |
|
| 28 |
name: |
| 29 |
description: |
| 30 |
- The name of the local cifs user |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
|
| 34 |
vserver: |
| 35 |
description: |
| 36 |
- the name of the data vserver to use. |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
|
| 40 |
account_disabled: |
| 41 |
description: |
| 42 |
- Whether the local cifs user is disabled or not |
| 43 |
type: bool |
| 44 |
|
| 45 |
description: |
| 46 |
description: |
| 47 |
- the description for the local cifs user |
| 48 |
type: str |
| 49 |
|
| 50 |
full_name: |
| 51 |
description: |
| 52 |
- the full name for the local cifs user |
| 53 |
type: str |
| 54 |
|
| 55 |
user_password: |
| 56 |
description: |
| 57 |
- Password for new user |
| 58 |
type: str |
| 59 |
|
| 60 |
set_password: |
| 61 |
description: |
| 62 |
- Modify the existing user password |
| 63 |
- Module is not idempotent when set to True |
| 64 |
type: bool |
| 65 |
default: False |
| 66 |
|
| 67 |
lambda_config: |
| 68 |
description: |
| 69 |
- Configuration parameters for AWS Lambda proxy functionality. |
| 70 |
- These option and suboptions are only supported with REST. |
| 71 |
type: dict |
| 72 |
version_added: 23.4.0 |
| 73 |
suboptions: |
| 74 |
function_name: |
| 75 |
description: |
| 76 |
- The name of the AWS Lambda function to invoke. |
| 77 |
type: str |
| 78 |
required: true |
| 79 |
aws_region: |
| 80 |
description: |
| 81 |
- The name of the AWS region. |
| 82 |
type: str |
| 83 |
required: true |
| 84 |
aws_profile: |
| 85 |
description: |
| 86 |
- The name of the AWS profile to use for authentication. |
| 87 |
type: str |
| 88 |
|
| 89 |
notes: |
| 90 |
- Supports AWS Lambda proxy functionality when using REST. See the README file for examples. |
| 91 |
''' |
| 92 |
|
| 93 |
EXAMPLES = """ |
| 94 |
- name: Create local cifs user |
| 95 |
netapp.ontap.na_ontap_cifs_local_user: |
| 96 |
state: present |
| 97 |
vserver: ansibleSVM_cifs |
| 98 |
name: carchi-cifs2 |
| 99 |
user_password: mypassword |
| 100 |
account_disabled: false |
| 101 |
full_name: Chris Archibald |
| 102 |
description: A user account for Chris |
| 103 |
|
| 104 |
- name: Modify local cifs user |
| 105 |
netapp.ontap.na_ontap_cifs_local_user: |
| 106 |
state: present |
| 107 |
vserver: ansibleSVM_cifs |
| 108 |
name: carchi-cifs2 |
| 109 |
account_disabled: false |
| 110 |
full_name: Christopher Archibald |
| 111 |
description: A user account for Chris Archibald |
| 112 |
|
| 113 |
- name: Change local cifs user password |
| 114 |
netapp.ontap.na_ontap_cifs_local_user: |
| 115 |
state: present |
| 116 |
vserver: ansibleSVM_cifs |
| 117 |
name: carchi-cifs2 |
| 118 |
user_password: mypassword2 |
| 119 |
set_password: true |
| 120 |
account_disabled: false |
| 121 |
full_name: Christopher Archibald |
| 122 |
description: A user account for Chris Archibald |
| 123 |
|
| 124 |
- name: Delete local cifs user |
| 125 |
netapp.ontap.na_ontap_cifs_local_user: |
| 126 |
state: absent |
| 127 |
vserver: ansibleSVM_cifs |
| 128 |
name: carchi-cifs2 |
| 129 |
""" |
| 130 |
|
| 131 |
RETURN = """ |
| 132 |
|
| 133 |
""" |
| 134 |
|
| 135 |
import traceback |
| 136 |
|
| 137 |
from ansible.module_utils.basic import AnsibleModule |
| 138 |
from ansible.module_utils._text import to_native |
| 139 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 140 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 141 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 142 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic, rest_vserver |
| 143 |
|
| 144 |
|
| 145 |
class NetAppOntapCifsLocalUser: |
| 146 |
def __init__(self): |
| 147 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 148 |
self.argument_spec.update(dict( |
| 149 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 150 |
name=dict(required=True, type='str'), |
| 151 |
vserver=dict(required=True, type='str'), |
| 152 |
account_disabled=dict(required=False, type='bool'), |
| 153 |
full_name=dict(required=False, type='str'), |
| 154 |
description=dict(required=False, type='str'), |
| 155 |
user_password=dict(required=False, type='str', no_log=True), |
| 156 |
set_password=dict(required=False, type='bool', default=False) |
| 157 |
)) |
| 158 |
self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec()) |
| 159 |
self.module = AnsibleModule( |
| 160 |
argument_spec=self.argument_spec, |
| 161 |
supports_check_mode=True, |
| 162 |
required_if=[ |
| 163 |
('use_lambda', True, ['lambda_config']) |
| 164 |
], |
| 165 |
) |
| 166 |
self.svm_uuid = None |
| 167 |
self.sid = None |
| 168 |
|
| 169 |
self.na_helper = NetAppModule() |
| 170 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 171 |
|
| 172 |
self.rest_api = OntapRestAPI(self.module) |
| 173 |
self.rest_api.fail_if_not_rest_minimum_version('na_ontap_cifs_local_user', 9, 10, 1) |
| 174 |
|
| 175 |
def get_cifs_local_user(self): |
| 176 |
self.get_svm_uuid() |
| 177 |
api = 'protocols/cifs/local-users' |
| 178 |
fields = 'account_disabled,description,full_name,name,sid' |
| 179 |
params = {'svm.uuid': self.svm_uuid, 'name': self.parameters['name'], 'fields': fields} |
| 180 |
record, error = rest_generic.get_one_record(self.rest_api, api, params) |
| 181 |
if error: |
| 182 |
self.module.fail_json(msg='Error fetching cifs/local-user %s: %s' % (self.parameters['name'], to_native(error)), |
| 183 |
exception=traceback.format_exc()) |
| 184 |
if record: |
| 185 |
return self.format_record(record) |
| 186 |
return None |
| 187 |
|
| 188 |
def get_svm_uuid(self): |
| 189 |
self.svm_uuid, dummy = rest_vserver.get_vserver_uuid(self.rest_api, self.parameters['vserver'], self.module, True) |
| 190 |
|
| 191 |
def format_record(self, record): |
| 192 |
self.sid = record['sid'] |
| 193 |
try: |
| 194 |
record['name'] = record['name'].split('\\')[1] |
| 195 |
except SyntaxError: |
| 196 |
self.module.fail_json(msg='Error fetching cifs/local-user') |
| 197 |
return record |
| 198 |
|
| 199 |
def create_cifs_local_user(self): |
| 200 |
api = 'protocols/cifs/local-users' |
| 201 |
body = { |
| 202 |
'svm.uuid': self.svm_uuid, |
| 203 |
'name': self.parameters['name'], |
| 204 |
} |
| 205 |
if self.parameters.get('user_password') is not None: |
| 206 |
body['password'] = self.parameters['user_password'] |
| 207 |
if self.parameters.get('full_name') is not None: |
| 208 |
body['full_name'] = self.parameters['full_name'] |
| 209 |
if self.parameters.get('description') is not None: |
| 210 |
body['description'] = self.parameters['description'] |
| 211 |
if self.parameters.get('account_disabled') is not None: |
| 212 |
body['account_disabled'] = self.parameters['account_disabled'] |
| 213 |
dummy, error = rest_generic.post_async(self.rest_api, api, body) |
| 214 |
if error: |
| 215 |
self.module.fail_json(msg="Error creating CIFS local users with name %s: %s" % (self.parameters['name'], error)) |
| 216 |
|
| 217 |
def delete_cifs_local_user(self): |
| 218 |
api = 'protocols/cifs/local-users' |
| 219 |
uuids = '%s/%s' % (self.svm_uuid, self.sid) |
| 220 |
dummy, error = rest_generic.delete_async(self.rest_api, api, uuids) |
| 221 |
if error: |
| 222 |
self.module.fail_json(msg='Error while deleting CIFS local user: %s' % error) |
| 223 |
|
| 224 |
def modify_cifs_local_user(self, modify): |
| 225 |
api = 'protocols/cifs/local-users' |
| 226 |
uuids = '%s/%s' % (self.svm_uuid, self.sid) |
| 227 |
body = {} |
| 228 |
if modify.get('full_name') is not None: |
| 229 |
body['full_name'] = self.parameters['full_name'] |
| 230 |
if modify.get('description') is not None: |
| 231 |
body['description'] = self.parameters['description'] |
| 232 |
if modify.get('account_disabled') is not None: |
| 233 |
body['account_disabled'] = self.parameters['account_disabled'] |
| 234 |
if self.parameters['set_password'] and modify.get('user_password') is not None: |
| 235 |
body['password'] = self.parameters['user_password'] |
| 236 |
dummy, error = rest_generic.patch_async(self.rest_api, api, uuids, body) |
| 237 |
if error: |
| 238 |
self.module.fail_json(msg='Error while modifying CIFS local user: %s' % error) |
| 239 |
|
| 240 |
def apply(self): |
| 241 |
current = self.get_cifs_local_user() |
| 242 |
modify = None |
| 243 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 244 |
if cd_action is None and self.parameters['state'] == 'present': |
| 245 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 246 |
if self.parameters['set_password'] and self.parameters.get('user_password') is not None: |
| 247 |
if not modify: |
| 248 |
modify = {} |
| 249 |
self.na_helper.changed = True |
| 250 |
modify.update({'user_password': self.parameters['user_password']}) |
| 251 |
self.module.warn("forcing a password change as set_password is true") |
| 252 |
if self.na_helper.changed and not self.module.check_mode: |
| 253 |
if cd_action == 'create': |
| 254 |
self.create_cifs_local_user() |
| 255 |
elif cd_action == 'delete': |
| 256 |
self.delete_cifs_local_user() |
| 257 |
elif modify: |
| 258 |
self.modify_cifs_local_user(modify) |
| 259 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify) |
| 260 |
self.module.exit_json(**result) |
| 261 |
|
| 262 |
|
| 263 |
def main(): |
| 264 |
""" |
| 265 |
Execute action from playbook |
| 266 |
""" |
| 267 |
command = NetAppOntapCifsLocalUser() |
| 268 |
command.apply() |
| 269 |
|
| 270 |
|
| 271 |
if __name__ == '__main__': |
| 272 |
main() |