2016-10-19 17:32:50 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2017-11-16 03:35:09 +00:00
|
|
|
# Copyright (c) 2016 F5 Networks Inc.
|
2017-10-04 19:16:17 +00:00
|
|
|
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
DOCUMENTATION = r'''
|
2016-10-19 17:32:50 +00:00
|
|
|
---
|
|
|
|
module: bigip_snat_pool
|
2017-11-16 03:35:09 +00:00
|
|
|
short_description: Manage SNAT pools on a BIG-IP
|
2016-10-19 17:32:50 +00:00
|
|
|
description:
|
|
|
|
- Manage SNAT pools on a BIG-IP.
|
2018-04-24 05:06:33 +00:00
|
|
|
version_added: 2.3
|
2016-10-19 17:32:50 +00:00
|
|
|
options:
|
|
|
|
members:
|
|
|
|
description:
|
|
|
|
- List of members to put in the SNAT pool. When a C(state) of present is
|
|
|
|
provided, this parameter is required. Otherwise, it is optional.
|
2017-11-16 03:35:09 +00:00
|
|
|
aliases:
|
|
|
|
- member
|
2016-10-19 17:32:50 +00:00
|
|
|
name:
|
|
|
|
description: The name of the SNAT pool.
|
|
|
|
required: True
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Whether the SNAT pool should exist or not.
|
|
|
|
default: present
|
|
|
|
choices:
|
|
|
|
- present
|
|
|
|
- absent
|
2017-11-16 03:35:09 +00:00
|
|
|
partition:
|
|
|
|
description:
|
|
|
|
- Device partition to manage resources on.
|
2018-01-20 18:40:20 +00:00
|
|
|
default: Common
|
2017-11-16 03:35:09 +00:00
|
|
|
version_added: 2.5
|
2016-10-19 17:32:50 +00:00
|
|
|
notes:
|
|
|
|
- Requires the netaddr Python package on the host. This is as easy as
|
|
|
|
pip install netaddr
|
|
|
|
extends_documentation_fragment: f5
|
|
|
|
author:
|
|
|
|
- Tim Rupp (@caphrim007)
|
|
|
|
'''
|
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
EXAMPLES = r'''
|
2016-10-19 17:32:50 +00:00
|
|
|
- name: Add the SNAT pool 'my-snat-pool'
|
|
|
|
bigip_snat_pool:
|
2017-11-16 03:35:09 +00:00
|
|
|
server: lb.mydomain.com
|
|
|
|
user: admin
|
|
|
|
password: secret
|
|
|
|
name: my-snat-pool
|
|
|
|
state: present
|
|
|
|
members:
|
|
|
|
- 10.10.10.10
|
|
|
|
- 20.20.20.20
|
2016-10-19 17:32:50 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
- name: Change the SNAT pool's members to a single member
|
|
|
|
bigip_snat_pool:
|
2017-11-16 03:35:09 +00:00
|
|
|
server: lb.mydomain.com
|
|
|
|
user: admin
|
|
|
|
password: secret
|
|
|
|
name: my-snat-pool
|
|
|
|
state: present
|
|
|
|
member: 30.30.30.30
|
2016-10-19 17:32:50 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
- name: Remove the SNAT pool 'my-snat-pool'
|
|
|
|
bigip_snat_pool:
|
2017-11-16 03:35:09 +00:00
|
|
|
server: lb.mydomain.com
|
|
|
|
user: admin
|
|
|
|
password: secret
|
|
|
|
name: johnd
|
|
|
|
state: absent
|
2016-10-19 17:32:50 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
'''
|
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
RETURN = r'''
|
2016-10-19 17:32:50 +00:00
|
|
|
members:
|
2017-11-16 03:35:09 +00:00
|
|
|
description:
|
|
|
|
- List of members that are part of the SNAT pool.
|
|
|
|
returned: changed and success
|
|
|
|
type: list
|
|
|
|
sample: "['10.10.10.10']"
|
2016-10-19 17:32:50 +00:00
|
|
|
'''
|
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
import os
|
|
|
|
|
2018-01-13 05:49:12 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.basic import env_fallback
|
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
try:
|
2018-01-13 05:49:12 +00:00
|
|
|
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 f5_argument_spec
|
|
|
|
try:
|
|
|
|
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
|
|
|
|
except ImportError:
|
|
|
|
HAS_F5SDK = False
|
2016-10-19 17:32:50 +00:00
|
|
|
except ImportError:
|
2018-01-13 05:49:12 +00:00
|
|
|
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 f5_argument_spec
|
|
|
|
try:
|
|
|
|
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
|
|
|
|
except ImportError:
|
|
|
|
HAS_F5SDK = False
|
2016-10-19 17:32:50 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from netaddr import IPAddress, AddrFormatError
|
|
|
|
HAS_NETADDR = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_NETADDR = False
|
|
|
|
|
2017-07-14 23:42:00 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
class Parameters(AnsibleF5Parameters):
|
|
|
|
api_map = {}
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
updatables = [
|
|
|
|
'members'
|
|
|
|
]
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
returnables = [
|
|
|
|
'members'
|
|
|
|
]
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
api_attributes = [
|
|
|
|
'members'
|
|
|
|
]
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def to_return(self):
|
|
|
|
result = {}
|
|
|
|
for returnable in self.returnables:
|
|
|
|
result[returnable] = getattr(self, returnable)
|
|
|
|
result = self._filter_params(result)
|
2016-10-19 17:32:50 +00:00
|
|
|
return result
|
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def api_params(self):
|
|
|
|
result = {}
|
|
|
|
for api_attribute in self.api_attributes:
|
|
|
|
if self.api_map is not None and api_attribute in self.api_map:
|
|
|
|
result[api_attribute] = getattr(self, self.api_map[api_attribute])
|
|
|
|
else:
|
|
|
|
result[api_attribute] = getattr(self, api_attribute)
|
|
|
|
result = self._filter_params(result)
|
|
|
|
return result
|
|
|
|
|
|
|
|
@property
|
|
|
|
def members(self):
|
|
|
|
if self._values['members'] is None:
|
|
|
|
return None
|
|
|
|
result = set()
|
|
|
|
for member in self._values['members']:
|
|
|
|
member = self._clear_member_prefix(member)
|
|
|
|
address = self._format_member_address(member)
|
|
|
|
result.update([address])
|
|
|
|
return list(result)
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def _clear_member_prefix(self, member):
|
|
|
|
result = os.path.basename(member)
|
|
|
|
return result
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def _format_member_address(self, member):
|
|
|
|
try:
|
|
|
|
address = str(IPAddress(member))
|
|
|
|
address = '/{0}/{1}'.format(self.partition, address)
|
|
|
|
return address
|
|
|
|
except (AddrFormatError, ValueError):
|
2016-10-19 17:32:50 +00:00
|
|
|
raise F5ModuleError(
|
2017-11-16 03:35:09 +00:00
|
|
|
'The provided member address is not a valid IP address'
|
2016-10-19 17:32:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-01-13 05:49:12 +00:00
|
|
|
class Changes(Parameters):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
class Difference(object):
|
|
|
|
def __init__(self, want, have=None):
|
|
|
|
self.want = want
|
|
|
|
self.have = have
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def compare(self, param):
|
|
|
|
try:
|
|
|
|
result = getattr(self, param)
|
|
|
|
return result
|
|
|
|
except AttributeError:
|
|
|
|
return self.__default(param)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def members(self):
|
|
|
|
if self.want.members is None:
|
|
|
|
return None
|
|
|
|
if set(self.want.members) == set(self.have.members):
|
|
|
|
return None
|
|
|
|
result = list(set(self.want.members))
|
|
|
|
return result
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def __default(self, param):
|
|
|
|
attr1 = getattr(self.want, param)
|
|
|
|
try:
|
|
|
|
attr2 = getattr(self.have, param)
|
|
|
|
if attr1 != attr2:
|
|
|
|
return attr1
|
|
|
|
except AttributeError:
|
|
|
|
return attr1
|
|
|
|
|
|
|
|
|
|
|
|
class ModuleManager(object):
|
2018-01-13 05:49:12 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.module = kwargs.get('module', None)
|
|
|
|
self.client = kwargs.get('client', None)
|
2017-11-16 03:35:09 +00:00
|
|
|
self.have = None
|
2018-01-13 05:49:12 +00:00
|
|
|
self.want = Parameters(params=self.module.params)
|
|
|
|
self.changes = Changes()
|
2017-11-16 03:35:09 +00:00
|
|
|
|
|
|
|
def _set_changed_options(self):
|
|
|
|
changed = {}
|
|
|
|
for key in Parameters.returnables:
|
|
|
|
if getattr(self.want, key) is not None:
|
|
|
|
changed[key] = getattr(self.want, key)
|
|
|
|
if changed:
|
2018-01-13 05:49:12 +00:00
|
|
|
self.changes = Changes(params=changed)
|
2017-11-16 03:35:09 +00:00
|
|
|
|
|
|
|
def _update_changed_options(self):
|
|
|
|
diff = Difference(self.want, self.have)
|
|
|
|
updatables = Parameters.updatables
|
|
|
|
changed = dict()
|
|
|
|
for k in updatables:
|
|
|
|
change = diff.compare(k)
|
|
|
|
if change is None:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
changed[k] = change
|
|
|
|
if changed:
|
2018-01-13 05:49:12 +00:00
|
|
|
self.changes = Changes(params=changed)
|
2017-11-16 03:35:09 +00:00
|
|
|
return True
|
|
|
|
return False
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def exec_module(self):
|
|
|
|
changed = False
|
|
|
|
result = dict()
|
|
|
|
state = self.want.state
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
try:
|
|
|
|
if state == "present":
|
|
|
|
changed = self.present()
|
|
|
|
elif state == "absent":
|
|
|
|
changed = self.absent()
|
|
|
|
except iControlUnexpectedHTTPError as e:
|
|
|
|
raise F5ModuleError(str(e))
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
changes = self.changes.to_return()
|
|
|
|
result.update(**changes)
|
|
|
|
result.update(dict(changed=changed))
|
|
|
|
self._announce_deprecations()
|
2016-10-19 17:32:50 +00:00
|
|
|
return result
|
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def _announce_deprecations(self):
|
|
|
|
warnings = []
|
|
|
|
if self.want:
|
|
|
|
warnings += self.want._values.get('__warnings', [])
|
|
|
|
if self.have:
|
|
|
|
warnings += self.have._values.get('__warnings', [])
|
|
|
|
for warning in warnings:
|
2018-01-13 05:49:12 +00:00
|
|
|
self.module.deprecate(
|
2017-11-16 03:35:09 +00:00
|
|
|
msg=warning['msg'],
|
|
|
|
version=warning['version']
|
|
|
|
)
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def present(self):
|
|
|
|
if self.exists():
|
|
|
|
return self.update()
|
|
|
|
else:
|
|
|
|
return self.create()
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def absent(self):
|
|
|
|
changed = False
|
|
|
|
if self.exists():
|
|
|
|
changed = self.remove()
|
|
|
|
return changed
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def read_current_from_device(self):
|
|
|
|
resource = self.client.api.tm.ltm.snatpools.snatpool.load(
|
|
|
|
name=self.want.name,
|
|
|
|
partition=self.want.partition
|
2016-10-19 17:32:50 +00:00
|
|
|
)
|
2017-11-16 03:35:09 +00:00
|
|
|
result = resource.attrs
|
2018-01-13 05:49:12 +00:00
|
|
|
return Parameters(params=result)
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def exists(self):
|
|
|
|
result = self.client.api.tm.ltm.snatpools.snatpool.exists(
|
|
|
|
name=self.want.name,
|
|
|
|
partition=self.want.partition
|
|
|
|
)
|
2016-10-19 17:32:50 +00:00
|
|
|
return result
|
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def should_update(self):
|
|
|
|
result = self._update_changed_options()
|
|
|
|
if result:
|
2016-10-19 17:32:50 +00:00
|
|
|
return True
|
2017-11-16 03:35:09 +00:00
|
|
|
return False
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def update(self):
|
|
|
|
self.have = self.read_current_from_device()
|
|
|
|
if not self.should_update():
|
2016-10-19 17:32:50 +00:00
|
|
|
return False
|
2018-01-13 05:49:12 +00:00
|
|
|
if self.module.check_mode:
|
2016-10-19 17:32:50 +00:00
|
|
|
return True
|
2017-11-16 03:35:09 +00:00
|
|
|
self.update_on_device()
|
|
|
|
return True
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def update_on_device(self):
|
|
|
|
params = self.changes.api_params()
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
resource = self.client.api.tm.ltm.snatpools.snatpool.load(
|
|
|
|
name=self.want.name,
|
|
|
|
partition=self.want.partition
|
|
|
|
)
|
|
|
|
resource.modify(**params)
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def create(self):
|
|
|
|
self._set_changed_options()
|
2018-01-13 05:49:12 +00:00
|
|
|
if self.module.check_mode:
|
2016-10-19 17:32:50 +00:00
|
|
|
return True
|
2017-11-16 03:35:09 +00:00
|
|
|
self.create_on_device()
|
|
|
|
if not self.exists():
|
2016-10-19 17:32:50 +00:00
|
|
|
raise F5ModuleError("Failed to create the SNAT pool")
|
2017-11-16 03:35:09 +00:00
|
|
|
return True
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def create_on_device(self):
|
|
|
|
params = self.want.api_params()
|
|
|
|
self.client.api.tm.ltm.snatpools.snatpool.create(
|
|
|
|
name=self.want.name,
|
|
|
|
partition=self.want.partition,
|
|
|
|
**params
|
2016-10-19 17:32:50 +00:00
|
|
|
)
|
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def remove(self):
|
2018-01-13 05:49:12 +00:00
|
|
|
if self.module.check_mode:
|
2016-10-19 17:32:50 +00:00
|
|
|
return True
|
2017-11-16 03:35:09 +00:00
|
|
|
self.remove_from_device()
|
|
|
|
if self.exists():
|
2016-10-19 17:32:50 +00:00
|
|
|
raise F5ModuleError("Failed to delete the SNAT pool")
|
|
|
|
return True
|
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
def remove_from_device(self):
|
|
|
|
resource = self.client.api.tm.ltm.snatpools.snatpool.load(
|
|
|
|
name=self.want.name,
|
|
|
|
partition=self.want.partition
|
|
|
|
)
|
|
|
|
resource.delete()
|
2016-10-19 17:32:50 +00:00
|
|
|
|
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
class ArgumentSpec(object):
|
2016-10-19 17:32:50 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.supports_check_mode = True
|
2018-01-13 05:49:12 +00:00
|
|
|
argument_spec = dict(
|
2016-10-19 17:32:50 +00:00
|
|
|
name=dict(required=True),
|
|
|
|
members=dict(
|
|
|
|
type='list',
|
|
|
|
aliases=['member']
|
|
|
|
),
|
|
|
|
state=dict(
|
|
|
|
default='present',
|
2017-11-16 03:35:09 +00:00
|
|
|
choices=['absent', 'present']
|
2018-01-13 05:49:12 +00:00
|
|
|
),
|
|
|
|
partition=dict(
|
|
|
|
default='Common',
|
|
|
|
fallback=(env_fallback, ['F5_PARTITION'])
|
2016-10-19 17:32:50 +00:00
|
|
|
)
|
|
|
|
)
|
2018-01-13 05:49:12 +00:00
|
|
|
self.argument_spec = {}
|
|
|
|
self.argument_spec.update(f5_argument_spec)
|
|
|
|
self.argument_spec.update(argument_spec)
|
2017-11-16 03:35:09 +00:00
|
|
|
self.required_if = [
|
|
|
|
['state', 'present', ['members']]
|
|
|
|
]
|
2016-10-19 17:32:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2017-11-16 03:35:09 +00:00
|
|
|
spec = ArgumentSpec()
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2018-01-13 05:49:12 +00:00
|
|
|
module = AnsibleModule(
|
2017-11-16 03:35:09 +00:00
|
|
|
argument_spec=spec.argument_spec,
|
|
|
|
supports_check_mode=spec.supports_check_mode,
|
|
|
|
required_if=spec.required_if
|
|
|
|
)
|
2018-01-13 05:49:12 +00:00
|
|
|
if not HAS_F5SDK:
|
|
|
|
module.fail_json(msg="The python f5-sdk module is required")
|
|
|
|
if not HAS_NETADDR:
|
|
|
|
module.fail_json(msg="The python netaddr module is required")
|
2016-10-19 17:32:50 +00:00
|
|
|
|
2017-11-16 03:35:09 +00:00
|
|
|
try:
|
2018-01-13 05:49:12 +00:00
|
|
|
client = F5Client(**module.params)
|
|
|
|
mm = ModuleManager(module=module, client=client)
|
2017-11-16 03:35:09 +00:00
|
|
|
results = mm.exec_module()
|
2018-01-13 05:49:12 +00:00
|
|
|
cleanup_tokens(client)
|
|
|
|
module.exit_json(**results)
|
|
|
|
except F5ModuleError as ex:
|
|
|
|
cleanup_tokens(client)
|
|
|
|
module.fail_json(msg=str(ex))
|
|
|
|
|
2016-10-19 17:32:50 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|