Debian Codegraph @

ansible_collections/netapp/ontap/plugins/modules/na_ontap_svm_options.py

raw
1 #!/usr/bin/python
2
3 # (c) 2018-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 __metaclass__ = type
8
9 ANSIBLE_METADATA = {'metadata_version': '1.1',
10 'status': ['preview'],
11 'supported_by': 'community'}
12
13 DOCUMENTATION = '''
14 short_description: NetApp ONTAP Modify SVM Options
15 author: NetApp Ansible Team (@carchi8py) <[email protected]>
16 description:
17 - Modify ONTAP SVM Options.
18 - Only Options that appear on "vserver options show" can be set.
19 extends_documentation_fragment:
20 - netapp.ontap.netapp.na_ontap_zapi
21 module: na_ontap_svm_options
22 version_added: 2.7.0
23 options:
24 name:
25 description:
26 - Name of the option.
27 type: str
28 value:
29 description:
30 - Value of the option.
31 - Value must be in quote
32 type: str
33 vserver:
34 description:
35 - The name of the vserver to which this option belongs to.
36 required: True
37 type: str
38 '''
39
40 EXAMPLES = """
41 - name: Set SVM Options
42 netapp.ontap.na_ontap_svm_options:
43 vserver: "{{ netapp_vserver_name }}"
44 hostname: "{{ netapp_hostname }}"
45 username: "{{ netapp_username }}"
46 password: "{{ netapp_password }}"
47 name: snmp.enable
48 value: 'on'
49 """
50
51 RETURN = """
52 """
53
54 import traceback
55
56 from ansible.module_utils.basic import AnsibleModule
57 from ansible.module_utils._text import to_native
58 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
59 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
60
61 HAS_NETAPP_LIB = netapp_utils.has_netapp_lib()
62
63
64 class NetAppONTAPSvnOptions(object):
65
66 def __init__(self):
67 self.argument_spec = netapp_utils.na_ontap_zapi_only_spec()
68 self.argument_spec.update(dict(
69 name=dict(required=False, type="str", default=None),
70 value=dict(required=False, type='str', default=None),
71 vserver=dict(required=True, type='str')
72
73 ))
74
75 self.module = AnsibleModule(
76 argument_spec=self.argument_spec,
77 supports_check_mode=True
78 )
79
80 self.na_helper = NetAppModule()
81 self.parameters = self.na_helper.set_parameters(self.module.params)
82 if HAS_NETAPP_LIB is False:
83 self.module.fail_json(msg="the python NetApp-Lib module is required")
84 else:
85 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver'])
86 return
87
88 def set_options(self):
89 """
90 Set a specific option
91 :return: None
92 """
93 option_obj = netapp_utils.zapi.NaElement("options-set")
94 option_obj.add_new_child('name', self.parameters['name'])
95 option_obj.add_new_child('value', self.parameters['value'])
96 try:
97 self.server.invoke_successfully(option_obj, True)
98 except netapp_utils.zapi.NaApiError as error:
99 self.module.fail_json(msg="Error setting options: %s" % to_native(error), exception=traceback.format_exc())
100
101 def list_options(self):
102 """
103 List all Options on the Vserver
104 :return: None
105 """
106 option_obj = netapp_utils.zapi.NaElement("options-list-info")
107 try:
108 self.server.invoke_successfully(option_obj, True)
109 except netapp_utils.zapi.NaApiError as error:
110 self.module.fail_json(msg="Error getting options: %s" % to_native(error), exception=traceback.format_exc())
111
112 def is_option_set(self):
113 """
114 Checks to see if an option is set or not
115 :return: If option is set return True, else return False
116 """
117 option_obj = netapp_utils.zapi.NaElement("options-get-iter")
118 options_info = netapp_utils.zapi.NaElement("option-info")
119 if self.parameters.get('name') is not None:
120 options_info.add_new_child("name", self.parameters['name'])
121 if self.parameters.get('value') is not None:
122 options_info.add_new_child("value", self.parameters['value'])
123 if "vserver" in self.parameters.keys():
124 if self.parameters['vserver'] is not None:
125 options_info.add_new_child("vserver", self.parameters['vserver'])
126 query = netapp_utils.zapi.NaElement("query")
127 query.add_child_elem(options_info)
128 option_obj.add_child_elem(query)
129 try:
130 result = self.server.invoke_successfully(option_obj, True)
131 except netapp_utils.zapi.NaApiError as error:
132 self.module.fail_json(msg="Error finding option: %s" % to_native(error), exception=traceback.format_exc())
133
134 if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1:
135 return True
136 return False
137
138 def apply(self):
139 changed = False
140 is_set = self.is_option_set()
141 if not is_set:
142 if self.module.check_mode:
143 pass
144 else:
145 self.set_options()
146 changed = True
147 self.module.exit_json(changed=changed)
148
149
150 def main():
151 """
152 Execute action from playbook
153 :return: none
154 """
155 cg_obj = NetAppONTAPSvnOptions()
156 cg_obj.apply()
157
158
159 if __name__ == '__main__':
160 main()