| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2022-2025, NetApp, Inc. GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
DOCUMENTATION = """ |
| 9 |
module: na_ontap_security_ipsec_config |
| 10 |
short_description: NetApp ONTAP module to configure IPsec config. |
| 11 |
extends_documentation_fragment: |
| 12 |
- netapp.ontap.netapp.na_ontap_rest |
| 13 |
version_added: '22.1.0' |
| 14 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 15 |
description: |
| 16 |
- Enable or disable IPsec config. |
| 17 |
- Configure replay window. |
| 18 |
options: |
| 19 |
state: |
| 20 |
description: |
| 21 |
- modify IPsec configuration, only present is supported. |
| 22 |
choices: ['present'] |
| 23 |
type: str |
| 24 |
default: present |
| 25 |
enabled: |
| 26 |
description: |
| 27 |
- Indicates whether or not IPsec is enabled. |
| 28 |
type: bool |
| 29 |
required: false |
| 30 |
replay_window: |
| 31 |
description: |
| 32 |
- Replay window size in packets, where 0 indicates that the relay window is disabled. |
| 33 |
type: str |
| 34 |
required: false |
| 35 |
choices: ['0', '64', '128', '256', '512', '1024'] |
| 36 |
|
| 37 |
notes: |
| 38 |
- Supports check_mode. |
| 39 |
- Only supported with REST and requires ONTAP 9.8 or later. |
| 40 |
""" |
| 41 |
|
| 42 |
EXAMPLES = """ |
| 43 |
- name: Enable IPsec config and set replay_window. |
| 44 |
netapp.ontap.na_ontap_security_ipsec_config: |
| 45 |
enabled: true |
| 46 |
replay_window: 64 |
| 47 |
hostname: "{{ netapp_hostname }}" |
| 48 |
username: "{{ netapp_username }}" |
| 49 |
password: "{{ netapp_password }}" |
| 50 |
https: true |
| 51 |
validate_certs: "{{ validate_certs }}" |
| 52 |
|
| 53 |
- name: Disable IPsec config. |
| 54 |
netapp.ontap.na_ontap_security_ipsec_config: |
| 55 |
enabled: false |
| 56 |
replay_window: 64 |
| 57 |
hostname: "{{ netapp_hostname }}" |
| 58 |
username: "{{ netapp_username }}" |
| 59 |
password: "{{ netapp_password }}" |
| 60 |
https: true |
| 61 |
validate_certs: "{{ validate_certs }}" |
| 62 |
""" |
| 63 |
|
| 64 |
RETURN = """ |
| 65 |
""" |
| 66 |
|
| 67 |
import traceback |
| 68 |
from ansible.module_utils.basic import AnsibleModule |
| 69 |
from ansible.module_utils._text import to_native |
| 70 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 71 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 72 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 73 |
|
| 74 |
|
| 75 |
class NetAppOntapSecurityIPsecConfig: |
| 76 |
def __init__(self): |
| 77 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 78 |
self.argument_spec.update(dict( |
| 79 |
state=dict(required=False, type='str', choices=['present'], default='present'), |
| 80 |
enabled=dict(required=False, type='bool'), |
| 81 |
replay_window=dict(required=False, type='str', choices=['0', '64', '128', '256', '512', '1024']) |
| 82 |
)) |
| 83 |
self.module = AnsibleModule( |
| 84 |
argument_spec=self.argument_spec, |
| 85 |
supports_check_mode=True |
| 86 |
) |
| 87 |
self.uuid = None |
| 88 |
self.na_helper = NetAppModule(self.module) |
| 89 |
self.parameters = self.na_helper.check_and_set_parameters(self.module) |
| 90 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 91 |
self.rest_api.fail_if_not_rest_minimum_version('na_ontap_security_ipsec_config:', 9, 8) |
| 92 |
|
| 93 |
def get_security_ipsec_config(self): |
| 94 |
"""Get IPsec config details""" |
| 95 |
record, error = rest_generic.get_one_record(self.rest_api, 'security/ipsec', None, 'enabled,replay_window') |
| 96 |
if error: |
| 97 |
self.module.fail_json(msg="Error fetching security IPsec config: %s" % to_native(error), exception=traceback.format_exc()) |
| 98 |
if record: |
| 99 |
return { |
| 100 |
'enabled': record.get('enabled'), |
| 101 |
'replay_window': record.get('replay_window') |
| 102 |
} |
| 103 |
return None |
| 104 |
|
| 105 |
def modify_security_ipsec_config(self, modify): |
| 106 |
""" |
| 107 |
Modify security ipsec config |
| 108 |
""" |
| 109 |
dummy, error = rest_generic.patch_async(self.rest_api, 'security/ipsec', None, modify) |
| 110 |
if error: |
| 111 |
self.module.fail_json(msg='Error modifying security IPsec config: %s.' % to_native(error), exception=traceback.format_exc()) |
| 112 |
|
| 113 |
def apply(self): |
| 114 |
modify = self.na_helper.get_modified_attributes(self.get_security_ipsec_config(), self.parameters) |
| 115 |
if self.na_helper.changed and not self.module.check_mode: |
| 116 |
self.modify_security_ipsec_config(modify) |
| 117 |
result = netapp_utils.generate_result(self.na_helper.changed, modify=modify) |
| 118 |
self.module.exit_json(**result) |
| 119 |
|
| 120 |
|
| 121 |
def main(): |
| 122 |
ipsec_config = NetAppOntapSecurityIPsecConfig() |
| 123 |
ipsec_config.apply() |
| 124 |
|
| 125 |
|
| 126 |
if __name__ == '__main__': |
| 127 |
main() |