| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2024, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
--- |
| 11 |
module: na_santricity_syslog |
| 12 |
short_description: NetApp E-Series manage syslog settings |
| 13 |
description: |
| 14 |
- Allow the syslog settings to be configured for an individual E-Series storage-system |
| 15 |
author: |
| 16 |
- Nathan Swartz (@swartzn) |
| 17 |
- Vu Tran (@VuTran007) |
| 18 |
extends_documentation_fragment: |
| 19 |
- netapp_eseries.santricity.santricity.santricity_doc |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- Add or remove the syslog server configuration for E-Series storage array. |
| 24 |
- Existing syslog server configuration will be removed or updated when its address matches I(address). |
| 25 |
- Fully qualified hostname that resolve to an IPv4 address that matches I(address) will not be |
| 26 |
treated as a match. |
| 27 |
type: str |
| 28 |
choices: |
| 29 |
- present |
| 30 |
- absent |
| 31 |
default: present |
| 32 |
required: false |
| 33 |
address: |
| 34 |
description: |
| 35 |
- The syslog server's IPv4 address or a fully qualified hostname. |
| 36 |
- All existing syslog configurations will be removed when I(state=absent) and I(address=None). |
| 37 |
type: str |
| 38 |
required: false |
| 39 |
port: |
| 40 |
description: |
| 41 |
- This is the port the syslog server is using. |
| 42 |
type: int |
| 43 |
default: 514 |
| 44 |
required: false |
| 45 |
protocol: |
| 46 |
description: |
| 47 |
- This is the transmission protocol the syslog server's using to receive syslog messages. |
| 48 |
type: str |
| 49 |
default: udp |
| 50 |
choices: |
| 51 |
- udp |
| 52 |
- tcp |
| 53 |
- tls |
| 54 |
required: false |
| 55 |
components: |
| 56 |
description: |
| 57 |
- The e-series logging components define the specific logs to transfer to the syslog server. |
| 58 |
- At the time of writing, 'auditLog' is the only logging component but more may become available. |
| 59 |
type: list |
| 60 |
elements: str |
| 61 |
default: ["auditLog"] |
| 62 |
required: false |
| 63 |
test: |
| 64 |
description: |
| 65 |
- This forces a test syslog message to be sent to the stated syslog server. |
| 66 |
- Only attempts transmission when I(state=present). |
| 67 |
type: bool |
| 68 |
default: false |
| 69 |
required: false |
| 70 |
notes: |
| 71 |
- Check mode is supported. |
| 72 |
- This API is currently only supported with the Embedded Web Services API v2.12 (bundled with |
| 73 |
SANtricity OS 11.40.2) and higher. |
| 74 |
""" |
| 75 |
|
| 76 |
EXAMPLES = """ |
| 77 |
- name: Add two syslog server configurations to NetApp E-Series storage array. |
| 78 |
na_santricity_syslog: |
| 79 |
ssid: "1" |
| 80 |
api_url: "https://192.168.1.100:8443/devmgr/v2" |
| 81 |
api_username: "admin" |
| 82 |
api_password: "adminpass" |
| 83 |
validate_certs: true |
| 84 |
state: present |
| 85 |
address: "{{ item }}" |
| 86 |
port: 514 |
| 87 |
protocol: tcp |
| 88 |
component: "auditLog" |
| 89 |
loop: |
| 90 |
- "192.168.1.1" |
| 91 |
- "192.168.1.100" |
| 92 |
""" |
| 93 |
|
| 94 |
RETURN = """ |
| 95 |
msg: |
| 96 |
description: Success message |
| 97 |
returned: on success |
| 98 |
type: str |
| 99 |
sample: The settings have been updated. |
| 100 |
syslog: |
| 101 |
description: |
| 102 |
- True if syslog server configuration has been added to e-series storage array. |
| 103 |
returned: on success |
| 104 |
sample: True |
| 105 |
type: bool |
| 106 |
""" |
| 107 |
from ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity import NetAppESeriesModule |
| 108 |
from ansible.module_utils._text import to_native |
| 109 |
|
| 110 |
|
| 111 |
class NetAppESeriesSyslog(NetAppESeriesModule): |
| 112 |
def __init__(self): |
| 113 |
ansible_options = dict( |
| 114 |
state=dict(choices=["present", "absent"], required=False, default="present"), |
| 115 |
address=dict(type="str", required=False), |
| 116 |
port=dict(type="int", default=514, required=False), |
| 117 |
protocol=dict(choices=["tcp", "tls", "udp"], default="udp", required=False), |
| 118 |
components=dict(type="list", elements="str", required=False, default=["auditLog"]), |
| 119 |
test=dict(type="bool", default=False, required=False)) |
| 120 |
|
| 121 |
required_if = [["state", "present", ["address", "port", "protocol", "components"]]] |
| 122 |
mutually_exclusive = None |
| 123 |
|
| 124 |
super(NetAppESeriesSyslog, self).__init__(ansible_options=ansible_options, |
| 125 |
web_services_version="02.00.0000.0000", |
| 126 |
mutually_exclusive=mutually_exclusive, |
| 127 |
required_if=required_if, |
| 128 |
supports_check_mode=True) |
| 129 |
args = self.module.params |
| 130 |
|
| 131 |
self.syslog = args["state"] in ["present"] |
| 132 |
self.address = args["address"] |
| 133 |
self.port = args["port"] |
| 134 |
self.protocol = args["protocol"] |
| 135 |
self.components = args["components"] |
| 136 |
self.test = args["test"] |
| 137 |
self.ssid = args["ssid"] |
| 138 |
self.url = args["api_url"] |
| 139 |
self.creds = dict(url_password=args["api_password"], |
| 140 |
validate_certs=args["validate_certs"], |
| 141 |
url_username=args["api_username"], ) |
| 142 |
|
| 143 |
self.components.sort() |
| 144 |
self.check_mode = self.module.check_mode |
| 145 |
|
| 146 |
# Check whether request needs to be forwarded on to the controller web services rest api. |
| 147 |
self.url_path_prefix = "" |
| 148 |
if not self.is_embedded() and self.ssid != "0" and self.ssid.lower() != "proxy": |
| 149 |
self.url_path_prefix = "storage-systems/%s/forward/devmgr/v2/" % self.ssid |
| 150 |
|
| 151 |
def get_configuration(self): |
| 152 |
"""Retrieve existing syslog configuration.""" |
| 153 |
try: |
| 154 |
rc, result = self.request(self.url_path_prefix + "storage-systems/%s/syslog" % self.ssid) |
| 155 |
return result |
| 156 |
except Exception as err: |
| 157 |
self.module.fail_json(msg="Failed to retrieve syslog configuration! Array Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 158 |
|
| 159 |
def test_configuration(self, body): |
| 160 |
"""Send test syslog message to the storage array. |
| 161 |
|
| 162 |
Allows fix number of retries to occur before failure is issued to give the storage array time to create |
| 163 |
new syslog server record. |
| 164 |
""" |
| 165 |
try: |
| 166 |
rc, result = self.request(self.url_path_prefix + "storage-systems/%s/syslog/%s/test" % (self.ssid, body["id"]), method='POST') |
| 167 |
except Exception as err: |
| 168 |
self.module.fail_json(msg="We failed to send test message! Array Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 169 |
|
| 170 |
def update_configuration(self): |
| 171 |
"""Post the syslog request to array.""" |
| 172 |
config_match = None |
| 173 |
perfect_match = None |
| 174 |
update = False |
| 175 |
body = dict() |
| 176 |
|
| 177 |
# search existing configuration for syslog server entry match |
| 178 |
configs = self.get_configuration() |
| 179 |
if self.address: |
| 180 |
for config in configs: |
| 181 |
if config["serverAddress"] == self.address: |
| 182 |
config_match = config |
| 183 |
if (config["port"] == self.port and config["protocol"] == self.protocol and |
| 184 |
len(config["components"]) == len(self.components) and |
| 185 |
all(component["type"] in self.components for component in config["components"])): |
| 186 |
perfect_match = config_match |
| 187 |
break |
| 188 |
|
| 189 |
# generate body for the http request |
| 190 |
if self.syslog: |
| 191 |
if not perfect_match: |
| 192 |
update = True |
| 193 |
if config_match: |
| 194 |
body.update(dict(id=config_match["id"])) |
| 195 |
components = [dict(type=component_type) for component_type in self.components] |
| 196 |
body.update(dict(serverAddress=self.address, port=self.port, |
| 197 |
protocol=self.protocol, components=components)) |
| 198 |
self.make_configuration_request(body) |
| 199 |
|
| 200 |
elif config_match: |
| 201 |
|
| 202 |
# remove specific syslog server configuration |
| 203 |
if self.address: |
| 204 |
update = True |
| 205 |
body.update(dict(id=config_match["id"])) |
| 206 |
self.make_configuration_request(body) |
| 207 |
|
| 208 |
# if no address is specified, remove all syslog server configurations |
| 209 |
elif configs: |
| 210 |
update = True |
| 211 |
for config in configs: |
| 212 |
body.update(dict(id=config["id"])) |
| 213 |
self.make_configuration_request(body) |
| 214 |
|
| 215 |
return update |
| 216 |
|
| 217 |
def make_configuration_request(self, body): |
| 218 |
# make http request(s) |
| 219 |
if not self.check_mode: |
| 220 |
try: |
| 221 |
if self.syslog: |
| 222 |
if "id" in body: |
| 223 |
rc, result = self.request(self.url_path_prefix + "storage-systems/%s/syslog/%s" % (self.ssid, body["id"]), |
| 224 |
method='POST', data=body) |
| 225 |
else: |
| 226 |
rc, result = self.request(self.url_path_prefix + "storage-systems/%s/syslog" % self.ssid, method='POST', data=body) |
| 227 |
body.update(result) |
| 228 |
|
| 229 |
# send syslog test message |
| 230 |
if self.test: |
| 231 |
self.test_configuration(body) |
| 232 |
|
| 233 |
elif "id" in body: |
| 234 |
rc, result = self.request(self.url_path_prefix + "storage-systems/%s/syslog/%s" % (self.ssid, body["id"]), method='DELETE') |
| 235 |
|
| 236 |
# This is going to catch cases like a connection failure |
| 237 |
except Exception as err: |
| 238 |
self.module.fail_json(msg="We failed to modify syslog configuration! Array Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 239 |
|
| 240 |
def update(self): |
| 241 |
"""Update configuration and respond to ansible.""" |
| 242 |
update = self.update_configuration() |
| 243 |
self.module.exit_json(msg="The syslog settings have been updated.", changed=update) |
| 244 |
|
| 245 |
|
| 246 |
def main(): |
| 247 |
settings = NetAppESeriesSyslog() |
| 248 |
settings.update() |
| 249 |
|
| 250 |
|
| 251 |
if __name__ == "__main__": |
| 252 |
main() |