| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2020-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 = { |
| 11 |
'metadata_version': '1.1', |
| 12 |
'status': ['preview'], |
| 13 |
'supported_by': 'certified' |
| 14 |
} |
| 15 |
|
| 16 |
DOCUMENTATION = """ |
| 17 |
|
| 18 |
module: na_ontap_file_directory_policy |
| 19 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 20 |
short_description: NetApp ONTAP create, delete, or modify vserver security file-directory policy |
| 21 |
extends_documentation_fragment: |
| 22 |
- netapp.ontap.netapp.na_ontap_zapi |
| 23 |
version_added: 20.8.0 |
| 24 |
description: |
| 25 |
- Create, modify, or destroy vserver security file-directory policy |
| 26 |
- Add or remove task from policy. |
| 27 |
- Each time a policy/task is created/modified, automatically apply policy to vserver. |
| 28 |
options: |
| 29 |
state: |
| 30 |
description: |
| 31 |
- Whether the specified policy or task should exist or not. |
| 32 |
choices: ['present', 'absent'] |
| 33 |
default: present |
| 34 |
type: str |
| 35 |
|
| 36 |
vserver: |
| 37 |
description: |
| 38 |
- Specifies the vserver for the policy. |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
|
| 42 |
policy_name: |
| 43 |
description: |
| 44 |
- Specifies the name of the policy. |
| 45 |
type: str |
| 46 |
required: true |
| 47 |
|
| 48 |
access_control: |
| 49 |
description: |
| 50 |
- Specifies the access control of task to be applied. |
| 51 |
choices: ['file_directory', 'slag'] |
| 52 |
type: str |
| 53 |
|
| 54 |
ntfs_mode: |
| 55 |
description: |
| 56 |
- Specifies NTFS Propagation Mode. |
| 57 |
choices: ['propagate', 'ignore', 'replace'] |
| 58 |
type: str |
| 59 |
|
| 60 |
ntfs_sd: |
| 61 |
description: |
| 62 |
- Specifies NTFS security descriptor identifier. |
| 63 |
type: list |
| 64 |
elements: str |
| 65 |
|
| 66 |
path: |
| 67 |
description: |
| 68 |
- Specifies the file or folder path of the task. |
| 69 |
- If path is specified and the policy which the task is adding to, does not exist, it will create the policy first then add the task to it. |
| 70 |
- If path is specified, delete operation only removes task from policy. |
| 71 |
type: str |
| 72 |
|
| 73 |
security_type: |
| 74 |
description: |
| 75 |
- Specifies the type of security. |
| 76 |
type: str |
| 77 |
choices: ['ntfs', 'nfsv4'] |
| 78 |
|
| 79 |
ignore_broken_symlinks: |
| 80 |
description: |
| 81 |
- Skip Broken Symlinks. |
| 82 |
- Options used when applying the policy to vserver. |
| 83 |
type: bool |
| 84 |
|
| 85 |
""" |
| 86 |
|
| 87 |
EXAMPLES = """ |
| 88 |
- name: Create policy |
| 89 |
netapp.ontap.na_ontap_file_directory_policy: |
| 90 |
hostname: "{{ netapp_hostname }}" |
| 91 |
username: "{{ netapp_username }}" |
| 92 |
password: "{{ netapp_password }}" |
| 93 |
state: present |
| 94 |
vserver: ansible |
| 95 |
policy_name: file_policy |
| 96 |
ignore_broken_symlinks: false |
| 97 |
|
| 98 |
- name: Add task to existing file_policy |
| 99 |
netapp.ontap.na_ontap_file_directory_policy: |
| 100 |
hostname: "{{ netapp_hostname }}" |
| 101 |
username: "{{ netapp_username }}" |
| 102 |
password: "{{ netapp_password }}" |
| 103 |
state: present |
| 104 |
vserver: ansible |
| 105 |
policy_name: file_policy |
| 106 |
path: /vol |
| 107 |
ntfs_sd: ansible_sd |
| 108 |
ntfs_mode: propagate |
| 109 |
|
| 110 |
- name: Delete task from file_policy. |
| 111 |
netapp.ontap.na_ontap_file_directory_policy: |
| 112 |
hostname: "{{ netapp_hostname }}" |
| 113 |
username: "{{ netapp_username }}" |
| 114 |
password: "{{ netapp_password }}" |
| 115 |
state: absent |
| 116 |
vserver: ansible |
| 117 |
policy_name: file_policy |
| 118 |
path: /vol |
| 119 |
|
| 120 |
- name: Delete file_policy along with the tasks. |
| 121 |
netapp.ontap.na_ontap_file_directory_policy: |
| 122 |
hostname: "{{ hostname }}" |
| 123 |
username: "{{ username }}" |
| 124 |
password: "{{ password }}" |
| 125 |
state: absent |
| 126 |
vserver: ansible |
| 127 |
policy_name: file_policy |
| 128 |
""" |
| 129 |
|
| 130 |
RETURN = """ |
| 131 |
""" |
| 132 |
|
| 133 |
import traceback |
| 134 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 135 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 136 |
from ansible.module_utils.basic import AnsibleModule |
| 137 |
from ansible.module_utils._text import to_native |
| 138 |
|
| 139 |
HAS_NETAPP_LIB = netapp_utils.has_netapp_lib() |
| 140 |
|
| 141 |
|
| 142 |
class NetAppOntapFilePolicy(object): |
| 143 |
|
| 144 |
def __init__(self): |
| 145 |
""" |
| 146 |
Initialize the Ontap file directory policy class |
| 147 |
""" |
| 148 |
|
| 149 |
self.argument_spec = netapp_utils.na_ontap_zapi_only_spec() |
| 150 |
self.argument_spec.update(dict( |
| 151 |
state=dict(required=False, choices=['present', 'absent'], default='present'), |
| 152 |
vserver=dict(required=True, type='str'), |
| 153 |
policy_name=dict(required=True, type='str'), |
| 154 |
access_control=dict(required=False, type='str', choices=['file_directory', 'slag']), |
| 155 |
ntfs_mode=dict(required=False, choices=['propagate', 'ignore', 'replace']), |
| 156 |
ntfs_sd=dict(required=False, type='list', elements='str'), |
| 157 |
path=dict(required=False, type='str'), |
| 158 |
security_type=dict(required=False, type='str', choices=['ntfs', 'nfsv4']), |
| 159 |
ignore_broken_symlinks=dict(required=False, type='bool') |
| 160 |
)) |
| 161 |
|
| 162 |
self.module = AnsibleModule( |
| 163 |
argument_spec=self.argument_spec, |
| 164 |
supports_check_mode=True, |
| 165 |
) |
| 166 |
|
| 167 |
# set up variables |
| 168 |
self.na_helper = NetAppModule() |
| 169 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 170 |
|
| 171 |
if HAS_NETAPP_LIB is False: |
| 172 |
self.module.fail_json(msg='The python NetApp-Lib module is required') |
| 173 |
else: |
| 174 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 175 |
|
| 176 |
def create_policy(self): |
| 177 |
policy_obj = netapp_utils.zapi.NaElement("file-directory-security-policy-create") |
| 178 |
policy_obj.add_new_child('policy-name', self.parameters['policy_name']) |
| 179 |
try: |
| 180 |
self.server.invoke_successfully(policy_obj, True) |
| 181 |
except netapp_utils.zapi.NaApiError as error: |
| 182 |
self.module.fail_json( |
| 183 |
msg='Error creating file-directory policy %s: %s' % (self.parameters['policy_name'], to_native(error)), |
| 184 |
exception=traceback.format_exc()) |
| 185 |
|
| 186 |
def get_policy_iter(self): |
| 187 |
policy_get_iter = netapp_utils.zapi.NaElement('file-directory-security-policy-get-iter') |
| 188 |
policy_info = netapp_utils.zapi.NaElement('file-directory-security-policy') |
| 189 |
policy_info.add_new_child('vserver', self.parameters['vserver']) |
| 190 |
policy_info.add_new_child('policy-name', self.parameters['policy_name']) |
| 191 |
query = netapp_utils.zapi.NaElement('query') |
| 192 |
query.add_child_elem(policy_info) |
| 193 |
policy_get_iter.add_child_elem(query) |
| 194 |
try: |
| 195 |
result = self.server.invoke_successfully(policy_get_iter, True) |
| 196 |
except netapp_utils.zapi.NaApiError as error: |
| 197 |
self.module.fail_json(msg='Error fetching file-directory policy %s: %s' |
| 198 |
% (self.parameters['policy_name'], to_native(error)), |
| 199 |
exception=traceback.format_exc()) |
| 200 |
if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1: |
| 201 |
attributes_list = result.get_child_by_name('attributes-list') |
| 202 |
policy = attributes_list.get_child_by_name('file-directory-security-policy') |
| 203 |
return policy.get_child_content('policy-name') |
| 204 |
return None |
| 205 |
|
| 206 |
def remove_policy(self): |
| 207 |
remove_policy = netapp_utils.zapi.NaElement('file-directory-security-policy-delete') |
| 208 |
remove_policy.add_new_child('policy-name', self.parameters['policy_name']) |
| 209 |
try: |
| 210 |
self.server.invoke_successfully(remove_policy, True) |
| 211 |
except netapp_utils.zapi.NaApiError as error: |
| 212 |
self.module.fail_json( |
| 213 |
msg='Error removing file-directory policy %s: %s' % (self.parameters['policy_name'], to_native(error)), |
| 214 |
exception=traceback.format_exc()) |
| 215 |
|
| 216 |
def get_task_iter(self): |
| 217 |
task_get_iter = netapp_utils.zapi.NaElement('file-directory-security-policy-task-get-iter') |
| 218 |
task_info = netapp_utils.zapi.NaElement('file-directory-security-policy-task') |
| 219 |
task_info.add_new_child('vserver', self.parameters['vserver']) |
| 220 |
task_info.add_new_child('policy-name', self.parameters['policy_name']) |
| 221 |
task_info.add_new_child('path', self.parameters['path']) |
| 222 |
query = netapp_utils.zapi.NaElement('query') |
| 223 |
query.add_child_elem(task_info) |
| 224 |
task_get_iter.add_child_elem(query) |
| 225 |
try: |
| 226 |
result = self.server.invoke_successfully(task_get_iter, True) |
| 227 |
except netapp_utils.zapi.NaApiError as error: |
| 228 |
self.module.fail_json(msg='Error fetching task from file-directory policy %s: %s' |
| 229 |
% (self.parameters['policy_name'], to_native(error)), |
| 230 |
exception=traceback.format_exc()) |
| 231 |
if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1: |
| 232 |
attributes_list = result.get_child_by_name('attributes-list') |
| 233 |
task = attributes_list.get_child_by_name('file-directory-security-policy-task') |
| 234 |
task_result = dict() |
| 235 |
task_result['path'] = task.get_child_content('path') |
| 236 |
if task.get_child_by_name('ntfs-mode'): |
| 237 |
task_result['ntfs_mode'] = task.get_child_content('ntfs-mode') |
| 238 |
if task.get_child_by_name('security-type'): |
| 239 |
task_result['security_type'] = task.get_child_content('security-type') |
| 240 |
if task.get_child_by_name('ntfs-sd'): |
| 241 |
task_result['ntfs_sd'] = [ntfs_sd.get_content() for ntfs_sd in task.get_child_by_name('ntfs-sd').get_children()] |
| 242 |
return task_result |
| 243 |
return None |
| 244 |
|
| 245 |
def add_task_to_policy(self): |
| 246 |
policy_add_task = netapp_utils.zapi.NaElement('file-directory-security-policy-task-add') |
| 247 |
policy_add_task.add_new_child('path', self.parameters['path']) |
| 248 |
policy_add_task.add_new_child('policy-name', self.parameters['policy_name']) |
| 249 |
if self.parameters.get('access_control') is not None: |
| 250 |
policy_add_task.add_new_child('access-control', self.parameters['access_control']) |
| 251 |
if self.parameters.get('ntfs_mode') is not None: |
| 252 |
policy_add_task.add_new_child('ntfs-mode', self.parameters['ntfs_mode']) |
| 253 |
if self.parameters.get('ntfs_sd') is not None: |
| 254 |
ntfs_sds = netapp_utils.zapi.NaElement('ntfs-sd') |
| 255 |
for ntfs_sd in self.parameters['ntfs_sd']: |
| 256 |
ntfs_sds.add_new_child('file-security-ntfs-sd', ntfs_sd) |
| 257 |
policy_add_task.add_child_elem(ntfs_sds) |
| 258 |
if self.parameters.get('security_type') is not None: |
| 259 |
policy_add_task.add_new_child('security-type', self.parameters['security_type']) |
| 260 |
try: |
| 261 |
self.server.invoke_successfully(policy_add_task, True) |
| 262 |
except netapp_utils.zapi.NaApiError as error: |
| 263 |
self.module.fail_json(msg='Error adding task to file-directory policy %s: %s' |
| 264 |
% (self.parameters['policy_name'], to_native(error)), |
| 265 |
exception=traceback.format_exc()) |
| 266 |
|
| 267 |
def remove_task_from_policy(self): |
| 268 |
policy_remove_task = netapp_utils.zapi.NaElement('file-directory-security-policy-task-remove') |
| 269 |
policy_remove_task.add_new_child('path', self.parameters['path']) |
| 270 |
policy_remove_task.add_new_child('policy-name', self.parameters['policy_name']) |
| 271 |
try: |
| 272 |
self.server.invoke_successfully(policy_remove_task, True) |
| 273 |
except netapp_utils.zapi.NaApiError as error: |
| 274 |
self.module.fail_json(msg='Error removing task from file-directory policy %s: %s' |
| 275 |
% (self.parameters['policy_name'], to_native(error)), |
| 276 |
exception=traceback.format_exc()) |
| 277 |
|
| 278 |
def modify_task(self, modify): |
| 279 |
policy_modify_task = netapp_utils.zapi.NaElement('file-directory-security-policy-task-modify') |
| 280 |
policy_modify_task.add_new_child('path', self.parameters['path']) |
| 281 |
policy_modify_task.add_new_child('policy-name', self.parameters['policy_name']) |
| 282 |
if modify.get('ntfs_mode') is not None: |
| 283 |
policy_modify_task.add_new_child('ntfs-mode', self.parameters['ntfs_mode']) |
| 284 |
if modify.get('ntfs_sd') is not None: |
| 285 |
ntfs_sds = netapp_utils.zapi.NaElement('ntfs-sd') |
| 286 |
for ntfs_sd in self.parameters['ntfs_sd']: |
| 287 |
ntfs_sds.add_new_child('file-security-ntfs-sd', ntfs_sd) |
| 288 |
policy_modify_task.add_child_elem(ntfs_sds) |
| 289 |
if modify.get('security_type') is not None: |
| 290 |
policy_modify_task.add_new_child('security-type', self.parameters['security_type']) |
| 291 |
try: |
| 292 |
self.server.invoke_successfully(policy_modify_task, True) |
| 293 |
except netapp_utils.zapi.NaApiError as error: |
| 294 |
self.module.fail_json(msg='Error modifying task in file-directory policy %s: %s' |
| 295 |
% (self.parameters['policy_name'], to_native(error)), |
| 296 |
exception=traceback.format_exc()) |
| 297 |
|
| 298 |
def set_sd(self): |
| 299 |
set_sd = netapp_utils.zapi.NaElement('file-directory-security-set') |
| 300 |
set_sd.add_new_child('policy-name', self.parameters['policy_name']) |
| 301 |
if self.parameters.get('ignore-broken-symlinks'): |
| 302 |
set_sd.add_new_child('ignore-broken-symlinks', str(self.parameters['ignore_broken_symlinks'])) |
| 303 |
try: |
| 304 |
self.server.invoke_successfully(set_sd, True) |
| 305 |
except netapp_utils.zapi.NaApiError as error: |
| 306 |
self.module.fail_json(msg='Error applying file-directory policy %s: %s' |
| 307 |
% (self.parameters['policy_name'], to_native(error)), |
| 308 |
exception=traceback.format_exc()) |
| 309 |
|
| 310 |
def apply(self): |
| 311 |
current = self.get_policy_iter() |
| 312 |
cd_action, task_cd_action, task_modify = None, None, None |
| 313 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 314 |
if self.parameters.get('path'): |
| 315 |
current_task = self.get_task_iter() |
| 316 |
task_cd_action = self.na_helper.get_cd_action(current_task, self.parameters) |
| 317 |
if task_cd_action is None and self.parameters['state'] == 'present': |
| 318 |
task_modify = self.na_helper.get_modified_attributes(current_task, self.parameters) |
| 319 |
if self.na_helper.changed: |
| 320 |
if self.module.check_mode: |
| 321 |
pass |
| 322 |
else: |
| 323 |
if self.parameters.get('path'): |
| 324 |
if task_cd_action == 'create': |
| 325 |
# if policy doesn't exist, create the policy first. |
| 326 |
if cd_action == 'create': |
| 327 |
self.create_policy() |
| 328 |
self.add_task_to_policy() |
| 329 |
self.set_sd() |
| 330 |
elif task_cd_action == 'delete': |
| 331 |
# delete the task, not the policy. |
| 332 |
self.remove_task_from_policy() |
| 333 |
elif task_modify: |
| 334 |
self.modify_task(task_modify) |
| 335 |
self.set_sd() |
| 336 |
else: |
| 337 |
if cd_action == 'create': |
| 338 |
self.create_policy() |
| 339 |
self.set_sd() |
| 340 |
elif cd_action == 'delete': |
| 341 |
self.remove_policy() |
| 342 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, extra_responses={'task action': task_cd_action, |
| 343 |
'task modify': task_modify}) |
| 344 |
self.module.exit_json(**result) |
| 345 |
|
| 346 |
|
| 347 |
def main(): |
| 348 |
""" |
| 349 |
Creates, deletes and modifies file directory policy |
| 350 |
""" |
| 351 |
obj = NetAppOntapFilePolicy() |
| 352 |
obj.apply() |
| 353 |
|
| 354 |
|
| 355 |
if __name__ == '__main__': |
| 356 |
main() |