| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: bigip_remote_syslog |
| 13 |
short_description: Manipulate remote syslog settings on a BIG-IP |
| 14 |
description: |
| 15 |
- Manipulate remote syslog settings on a BIG-IP system. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
remote_host: |
| 19 |
description: |
| 20 |
- Specifies the IP address or hostname for the remote system, to |
| 21 |
which the system sends log messages. |
| 22 |
type: str |
| 23 |
required: True |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Specifies the name of the syslog object. |
| 27 |
- This option is required when multiple C(remote_host)s with the same IP |
| 28 |
or hostname are present on the device. |
| 29 |
- If C(name) is not provided, C(remote_host) is used by default. |
| 30 |
type: str |
| 31 |
remote_port: |
| 32 |
description: |
| 33 |
- Specifies the port the system uses to send messages to the |
| 34 |
remote logging server. |
| 35 |
- When creating a remote syslog, if this parameter is not specified, the |
| 36 |
default value is C(514). |
| 37 |
type: str |
| 38 |
local_ip: |
| 39 |
description: |
| 40 |
- Specifies the local IP address of the system that is logging. To |
| 41 |
provide no local IP, specify the value C(none). |
| 42 |
- When creating a remote syslog, if this parameter is not specified, the |
| 43 |
default value is C(none). |
| 44 |
type: str |
| 45 |
state: |
| 46 |
description: |
| 47 |
- When C(present), guarantees the remote syslog exists with the provided |
| 48 |
attributes. |
| 49 |
- When C(absent), removes the remote syslog from the system. |
| 50 |
type: str |
| 51 |
choices: |
| 52 |
- absent |
| 53 |
- present |
| 54 |
default: present |
| 55 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 56 |
author: |
| 57 |
- Tim Rupp (@caphrim007) |
| 58 |
- Wojciech Wypior (@wojtek0806) |
| 59 |
''' |
| 60 |
|
| 61 |
EXAMPLES = r''' |
| 62 |
- name: Add a remote syslog server to log to |
| 63 |
bigip_remote_syslog: |
| 64 |
remote_host: 10.10.10.10 |
| 65 |
provider: |
| 66 |
password: secret |
| 67 |
server: lb.mydomain.com |
| 68 |
user: admin |
| 69 |
delegate_to: localhost |
| 70 |
|
| 71 |
- name: Add a remote syslog server on a non-standard port to log to |
| 72 |
bigip_remote_syslog: |
| 73 |
remote_host: 10.10.10.10 |
| 74 |
remote_port: 1234 |
| 75 |
provider: |
| 76 |
password: secret |
| 77 |
server: lb.mydomain.com |
| 78 |
user: admin |
| 79 |
delegate_to: localhost |
| 80 |
''' |
| 81 |
|
| 82 |
RETURN = r''' |
| 83 |
remote_port: |
| 84 |
description: New remote port of the remote syslog server. |
| 85 |
returned: changed |
| 86 |
type: int |
| 87 |
sample: 514 |
| 88 |
local_ip: |
| 89 |
description: The new local IP of the remote syslog server. |
| 90 |
returned: changed |
| 91 |
type: str |
| 92 |
sample: 10.10.10.10 |
| 93 |
''' |
| 94 |
from datetime import datetime |
| 95 |
|
| 96 |
from ansible.module_utils.basic import AnsibleModule |
| 97 |
from ansible.module_utils.six import iteritems |
| 98 |
|
| 99 |
from ..module_utils.bigip import F5RestClient |
| 100 |
from ..module_utils.common import ( |
| 101 |
F5ModuleError, AnsibleF5Parameters, f5_argument_spec, fq_name, is_valid_hostname |
| 102 |
) |
| 103 |
from ..module_utils.ipaddress import is_valid_ip |
| 104 |
from ..module_utils.icontrol import tmos_version |
| 105 |
from ..module_utils.teem import send_teem |
| 106 |
|
| 107 |
|
| 108 |
class Parameters(AnsibleF5Parameters): |
| 109 |
api_map = { |
| 110 |
'remotePort': 'remote_port', |
| 111 |
'localIp': 'local_ip', |
| 112 |
'host': 'remote_host', |
| 113 |
} |
| 114 |
|
| 115 |
updatables = [ |
| 116 |
'remote_port', |
| 117 |
'local_ip', |
| 118 |
'remote_host', |
| 119 |
'name', |
| 120 |
] |
| 121 |
|
| 122 |
returnables = [ |
| 123 |
'remote_port', |
| 124 |
'local_ip', |
| 125 |
'remote_host', |
| 126 |
'name', |
| 127 |
'remoteServers', |
| 128 |
] |
| 129 |
|
| 130 |
api_attributes = [ |
| 131 |
'remotePort', |
| 132 |
'localIp', |
| 133 |
'host', |
| 134 |
'name', |
| 135 |
'remoteServers', |
| 136 |
] |
| 137 |
|
| 138 |
|
| 139 |
class ApiParameters(Parameters): |
| 140 |
pass |
| 141 |
|
| 142 |
|
| 143 |
class ModuleParameters(Parameters): |
| 144 |
@property |
| 145 |
def remote_host(self): |
| 146 |
if is_valid_ip(self._values['remote_host']): |
| 147 |
return self._values['remote_host'] |
| 148 |
elif is_valid_hostname(self._values['remote_host']): |
| 149 |
return str(self._values['remote_host']) |
| 150 |
raise F5ModuleError( |
| 151 |
"The provided 'remote_host' is not a valid IP or hostname" |
| 152 |
) |
| 153 |
|
| 154 |
@property |
| 155 |
def remote_port(self): |
| 156 |
if self._values['remote_port'] in [None, 'none']: |
| 157 |
return None |
| 158 |
if self._values['remote_port'] == 0: |
| 159 |
raise F5ModuleError( |
| 160 |
"The 'remote_port' value must between 1 and 65535" |
| 161 |
) |
| 162 |
return int(self._values['remote_port']) |
| 163 |
|
| 164 |
@property |
| 165 |
def local_ip(self): |
| 166 |
if self._values['local_ip'] in [None, 'none']: |
| 167 |
return None |
| 168 |
if is_valid_ip(self._values['local_ip']): |
| 169 |
return self._values['local_ip'] |
| 170 |
else: |
| 171 |
raise F5ModuleError( |
| 172 |
"The provided 'local_ip' is not a valid IP address" |
| 173 |
) |
| 174 |
|
| 175 |
@property |
| 176 |
def name(self): |
| 177 |
if self._values['remote_host'] is None: |
| 178 |
return None |
| 179 |
if self._values['name'] is None: |
| 180 |
return None |
| 181 |
name = fq_name(self.partition, self._values['name']) |
| 182 |
return name |
| 183 |
|
| 184 |
|
| 185 |
class Changes(Parameters): |
| 186 |
def to_return(self): |
| 187 |
result = {} |
| 188 |
try: |
| 189 |
for returnable in self.returnables: |
| 190 |
change = getattr(self, returnable) |
| 191 |
if isinstance(change, dict): |
| 192 |
result.update(change) |
| 193 |
else: |
| 194 |
result[returnable] = change |
| 195 |
result = self._filter_params(result) |
| 196 |
except Exception: |
| 197 |
pass |
| 198 |
return result |
| 199 |
|
| 200 |
|
| 201 |
class UsableChanges(Changes): |
| 202 |
pass |
| 203 |
|
| 204 |
|
| 205 |
class ReportableChanges(Changes): |
| 206 |
@property |
| 207 |
def remote_port(self): |
| 208 |
if self._values['remote_port'] is None: |
| 209 |
return None |
| 210 |
return int(self._values['remote_port']) |
| 211 |
|
| 212 |
@property |
| 213 |
def remoteServers(self): |
| 214 |
pass |
| 215 |
|
| 216 |
|
| 217 |
class Difference(object): |
| 218 |
def __init__(self, want, have=None): |
| 219 |
self.want = want |
| 220 |
self.have = have |
| 221 |
self._local_ip = None |
| 222 |
self._remote_port = None |
| 223 |
|
| 224 |
def compare(self, param): |
| 225 |
try: |
| 226 |
result = getattr(self, param) |
| 227 |
return result |
| 228 |
except AttributeError: |
| 229 |
return self.__default(param) |
| 230 |
|
| 231 |
def __default(self, param): |
| 232 |
attr1 = getattr(self.want, param) |
| 233 |
try: |
| 234 |
attr2 = getattr(self.have, param) |
| 235 |
if attr1 != attr2: |
| 236 |
return attr1 |
| 237 |
except AttributeError: |
| 238 |
return attr1 |
| 239 |
|
| 240 |
|
| 241 |
class ModuleManager(object): |
| 242 |
def __init__(self, *args, **kwargs): |
| 243 |
self.module = kwargs.pop('module', None) |
| 244 |
self.client = F5RestClient(**self.module.params) |
| 245 |
self.want = ModuleParameters(params=self.module.params) |
| 246 |
self.have = ApiParameters() |
| 247 |
self.changes = UsableChanges() |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
self.syslogs = dict() |
| 254 |
|
| 255 |
def _announce_deprecations(self, result): |
| 256 |
warnings = result.pop('__warnings', []) |
| 257 |
for warning in warnings: |
| 258 |
self.module.deprecate( |
| 259 |
msg=warning['msg'], |
| 260 |
version=warning['version'] |
| 261 |
) |
| 262 |
|
| 263 |
def _update_changed_options(self): |
| 264 |
diff = Difference(self.want, self.have) |
| 265 |
updatables = Parameters.updatables |
| 266 |
changed = dict() |
| 267 |
for k in updatables: |
| 268 |
change = diff.compare(k) |
| 269 |
if change is None: |
| 270 |
continue |
| 271 |
else: |
| 272 |
if isinstance(change, dict): |
| 273 |
changed.update(change) |
| 274 |
else: |
| 275 |
changed[k] = change |
| 276 |
if changed: |
| 277 |
self.changes = UsableChanges(params=changed) |
| 278 |
return True |
| 279 |
return False |
| 280 |
|
| 281 |
def format_syslogs(self, syslogs): |
| 282 |
result = None |
| 283 |
for x in syslogs: |
| 284 |
syslog = ApiParameters(params=x) |
| 285 |
self.syslogs[syslog.name] = x |
| 286 |
|
| 287 |
if syslog.name == self.want.name: |
| 288 |
result = syslog |
| 289 |
elif syslog.remote_host == self.want.remote_host: |
| 290 |
result = syslog |
| 291 |
|
| 292 |
if not result: |
| 293 |
return ApiParameters() |
| 294 |
return result |
| 295 |
|
| 296 |
def exec_module(self): |
| 297 |
start = datetime.now().isoformat() |
| 298 |
version = tmos_version(self.client) |
| 299 |
result = dict() |
| 300 |
|
| 301 |
changed = self.present() |
| 302 |
|
| 303 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 304 |
changes = reportable.to_return() |
| 305 |
result.update(**changes) |
| 306 |
result.update(dict(changed=changed)) |
| 307 |
self._announce_deprecations(result) |
| 308 |
send_teem(start, self.client, self.module, version) |
| 309 |
return result |
| 310 |
|
| 311 |
def present(self): |
| 312 |
return self.update() |
| 313 |
|
| 314 |
def should_update(self): |
| 315 |
result = self._update_changed_options() |
| 316 |
if result: |
| 317 |
return True |
| 318 |
return False |
| 319 |
|
| 320 |
def update(self): |
| 321 |
self.have = self.format_syslogs(self.read_current_from_device()) |
| 322 |
if not self.should_update() and self.want.state != 'absent': |
| 323 |
return False |
| 324 |
if self.module.check_mode: |
| 325 |
return True |
| 326 |
|
| 327 |
if self.want.name is None: |
| 328 |
self.want.update({'name': self.want.remote_host}) |
| 329 |
|
| 330 |
syslogs = [v for k, v in iteritems(self.syslogs)] |
| 331 |
dupes = [x for x in syslogs if x['host'] == self.want.remote_host] |
| 332 |
if len(dupes) > 1: |
| 333 |
raise F5ModuleError( |
| 334 |
"Multiple occurrences of hostname: {0} detected, please specify 'name' parameter". format( |
| 335 |
self.want.remote_host |
| 336 |
) |
| 337 |
) |
| 338 |
|
| 339 |
|
| 340 |
if self.want.state == 'absent': |
| 341 |
if self.want.name not in self.syslogs: |
| 342 |
return False |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
changes = dict(self.changes.api_params()) |
| 350 |
if self.want.name in self.syslogs: |
| 351 |
self.syslogs[self.want.name].update(changes) |
| 352 |
else: |
| 353 |
|
| 354 |
self.syslogs[self.want.name] = changes |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
self.syslogs[self.want.name].update({'name': self.want.name}) |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
if self.want.state == 'absent': |
| 364 |
del self.syslogs[self.want.name] |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
result = [v for k, v in iteritems(self.syslogs)] |
| 374 |
|
| 375 |
self.changes = Changes(params=dict(remoteServers=result)) |
| 376 |
self.changes.update(self.want._values) |
| 377 |
self.update_on_device() |
| 378 |
return True |
| 379 |
|
| 380 |
def update_on_device(self): |
| 381 |
params = self.changes.api_params() |
| 382 |
params = dict( |
| 383 |
remoteServers=params.get('remoteServers') |
| 384 |
) |
| 385 |
uri = "https://{0}:{1}/mgmt/tm/sys/syslog/".format( |
| 386 |
self.client.provider['server'], |
| 387 |
self.client.provider['server_port'], |
| 388 |
) |
| 389 |
resp = self.client.api.patch(uri, json=params) |
| 390 |
try: |
| 391 |
response = resp.json() |
| 392 |
except ValueError as ex: |
| 393 |
raise F5ModuleError(str(ex)) |
| 394 |
|
| 395 |
if 'code' in response and response['code'] in [400, 409]: |
| 396 |
if 'message' in response: |
| 397 |
raise F5ModuleError(response['message']) |
| 398 |
else: |
| 399 |
raise F5ModuleError(resp.content) |
| 400 |
|
| 401 |
def read_current_from_device(self): |
| 402 |
uri = "https://{0}:{1}/mgmt/tm/sys/syslog/".format( |
| 403 |
self.client.provider['server'], |
| 404 |
self.client.provider['server_port'], |
| 405 |
) |
| 406 |
resp = self.client.api.get(uri) |
| 407 |
try: |
| 408 |
response = resp.json() |
| 409 |
except ValueError as ex: |
| 410 |
raise F5ModuleError(str(ex)) |
| 411 |
|
| 412 |
if 'code' in response and response['code'] == 400: |
| 413 |
if 'message' in response: |
| 414 |
raise F5ModuleError(response['message']) |
| 415 |
else: |
| 416 |
raise F5ModuleError(resp.content) |
| 417 |
result = response.get('remoteServers', []) |
| 418 |
return result |
| 419 |
|
| 420 |
|
| 421 |
class ArgumentSpec(object): |
| 422 |
def __init__(self): |
| 423 |
self.supports_check_mode = True |
| 424 |
argument_spec = dict( |
| 425 |
remote_host=dict( |
| 426 |
required=True |
| 427 |
), |
| 428 |
remote_port=dict(), |
| 429 |
local_ip=dict(), |
| 430 |
name=dict(), |
| 431 |
state=dict( |
| 432 |
default='present', |
| 433 |
choices=['absent', 'present'] |
| 434 |
) |
| 435 |
) |
| 436 |
self.argument_spec = {} |
| 437 |
self.argument_spec.update(f5_argument_spec) |
| 438 |
self.argument_spec.update(argument_spec) |
| 439 |
|
| 440 |
|
| 441 |
def main(): |
| 442 |
spec = ArgumentSpec() |
| 443 |
|
| 444 |
module = AnsibleModule( |
| 445 |
argument_spec=spec.argument_spec, |
| 446 |
supports_check_mode=spec.supports_check_mode |
| 447 |
) |
| 448 |
|
| 449 |
try: |
| 450 |
mm = ModuleManager(module=module) |
| 451 |
results = mm.exec_module() |
| 452 |
module.exit_json(**results) |
| 453 |
except F5ModuleError as ex: |
| 454 |
module.fail_json(msg=str(ex)) |
| 455 |
|
| 456 |
|
| 457 |
if __name__ == '__main__': |
| 458 |
main() |