#!/usr/bin/python # -*- coding: utf-8 -*- # # Copyright (c) 2017 F5 Networks Inc. # GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function __metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community'} DOCUMENTATION = r''' module: bigip_remote_syslog short_description: Manipulate remote syslog settings on a BIG-IP description: - Manipulate remote syslog settings on a BIG-IP. version_added: 2.5 options: remote_host: description: - Specifies the IP address, or hostname, for the remote system to which the system sends log messages. required: True remote_port: description: - Specifies the port that the system uses to send messages to the remote logging server. When creating a remote syslog, if this parameter is not specified, the default value C(514) is used. local_ip: description: - Specifies the local IP address of the system that is logging. To provide no local IP, specify the value C(none). When creating a remote syslog, if this parameter is not specified, the default value C(none) is used. state: description: - When C(present), guarantees that the remote syslog exists with the provided attributes. - When C(absent), removes the remote syslog from the system. default: present choices: - absent - present notes: - Requires the netaddr Python package on the host. This is as easy as pip install netaddr. extends_documentation_fragment: f5 requirements: - netaddr author: - Tim Rupp (@caphrim007) ''' EXAMPLES = r''' - name: Add a remote syslog server to log to bigip_remote_syslog: remote_host: 10.10.10.10 password: secret server: lb.mydomain.com user: admin validate_certs: no delegate_to: localhost - name: Add a remote syslog server on a non-standard port to log to bigip_remote_syslog: remote_host: 10.10.10.10 remote_port: 1234 password: secret server: lb.mydomain.com user: admin validate_certs: no delegate_to: localhost ''' RETURN = r''' remote_port: description: New remote port of the remote syslog server. returned: changed type: int sample: 514 local_ip: description: The new local IP of the remote syslog server returned: changed type: string sample: 10.10.10.10 ''' import re from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.six import iteritems HAS_DEVEL_IMPORTS = False try: # Sideband repository used for dev from library.module_utils.network.f5.bigip import HAS_F5SDK from library.module_utils.network.f5.bigip import F5Client from library.module_utils.network.f5.common import F5ModuleError from library.module_utils.network.f5.common import AnsibleF5Parameters from library.module_utils.network.f5.common import cleanup_tokens from library.module_utils.network.f5.common import fqdn_name from library.module_utils.network.f5.common import f5_argument_spec try: from library.module_utils.network.f5.common import iControlUnexpectedHTTPError except ImportError: HAS_F5SDK = False HAS_DEVEL_IMPORTS = True except ImportError: # Upstream Ansible from ansible.module_utils.network.f5.bigip import HAS_F5SDK from ansible.module_utils.network.f5.bigip import F5Client from ansible.module_utils.network.f5.common import F5ModuleError from ansible.module_utils.network.f5.common import AnsibleF5Parameters from ansible.module_utils.network.f5.common import cleanup_tokens from ansible.module_utils.network.f5.common import fqdn_name from ansible.module_utils.network.f5.common import f5_argument_spec try: from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError except ImportError: HAS_F5SDK = False try: import netaddr HAS_NETADDR = True except ImportError: HAS_NETADDR = False class Parameters(AnsibleF5Parameters): updatables = [ 'remote_port', 'local_ip', 'remoteServers' ] returnables = [ 'remote_port', 'local_ip' ] api_attributes = [ 'remoteServers' ] def to_return(self): result = {} for returnable in self.returnables: result[returnable] = getattr(self, returnable) result = self._filter_params(result) return result @property def remote_host(self): try: # Check for valid IPv4 or IPv6 entries netaddr.IPAddress(self._values['remote_host']) return self._values['remote_host'] except netaddr.core.AddrFormatError: # else fallback to checking reasonably well formatted hostnames if self.is_valid_hostname(self._values['remote_host']): return str(self._values['remote_host']) raise F5ModuleError( "The provided 'remote_host' is not a valid IP or hostname" ) def is_valid_hostname(self, host): """Reasonable attempt at validating a hostname Compiled from various paragraphs outlined here https://tools.ietf.org/html/rfc3696#section-2 https://tools.ietf.org/html/rfc1123 Notably, * Host software MUST handle host names of up to 63 characters and SHOULD handle host names of up to 255 characters. * The "LDH rule", after the characters that it permits. (letters, digits, hyphen) * If the hyphen is used, it is not permitted to appear at either the beginning or end of a label :param host: :return: """ if len(host) > 255: return False host = host.rstrip(".") allowed = re.compile(r'(?!-)[A-Z0-9-]{1,63}(?