2014-09-26 01:01:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2016-07-08 05:56:09 +00:00
|
|
|
#
|
2018-11-10 00:48:08 +00:00
|
|
|
# Copyright: (c) 2017, F5 Networks Inc.
|
|
|
|
# Copyright: (c) 2013, Matt Hite <mhite@hotmail.com>
|
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)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-01-17 05:02:33 +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'],
|
2018-10-12 15:57:28 +00:00
|
|
|
'supported_by': 'certified'}
|
2017-03-14 16:07:22 +00:00
|
|
|
|
2018-01-17 05:02:33 +00:00
|
|
|
DOCUMENTATION = r'''
|
2014-09-26 01:01:01 +00:00
|
|
|
---
|
|
|
|
module: bigip_pool_member
|
2016-07-08 05:56:09 +00:00
|
|
|
short_description: Manages F5 BIG-IP LTM pool members
|
2014-09-26 01:01:01 +00:00
|
|
|
description:
|
2018-01-17 05:02:33 +00:00
|
|
|
- Manages F5 BIG-IP LTM pool members via iControl SOAP API.
|
2016-07-08 05:56:09 +00:00
|
|
|
version_added: 1.4
|
2014-09-26 01:01:01 +00:00
|
|
|
options:
|
2018-04-25 14:16:11 +00:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of the node to create, or re-use, when creating a new pool member.
|
|
|
|
- This parameter is optional and, if not specified, a node name will be
|
|
|
|
created automatically from either the specified C(address) or C(fqdn).
|
2018-08-21 22:01:52 +00:00
|
|
|
- The C(enabled) state is an alias of C(present).
|
2018-04-25 14:16:11 +00:00
|
|
|
version_added: 2.6
|
2016-07-08 05:56:09 +00:00
|
|
|
state:
|
|
|
|
description:
|
2018-01-17 05:02:33 +00:00
|
|
|
- Pool member state.
|
|
|
|
required: True
|
2016-07-08 05:56:09 +00:00
|
|
|
default: present
|
|
|
|
choices:
|
|
|
|
- present
|
|
|
|
- absent
|
|
|
|
- enabled
|
|
|
|
- disabled
|
2018-04-25 14:16:11 +00:00
|
|
|
- forced_offline
|
2016-07-08 05:56:09 +00:00
|
|
|
pool:
|
|
|
|
description:
|
|
|
|
- Pool name. This pool must exist.
|
2018-01-17 05:02:33 +00:00
|
|
|
required: True
|
2016-07-08 05:56:09 +00:00
|
|
|
partition:
|
|
|
|
description:
|
|
|
|
- Partition
|
2018-01-17 05:02:33 +00:00
|
|
|
default: Common
|
2018-04-25 14:16:11 +00:00
|
|
|
address:
|
2016-07-08 05:56:09 +00:00
|
|
|
description:
|
2018-04-25 14:16:11 +00:00
|
|
|
- IP address of the pool member. This can be either IPv4 or IPv6. When creating a
|
|
|
|
new pool member, one of either C(address) or C(fqdn) must be provided. This
|
|
|
|
parameter cannot be updated after it is set.
|
|
|
|
aliases:
|
|
|
|
- ip
|
|
|
|
- host
|
|
|
|
version_added: 2.2
|
|
|
|
fqdn:
|
|
|
|
description:
|
|
|
|
- FQDN name of the pool member. This can be any name that is a valid RFC 1123 DNS
|
|
|
|
name. Therefore, the only characters that can be used are "A" to "Z",
|
|
|
|
"a" to "z", "0" to "9", the hyphen ("-") and the period (".").
|
|
|
|
- FQDN names must include at lease one period; delineating the host from
|
|
|
|
the domain. ex. C(host.domain).
|
|
|
|
- FQDN names must end with a letter or a number.
|
|
|
|
- When creating a new pool member, one of either C(address) or C(fqdn) must be
|
|
|
|
provided. This parameter cannot be updated after it is set.
|
2016-07-08 05:56:09 +00:00
|
|
|
aliases:
|
2018-04-25 14:16:11 +00:00
|
|
|
- hostname
|
|
|
|
version_added: 2.6
|
2016-07-08 05:56:09 +00:00
|
|
|
port:
|
2014-09-26 01:01:01 +00:00
|
|
|
description:
|
2018-01-17 05:02:33 +00:00
|
|
|
- Pool member port.
|
2018-04-25 14:16:11 +00:00
|
|
|
- This value cannot be changed after it has been set.
|
2018-01-17 05:02:33 +00:00
|
|
|
required: True
|
2016-07-08 05:56:09 +00:00
|
|
|
connection_limit:
|
|
|
|
description:
|
|
|
|
- Pool member connection limit. Setting this to 0 disables the limit.
|
|
|
|
description:
|
|
|
|
description:
|
2018-01-17 05:02:33 +00:00
|
|
|
- Pool member description.
|
2016-07-08 05:56:09 +00:00
|
|
|
rate_limit:
|
|
|
|
description:
|
|
|
|
- Pool member rate limit (connections-per-second). Setting this to 0
|
|
|
|
disables the limit.
|
|
|
|
ratio:
|
|
|
|
description:
|
|
|
|
- Pool member ratio weight. Valid values range from 1 through 100.
|
2016-12-11 02:50:09 +00:00
|
|
|
New pool members -- unless overridden with this value -- default
|
2016-07-08 05:56:09 +00:00
|
|
|
to 1.
|
|
|
|
preserve_node:
|
|
|
|
description:
|
2018-04-25 14:16:11 +00:00
|
|
|
- When state is C(absent) attempts to remove the node that the pool
|
|
|
|
member references.
|
|
|
|
- The node will not be removed if it is still referenced by other pool
|
|
|
|
members. If this happens, the module will not raise an error.
|
|
|
|
- Setting this to C(yes) disables this behavior.
|
2018-04-24 17:05:50 +00:00
|
|
|
type: bool
|
2016-07-08 05:56:09 +00:00
|
|
|
version_added: 2.1
|
2018-01-20 18:40:20 +00:00
|
|
|
priority_group:
|
|
|
|
description:
|
|
|
|
- Specifies a number representing the priority group for the pool member.
|
|
|
|
- When adding a new member, the default is 0, meaning that the member has no priority.
|
|
|
|
- To specify a priority, you must activate priority group usage when you
|
|
|
|
create a new pool or when adding or removing pool members. When activated,
|
|
|
|
the system load balances traffic according to the priority group number
|
|
|
|
assigned to the pool member.
|
|
|
|
- The higher the number, the higher the priority, so a member with a priority
|
|
|
|
of 3 has higher priority than a member with a priority of 1.
|
|
|
|
version_added: 2.5
|
2018-04-25 14:16:11 +00:00
|
|
|
fqdn_auto_populate:
|
|
|
|
description:
|
|
|
|
- Specifies whether the system automatically creates ephemeral nodes using
|
|
|
|
the IP addresses returned by the resolution of a DNS query for a node
|
|
|
|
defined by an FQDN.
|
2018-08-21 22:01:52 +00:00
|
|
|
- When C(yes), the system generates an ephemeral node for each IP address
|
2018-04-25 14:16:11 +00:00
|
|
|
returned in response to a DNS query for the FQDN of the node. Additionally,
|
|
|
|
when a DNS response indicates the IP address of an ephemeral node no longer
|
|
|
|
exists, the system deletes the ephemeral node.
|
2018-08-21 22:01:52 +00:00
|
|
|
- When C(no), the system resolves a DNS query for the FQDN of the node
|
2018-04-25 14:16:11 +00:00
|
|
|
with the single IP address associated with the FQDN.
|
|
|
|
- When creating a new pool member, the default for this parameter is C(yes).
|
|
|
|
- This parameter is ignored when C(reuse_nodes) is C(yes).
|
|
|
|
type: bool
|
|
|
|
version_added: 2.6
|
|
|
|
reuse_nodes:
|
|
|
|
description:
|
|
|
|
- Reuses node definitions if requested.
|
|
|
|
default: yes
|
|
|
|
type: bool
|
|
|
|
version_added: 2.6
|
2018-11-10 00:48:08 +00:00
|
|
|
monitors:
|
|
|
|
description:
|
|
|
|
- Specifies the health monitors that the system currently uses to monitor
|
|
|
|
this resource.
|
|
|
|
version_added: 2.8
|
|
|
|
availability_requirements:
|
|
|
|
description:
|
|
|
|
- Specifies, if you activate more than one health monitor, the number of health
|
|
|
|
monitors that must receive successful responses in order for the link to be
|
|
|
|
considered available.
|
|
|
|
suboptions:
|
|
|
|
type:
|
|
|
|
description:
|
|
|
|
- Monitor rule type when C(monitors) is specified.
|
|
|
|
- When creating a new pool, if this value is not specified, the default of
|
|
|
|
'all' will be used.
|
|
|
|
choices: ['all', 'at_least']
|
|
|
|
at_least:
|
|
|
|
description:
|
|
|
|
- Specifies the minimum number of active health monitors that must be successful
|
|
|
|
before the link is considered up.
|
|
|
|
- This parameter is only relevant when a C(type) of C(at_least) is used.
|
|
|
|
- This parameter will be ignored if a type of C(all) is used.
|
|
|
|
version_added: 2.8
|
|
|
|
ip_encapsulation:
|
|
|
|
description:
|
|
|
|
- Specifies the IP encapsulation using either IPIP (IP encapsulation within IP,
|
|
|
|
RFC 2003) or GRE (Generic Router Encapsulation, RFC 2784) on outbound packets
|
|
|
|
(from BIG-IP system to server-pool member).
|
|
|
|
- When C(none), disables IP encapsulation.
|
|
|
|
- When C(inherit), inherits IP encapsulation setting from the member's pool.
|
|
|
|
- When any other value, Options are None, Inherit from Pool, and Member Specific.
|
|
|
|
version_added: 2.8
|
2016-08-23 17:08:41 +00:00
|
|
|
extends_documentation_fragment: f5
|
2018-04-25 14:16:11 +00:00
|
|
|
author:
|
|
|
|
- Tim Rupp (@caphrim007)
|
2018-11-10 00:48:08 +00:00
|
|
|
- Wojciech Wypior (@wojtek0806)
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2016-07-08 05:56:09 +00:00
|
|
|
- name: Add pool member
|
|
|
|
bigip_pool_member:
|
2018-01-17 05:02:33 +00:00
|
|
|
pool: my-pool
|
|
|
|
partition: Common
|
|
|
|
host: "{{ ansible_default_ipv4['address'] }}"
|
|
|
|
port: 80
|
|
|
|
description: web server
|
|
|
|
connection_limit: 100
|
|
|
|
rate_limit: 50
|
|
|
|
ratio: 2
|
2018-11-10 00:48:08 +00:00
|
|
|
provider:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
user: admin
|
|
|
|
password: secret
|
2016-07-08 05:56:09 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
- name: Modify pool member ratio and description
|
|
|
|
bigip_pool_member:
|
2018-01-17 05:02:33 +00:00
|
|
|
pool: my-pool
|
|
|
|
partition: Common
|
|
|
|
host: "{{ ansible_default_ipv4['address'] }}"
|
|
|
|
port: 80
|
|
|
|
ratio: 1
|
|
|
|
description: nginx server
|
2018-11-10 00:48:08 +00:00
|
|
|
provider:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
user: admin
|
|
|
|
password: secret
|
2016-07-08 05:56:09 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
- name: Remove pool member from pool
|
|
|
|
bigip_pool_member:
|
2018-01-17 05:02:33 +00:00
|
|
|
state: absent
|
|
|
|
pool: my-pool
|
|
|
|
partition: Common
|
|
|
|
host: "{{ ansible_default_ipv4['address'] }}"
|
|
|
|
port: 80
|
2018-11-10 00:48:08 +00:00
|
|
|
provider:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
user: admin
|
|
|
|
password: secret
|
2016-07-08 05:56:09 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
- name: Force pool member offline
|
|
|
|
bigip_pool_member:
|
2018-04-25 14:16:11 +00:00
|
|
|
state: forced_offline
|
2018-01-17 05:02:33 +00:00
|
|
|
pool: my-pool
|
|
|
|
partition: Common
|
|
|
|
host: "{{ ansible_default_ipv4['address'] }}"
|
|
|
|
port: 80
|
2018-11-10 00:48:08 +00:00
|
|
|
provider:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
user: admin
|
|
|
|
password: secret
|
2016-07-08 05:56:09 +00:00
|
|
|
delegate_to: localhost
|
2018-04-25 14:16:11 +00:00
|
|
|
|
|
|
|
- name: Create members with priority groups
|
|
|
|
bigip_pool_member:
|
|
|
|
pool: my-pool
|
|
|
|
partition: Common
|
|
|
|
host: "{{ item.address }}"
|
|
|
|
name: "{{ item.name }}"
|
|
|
|
priority_group: "{{ item.priority_group }}"
|
|
|
|
port: 80
|
2018-11-10 00:48:08 +00:00
|
|
|
provider:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
user: admin
|
|
|
|
password: secret
|
2018-04-25 14:16:11 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
loop:
|
|
|
|
- host: 1.1.1.1
|
|
|
|
name: web1
|
|
|
|
priority_group: 4
|
|
|
|
- host: 2.2.2.2
|
|
|
|
name: web2
|
|
|
|
priority_group: 3
|
|
|
|
- host: 3.3.3.3
|
|
|
|
name: web3
|
|
|
|
priority_group: 2
|
|
|
|
- host: 4.4.4.4
|
|
|
|
name: web4
|
|
|
|
priority_group: 1
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
RETURN = '''
|
|
|
|
rate_limit:
|
|
|
|
description: The new rate limit, in connections per second, of the pool member.
|
|
|
|
returned: changed
|
|
|
|
type: int
|
|
|
|
sample: 100
|
|
|
|
connection_limit:
|
|
|
|
description: The new connection limit of the pool member
|
|
|
|
returned: changed
|
|
|
|
type: int
|
|
|
|
sample: 1000
|
|
|
|
description:
|
|
|
|
description: The new description of pool member.
|
|
|
|
returned: changed
|
2018-12-18 21:25:30 +00:00
|
|
|
type: str
|
2018-04-25 14:16:11 +00:00
|
|
|
sample: My pool member
|
|
|
|
ratio:
|
|
|
|
description: The new pool member ratio weight.
|
|
|
|
returned: changed
|
|
|
|
type: int
|
|
|
|
sample: 50
|
|
|
|
priority_group:
|
|
|
|
description: The new priority group.
|
|
|
|
returned: changed
|
|
|
|
type: int
|
|
|
|
sample: 3
|
|
|
|
fqdn_auto_populate:
|
|
|
|
description: Whether FQDN auto population was set on the member or not.
|
|
|
|
returned: changed
|
|
|
|
type: bool
|
|
|
|
sample: True
|
|
|
|
fqdn:
|
|
|
|
description: The FQDN of the pool member.
|
|
|
|
returned: changed
|
2018-12-18 21:25:30 +00:00
|
|
|
type: str
|
2018-04-25 14:16:11 +00:00
|
|
|
sample: foo.bar.com
|
|
|
|
address:
|
|
|
|
description: The address of the pool member.
|
|
|
|
returned: changed
|
2018-12-18 21:25:30 +00:00
|
|
|
type: str
|
2018-04-25 14:16:11 +00:00
|
|
|
sample: 1.2.3.4
|
2018-11-10 00:48:08 +00:00
|
|
|
monitors:
|
|
|
|
description: The new list of monitors for the resource.
|
|
|
|
returned: changed
|
|
|
|
type: list
|
|
|
|
sample: ['/Common/monitor1', '/Common/monitor2']
|
2018-04-25 14:16:11 +00:00
|
|
|
'''
|
2018-01-17 05:02:33 +00:00
|
|
|
|
2018-11-10 00:48:08 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2018-01-17 05:02:33 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2018-01-20 04:25:47 +00:00
|
|
|
from ansible.module_utils.basic import env_fallback
|
2018-01-17 05:02:33 +00:00
|
|
|
|
|
|
|
try:
|
2018-09-13 19:26:17 +00:00
|
|
|
from library.module_utils.network.f5.bigip import F5RestClient
|
2018-04-25 14:16:11 +00:00
|
|
|
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 fq_name
|
2018-09-13 19:26:17 +00:00
|
|
|
from library.module_utils.network.f5.common import cleanup_tokens
|
|
|
|
from library.module_utils.network.f5.common import transform_name
|
|
|
|
from library.module_utils.network.f5.common import exit_json
|
|
|
|
from library.module_utils.network.f5.common import fail_json
|
2018-01-17 05:02:33 +00:00
|
|
|
from library.module_utils.network.f5.common import f5_argument_spec
|
2018-09-13 19:26:17 +00:00
|
|
|
from library.module_utils.network.f5.common import is_valid_hostname
|
2018-11-10 00:48:08 +00:00
|
|
|
from library.module_utils.network.f5.common import flatten_boolean
|
|
|
|
from library.module_utils.network.f5.compare import cmp_str_with_none
|
2018-08-21 22:01:52 +00:00
|
|
|
from library.module_utils.network.f5.ipaddress import is_valid_ip
|
|
|
|
from library.module_utils.network.f5.ipaddress import validate_ip_v6_address
|
2018-01-17 05:02:33 +00:00
|
|
|
except ImportError:
|
2018-09-13 19:26:17 +00:00
|
|
|
from ansible.module_utils.network.f5.bigip import F5RestClient
|
2018-04-25 14:16:11 +00:00
|
|
|
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 fq_name
|
2018-09-13 19:26:17 +00:00
|
|
|
from ansible.module_utils.network.f5.common import cleanup_tokens
|
|
|
|
from ansible.module_utils.network.f5.common import transform_name
|
|
|
|
from ansible.module_utils.network.f5.common import exit_json
|
|
|
|
from ansible.module_utils.network.f5.common import fail_json
|
|
|
|
from ansible.module_utils.network.f5.common import f5_argument_spec
|
2018-04-25 14:16:11 +00:00
|
|
|
from ansible.module_utils.network.f5.common import is_valid_hostname
|
2018-01-17 05:02:33 +00:00
|
|
|
from ansible.module_utils.network.f5.common import f5_argument_spec
|
2018-11-10 00:48:08 +00:00
|
|
|
from ansible.module_utils.network.f5.common import flatten_boolean
|
|
|
|
from ansible.module_utils.network.f5.compare import cmp_str_with_none
|
2018-08-21 22:01:52 +00:00
|
|
|
from ansible.module_utils.network.f5.ipaddress import is_valid_ip
|
|
|
|
from ansible.module_utils.network.f5.ipaddress import validate_ip_v6_address
|
2016-07-08 05:56:09 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
class Parameters(AnsibleF5Parameters):
|
|
|
|
api_map = {
|
|
|
|
'rateLimit': 'rate_limit',
|
|
|
|
'connectionLimit': 'connection_limit',
|
|
|
|
'priorityGroup': 'priority_group',
|
2018-11-10 00:48:08 +00:00
|
|
|
'monitor': 'monitors',
|
|
|
|
'inheritProfile': 'inherit_profile',
|
|
|
|
'profiles': 'ip_encapsulation',
|
2018-04-25 14:16:11 +00:00
|
|
|
}
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
api_attributes = [
|
2018-11-10 00:48:08 +00:00
|
|
|
'rateLimit',
|
|
|
|
'connectionLimit',
|
|
|
|
'description',
|
|
|
|
'ratio',
|
|
|
|
'priorityGroup',
|
|
|
|
'address',
|
|
|
|
'fqdn',
|
|
|
|
'session',
|
|
|
|
'state',
|
|
|
|
'monitor',
|
|
|
|
|
|
|
|
# These two settings are for IP Encapsulation
|
|
|
|
'inheritProfile',
|
|
|
|
'profiles',
|
2018-04-25 14:16:11 +00:00
|
|
|
]
|
2016-07-08 05:56:09 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
returnables = [
|
2018-11-10 00:48:08 +00:00
|
|
|
'rate_limit',
|
|
|
|
'connection_limit',
|
|
|
|
'description',
|
|
|
|
'ratio',
|
|
|
|
'priority_group',
|
|
|
|
'fqdn_auto_populate',
|
|
|
|
'session',
|
|
|
|
'state',
|
|
|
|
'fqdn',
|
|
|
|
'address',
|
|
|
|
'monitors',
|
|
|
|
|
|
|
|
# IP Encapsulation related
|
|
|
|
'inherit_profile',
|
|
|
|
'ip_encapsulation',
|
2018-04-25 14:16:11 +00:00
|
|
|
]
|
2016-07-08 05:56:09 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
updatables = [
|
2018-11-10 00:48:08 +00:00
|
|
|
'rate_limit',
|
|
|
|
'connection_limit',
|
|
|
|
'description',
|
|
|
|
'ratio',
|
|
|
|
'priority_group',
|
|
|
|
'fqdn_auto_populate',
|
|
|
|
'state',
|
|
|
|
'monitors',
|
|
|
|
'inherit_profile',
|
|
|
|
'ip_encapsulation',
|
2018-04-25 14:16:11 +00:00
|
|
|
]
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
class ModuleParameters(Parameters):
|
|
|
|
@property
|
|
|
|
def full_name(self):
|
2018-04-30 16:48:41 +00:00
|
|
|
delimiter = ':'
|
|
|
|
try:
|
2018-08-21 22:01:52 +00:00
|
|
|
if validate_ip_v6_address(self.full_name_dict['name']):
|
2018-04-30 16:48:41 +00:00
|
|
|
delimiter = '.'
|
2018-08-21 22:01:52 +00:00
|
|
|
except TypeError:
|
2018-04-30 16:48:41 +00:00
|
|
|
pass
|
|
|
|
return '{0}{1}{2}'.format(self.full_name_dict['name'], delimiter, self.port)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def full_name_dict(self):
|
2018-04-25 14:16:11 +00:00
|
|
|
if self._values['name'] is None:
|
|
|
|
name = self._values['address'] if self._values['address'] else self._values['fqdn']
|
|
|
|
else:
|
|
|
|
name = self._values['name']
|
2018-04-30 16:48:41 +00:00
|
|
|
return dict(
|
|
|
|
name=name,
|
|
|
|
port=self.port
|
|
|
|
)
|
2018-04-25 14:16:11 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def node_name(self):
|
2018-04-30 16:48:41 +00:00
|
|
|
return self.full_name_dict['name']
|
2018-04-25 14:16:11 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def fqdn_name(self):
|
|
|
|
return self._values['fqdn']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fqdn(self):
|
|
|
|
result = {}
|
|
|
|
if self.fqdn_auto_populate:
|
|
|
|
result['autopopulate'] = 'enabled'
|
|
|
|
else:
|
|
|
|
result['autopopulate'] = 'disabled'
|
|
|
|
if self._values['fqdn'] is None:
|
|
|
|
return result
|
|
|
|
if not is_valid_hostname(self._values['fqdn']):
|
|
|
|
raise F5ModuleError(
|
|
|
|
"The specified 'fqdn' is not a valid hostname."
|
|
|
|
)
|
|
|
|
result['tmName'] = self._values['fqdn']
|
|
|
|
return result
|
|
|
|
|
|
|
|
@property
|
|
|
|
def pool(self):
|
|
|
|
return fq_name(self.want.partition, self._values['pool'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port(self):
|
|
|
|
if 0 > int(self._values['port']) or int(self._values['port']) > 65535:
|
|
|
|
raise F5ModuleError(
|
|
|
|
"Valid ports must be in range 0 - 65535"
|
|
|
|
)
|
|
|
|
return int(self._values['port'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def address(self):
|
|
|
|
if self._values['address'] is None:
|
|
|
|
return None
|
|
|
|
elif self._values['address'] == 'any6':
|
|
|
|
return 'any6'
|
2018-11-10 00:48:08 +00:00
|
|
|
address = self._values['address'].split('%')[0]
|
|
|
|
if is_valid_ip(address):
|
2018-08-21 22:01:52 +00:00
|
|
|
return self._values['address']
|
|
|
|
raise F5ModuleError(
|
|
|
|
"The specified 'address' value is not a valid IP address."
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
if self._values['state'] == 'enabled':
|
|
|
|
return 'present'
|
|
|
|
return self._values['state']
|
2018-04-25 14:16:11 +00:00
|
|
|
|
2018-11-10 00:48:08 +00:00
|
|
|
@property
|
|
|
|
def monitors_list(self):
|
|
|
|
if self._values['monitors'] is None:
|
|
|
|
return []
|
|
|
|
try:
|
|
|
|
result = re.findall(r'/\w+/[^\s}]+', self._values['monitors'])
|
|
|
|
result.sort()
|
|
|
|
return result
|
|
|
|
except Exception:
|
|
|
|
return self._values['monitors']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def monitors(self):
|
|
|
|
if self._values['monitors'] is None:
|
|
|
|
return None
|
|
|
|
if len(self._values['monitors']) == 1 and self._values['monitors'][0] in ['', 'none']:
|
|
|
|
return 'default'
|
|
|
|
monitors = [fq_name(self.partition, x) for x in self.monitors_list]
|
|
|
|
if self.availability_requirement_type == 'at_least':
|
|
|
|
if self.at_least > len(self.monitors_list):
|
|
|
|
raise F5ModuleError(
|
|
|
|
"The 'at_least' value must not exceed the number of 'monitors'."
|
|
|
|
)
|
|
|
|
monitors = ' '.join(monitors)
|
|
|
|
result = 'min {0} of {{ {1} }}'.format(self.at_least, monitors)
|
|
|
|
else:
|
|
|
|
result = ' and '.join(monitors).strip()
|
|
|
|
return result
|
|
|
|
|
|
|
|
@property
|
|
|
|
def availability_requirement_type(self):
|
|
|
|
if self._values['availability_requirements'] is None:
|
|
|
|
return None
|
|
|
|
return self._values['availability_requirements']['type']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def at_least(self):
|
|
|
|
return self._get_availability_value('at_least')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ip_encapsulation(self):
|
|
|
|
if self._values['ip_encapsulation'] is None:
|
|
|
|
return None
|
|
|
|
if self._values['ip_encapsulation'] == 'inherit':
|
|
|
|
return 'inherit'
|
|
|
|
if self._values['ip_encapsulation'] in ['', 'none']:
|
|
|
|
return ''
|
|
|
|
return fq_name(self.partition, self._values['ip_encapsulation'])
|
|
|
|
|
|
|
|
def _get_availability_value(self, type):
|
|
|
|
if self._values['availability_requirements'] is None:
|
|
|
|
return None
|
|
|
|
if self._values['availability_requirements'][type] is None:
|
|
|
|
return None
|
|
|
|
return int(self._values['availability_requirements'][type])
|
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
|
|
|
|
class ApiParameters(Parameters):
|
2018-11-10 00:48:08 +00:00
|
|
|
@property
|
|
|
|
def ip_encapsulation(self):
|
|
|
|
"""Returns a simple name for the tunnel.
|
|
|
|
|
|
|
|
The API stores the data like so
|
|
|
|
|
|
|
|
"profiles": [
|
|
|
|
{
|
|
|
|
"name": "gre",
|
|
|
|
"partition": "Common",
|
|
|
|
"nameReference": {
|
|
|
|
"link": "https://localhost/mgmt/tm/net/tunnels/gre/~Common~gre?ver=13.1.0.7"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
This method returns that data as a simple profile name. For instance,
|
|
|
|
|
|
|
|
/Common/gre
|
|
|
|
|
|
|
|
This allows us to do comparisons of it in the Difference class and then,
|
|
|
|
as needed, translate it back to the more complex form in the UsableChanges
|
|
|
|
class.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string: The simple form representation of the tunnel
|
|
|
|
"""
|
|
|
|
if self._values['ip_encapsulation'] is None and self.inherit_profile == 'yes':
|
|
|
|
return 'inherit'
|
|
|
|
if self._values['ip_encapsulation'] is None and self.inherit_profile == 'no':
|
|
|
|
return ''
|
|
|
|
if self._values['ip_encapsulation'] is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# There can be only one
|
|
|
|
tunnel = self._values['ip_encapsulation'][0]
|
|
|
|
|
|
|
|
return fq_name(tunnel['partition'], tunnel['name'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def inherit_profile(self):
|
|
|
|
return flatten_boolean(self._values['inherit_profile'])
|
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
@property
|
|
|
|
def allow(self):
|
|
|
|
if self._values['allow'] is None:
|
|
|
|
return ''
|
|
|
|
if self._values['allow'][0] == 'All':
|
|
|
|
return 'all'
|
|
|
|
allow = self._values['allow']
|
|
|
|
result = list(set([str(x) for x in allow]))
|
|
|
|
result = sorted(result)
|
|
|
|
return result
|
|
|
|
|
|
|
|
@property
|
|
|
|
def rate_limit(self):
|
|
|
|
if self._values['rate_limit'] is None:
|
|
|
|
return None
|
|
|
|
if self._values['rate_limit'] == 'disabled':
|
|
|
|
return 0
|
|
|
|
return int(self._values['rate_limit'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2018-08-27 21:09:03 +00:00
|
|
|
if self._values['state'] in ['user-up', 'unchecked', 'fqdn-up-no-addr', 'fqdn-up'] and self._values['session'] in ['user-enabled']:
|
2018-04-25 14:16:11 +00:00
|
|
|
return 'present'
|
2018-11-10 00:48:08 +00:00
|
|
|
elif self._values['state'] in ['down', 'up', 'checking'] and self._values['session'] == 'monitor-enabled':
|
|
|
|
# monitor-enabled + checking:
|
|
|
|
# Monitor is checking to see state of pool member. For instance,
|
|
|
|
# whether it is up or down
|
|
|
|
#
|
|
|
|
# monitor-enabled + down:
|
|
|
|
# Monitor returned and determined that pool member is down.
|
|
|
|
#
|
|
|
|
# monitor-enabled + up
|
|
|
|
# Monitor returned and determined that pool member is up.
|
2018-06-01 21:38:23 +00:00
|
|
|
return 'present'
|
2018-04-25 14:16:11 +00:00
|
|
|
elif self._values['state'] in ['user-down'] and self._values['session'] in ['user-disabled']:
|
|
|
|
return 'forced_offline'
|
|
|
|
else:
|
|
|
|
return 'disabled'
|
2016-07-08 05:56:09 +00:00
|
|
|
|
2018-11-10 00:48:08 +00:00
|
|
|
@property
|
|
|
|
def availability_requirement_type(self):
|
|
|
|
if self._values['monitors'] is None:
|
|
|
|
return None
|
|
|
|
if 'min ' in self._values['monitors']:
|
|
|
|
return 'at_least'
|
|
|
|
else:
|
|
|
|
return 'all'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def monitors_list(self):
|
|
|
|
if self._values['monitors'] is None:
|
|
|
|
return []
|
|
|
|
try:
|
|
|
|
result = re.findall(r'/\w+/[^\s}]+', self._values['monitors'])
|
|
|
|
result.sort()
|
|
|
|
return result
|
|
|
|
except Exception:
|
|
|
|
return self._values['monitors']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def monitors(self):
|
|
|
|
if self._values['monitors'] is None:
|
|
|
|
return None
|
|
|
|
if self._values['monitors'] == 'default':
|
|
|
|
return 'default'
|
|
|
|
monitors = [fq_name(self.partition, x) for x in self.monitors_list]
|
|
|
|
if self.availability_requirement_type == 'at_least':
|
|
|
|
monitors = ' '.join(monitors)
|
|
|
|
result = 'min {0} of {{ {1} }}'.format(self.at_least, monitors)
|
|
|
|
else:
|
|
|
|
result = ' and '.join(monitors).strip()
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
@property
|
|
|
|
def at_least(self):
|
|
|
|
"""Returns the 'at least' value from the monitor string.
|
|
|
|
The monitor string for a Require monitor looks like this.
|
|
|
|
min 1 of { /Common/gateway_icmp }
|
|
|
|
This method parses out the first of the numeric values. This values represents
|
|
|
|
the "at_least" value that can be updated in the module.
|
|
|
|
Returns:
|
|
|
|
int: The at_least value if found. None otherwise.
|
|
|
|
"""
|
|
|
|
if self._values['monitors'] is None:
|
|
|
|
return None
|
|
|
|
pattern = r'min\s+(?P<least>\d+)\s+of\s+'
|
|
|
|
matches = re.search(pattern, self._values['monitors'])
|
|
|
|
if matches is None:
|
|
|
|
return None
|
|
|
|
return matches.group('least')
|
|
|
|
|
2018-12-19 04:32:06 +00:00
|
|
|
@property
|
|
|
|
def fqdn_auto_populate(self):
|
|
|
|
if self._values['fqdn'] is None:
|
|
|
|
return None
|
|
|
|
if 'autopopulate' in self._values['fqdn']:
|
|
|
|
if self._values['fqdn']['autopopulate'] == 'enabled':
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fqdn(self):
|
|
|
|
if self._values['fqdn'] is None:
|
|
|
|
return None
|
|
|
|
if 'tmName' in self._values['fqdn']:
|
|
|
|
return self._values['fqdn']['tmName']
|
|
|
|
|
2016-07-08 05:56:09 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
class NodeApiParameters(Parameters):
|
|
|
|
pass
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-08-01 14:15:54 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
class Changes(Parameters):
|
|
|
|
def to_return(self):
|
|
|
|
result = {}
|
|
|
|
try:
|
|
|
|
for returnable in self.returnables:
|
|
|
|
result[returnable] = getattr(self, returnable)
|
|
|
|
result = self._filter_params(result)
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
return result
|
2017-08-01 14:15:54 +00:00
|
|
|
|
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
class UsableChanges(Changes):
|
2018-11-10 00:48:08 +00:00
|
|
|
@property
|
|
|
|
def monitors(self):
|
|
|
|
monitor_string = self._values['monitors']
|
|
|
|
if monitor_string is None:
|
|
|
|
return None
|
|
|
|
if '{' in monitor_string and '}':
|
|
|
|
tmp = monitor_string.strip('}').split('{')
|
|
|
|
monitor = ''.join(tmp).rstrip()
|
|
|
|
return monitor
|
|
|
|
return monitor_string
|
2017-08-01 14:15:54 +00:00
|
|
|
|
2016-07-08 05:56:09 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
class ReportableChanges(Changes):
|
|
|
|
@property
|
|
|
|
def ssl_cipher_suite(self):
|
|
|
|
default = ':'.join(sorted(Parameters._ciphers.split(':')))
|
|
|
|
if self._values['ssl_cipher_suite'] == default:
|
|
|
|
return 'default'
|
|
|
|
else:
|
|
|
|
return self._values['ssl_cipher_suite']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fqdn_auto_populate(self):
|
|
|
|
if self._values['fqdn'] is None:
|
|
|
|
return None
|
|
|
|
if 'autopopulate' in self._values['fqdn']:
|
|
|
|
if self._values['fqdn']['autopopulate'] == 'enabled':
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fqdn(self):
|
|
|
|
if self._values['fqdn'] is None:
|
|
|
|
return None
|
|
|
|
if 'tmName' in self._values['fqdn']:
|
|
|
|
return self._values['fqdn']['tmName']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2018-08-27 21:09:03 +00:00
|
|
|
if self._values['state'] in ['user-up', 'unchecked', 'fqdn-up-no-addr', 'fqdn-up'] and self._values['session'] in ['user-enabled']:
|
2018-04-25 14:16:11 +00:00
|
|
|
return 'present'
|
2018-11-10 00:48:08 +00:00
|
|
|
elif self._values['state'] in ['down', 'up', 'checking'] and self._values['session'] == 'monitor-enabled':
|
2018-06-03 02:09:13 +00:00
|
|
|
return 'present'
|
2018-04-25 14:16:11 +00:00
|
|
|
elif self._values['state'] in ['user-down'] and self._values['session'] in ['user-disabled']:
|
|
|
|
return 'forced_offline'
|
|
|
|
else:
|
|
|
|
return 'disabled'
|
|
|
|
|
2018-11-10 00:48:08 +00:00
|
|
|
@property
|
|
|
|
def monitors(self):
|
|
|
|
if self._values['monitors'] is None:
|
|
|
|
return []
|
|
|
|
try:
|
|
|
|
result = re.findall(r'/\w+/[^\s}]+', self._values['monitors'])
|
|
|
|
result.sort()
|
|
|
|
return result
|
|
|
|
except Exception:
|
|
|
|
return self._values['monitors']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def availability_requirement_type(self):
|
|
|
|
if self._values['monitors'] is None:
|
|
|
|
return None
|
|
|
|
if 'min ' in self._values['monitors']:
|
|
|
|
return 'at_least'
|
|
|
|
else:
|
|
|
|
return 'all'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def at_least(self):
|
|
|
|
"""Returns the 'at least' value from the monitor string.
|
|
|
|
The monitor string for a Require monitor looks like this.
|
|
|
|
min 1 of { /Common/gateway_icmp }
|
|
|
|
This method parses out the first of the numeric values. This values represents
|
|
|
|
the "at_least" value that can be updated in the module.
|
|
|
|
Returns:
|
|
|
|
int: The at_least value if found. None otherwise.
|
|
|
|
"""
|
|
|
|
if self._values['monitors'] is None:
|
|
|
|
return None
|
|
|
|
pattern = r'min\s+(?P<least>\d+)\s+of\s+'
|
|
|
|
matches = re.search(pattern, self._values['monitors'])
|
|
|
|
if matches is None:
|
|
|
|
return None
|
|
|
|
return int(matches.group('least'))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def availability_requirements(self):
|
|
|
|
if self._values['monitors'] is None:
|
|
|
|
return None
|
|
|
|
result = dict()
|
|
|
|
result['type'] = self.availability_requirement_type
|
|
|
|
result['at_least'] = self.at_least
|
|
|
|
return result
|
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
|
|
|
|
class Difference(object):
|
|
|
|
def __init__(self, want, have=None):
|
|
|
|
self.want = want
|
|
|
|
self.have = have
|
|
|
|
|
|
|
|
def compare(self, param):
|
|
|
|
try:
|
|
|
|
result = getattr(self, param)
|
|
|
|
return result
|
|
|
|
except AttributeError:
|
|
|
|
return self.__default(param)
|
|
|
|
|
|
|
|
def __default(self, param):
|
|
|
|
attr1 = getattr(self.want, param)
|
|
|
|
try:
|
|
|
|
attr2 = getattr(self.have, param)
|
|
|
|
if attr1 != attr2:
|
|
|
|
return attr1
|
|
|
|
except AttributeError:
|
|
|
|
return attr1
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
if self.want.state == self.have.state:
|
|
|
|
return None
|
|
|
|
if self.want.state == 'forced_offline':
|
|
|
|
return {
|
|
|
|
'state': 'user-down',
|
|
|
|
'session': 'user-disabled'
|
|
|
|
}
|
|
|
|
elif self.want.state == 'disabled':
|
|
|
|
return {
|
|
|
|
'state': 'user-up',
|
|
|
|
'session': 'user-disabled'
|
|
|
|
}
|
|
|
|
elif self.want.state in ['present', 'enabled']:
|
|
|
|
return {
|
|
|
|
'state': 'user-up',
|
|
|
|
'session': 'user-enabled'
|
|
|
|
}
|
|
|
|
|
2018-11-10 00:48:08 +00:00
|
|
|
@property
|
|
|
|
def monitors(self):
|
|
|
|
if self.want.monitors is None:
|
|
|
|
return None
|
|
|
|
if self.want.monitors == 'default' and self.have.monitors == 'default':
|
|
|
|
return None
|
|
|
|
if self.want.monitors == 'default' and self.have.monitors is None:
|
|
|
|
return None
|
|
|
|
if self.want.monitors == 'default' and len(self.have.monitors) > 0:
|
|
|
|
return 'default'
|
|
|
|
if self.have.monitors is None:
|
|
|
|
return self.want.monitors
|
|
|
|
if self.have.monitors != self.want.monitors:
|
|
|
|
return self.want.monitors
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ip_encapsulation(self):
|
|
|
|
result = cmp_str_with_none(self.want.ip_encapsulation, self.have.ip_encapsulation)
|
|
|
|
if result is None:
|
|
|
|
return None
|
|
|
|
if result == 'inherit':
|
|
|
|
return dict(
|
|
|
|
inherit_profile='enabled',
|
|
|
|
ip_encapsulation=[]
|
|
|
|
)
|
|
|
|
elif result in ['', 'none']:
|
|
|
|
return dict(
|
|
|
|
inherit_profile='disabled',
|
|
|
|
ip_encapsulation=[]
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return dict(
|
|
|
|
inherit_profile='disabled',
|
|
|
|
ip_encapsulation=[
|
|
|
|
dict(
|
|
|
|
name=os.path.basename(result).strip('/'),
|
|
|
|
partition=os.path.dirname(result)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
|
|
|
|
class ModuleManager(object):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.module = kwargs.get('module', None)
|
|
|
|
self.client = kwargs.get('client', None)
|
|
|
|
self.want = ModuleParameters(params=self.module.params)
|
|
|
|
self.have = ApiParameters()
|
|
|
|
self.changes = UsableChanges()
|
|
|
|
|
|
|
|
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:
|
|
|
|
self.changes = UsableChanges(params=changed)
|
|
|
|
|
|
|
|
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:
|
|
|
|
if isinstance(change, dict):
|
|
|
|
changed.update(change)
|
|
|
|
else:
|
|
|
|
changed[k] = change
|
|
|
|
if changed:
|
|
|
|
self.changes = UsableChanges(params=changed)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def should_update(self):
|
|
|
|
result = self._update_changed_options()
|
|
|
|
if result:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def exec_module(self):
|
|
|
|
changed = False
|
|
|
|
result = dict()
|
|
|
|
state = self.want.state
|
|
|
|
|
2018-09-13 19:26:17 +00:00
|
|
|
if state in ['present', 'present', 'enabled', 'disabled', 'forced_offline']:
|
|
|
|
changed = self.present()
|
|
|
|
elif state == "absent":
|
|
|
|
changed = self.absent()
|
2018-04-25 14:16:11 +00:00
|
|
|
|
|
|
|
reportable = ReportableChanges(params=self.changes.to_return())
|
|
|
|
changes = reportable.to_return()
|
|
|
|
result.update(**changes)
|
|
|
|
result.update(dict(changed=changed))
|
|
|
|
self._announce_deprecations(result)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def _announce_deprecations(self, result):
|
|
|
|
warnings = result.pop('__warnings', [])
|
|
|
|
for warning in warnings:
|
|
|
|
self.module.deprecate(
|
|
|
|
msg=warning['msg'],
|
|
|
|
version=warning['version']
|
|
|
|
)
|
|
|
|
|
|
|
|
def present(self):
|
|
|
|
if self.exists():
|
|
|
|
return self.update()
|
|
|
|
else:
|
|
|
|
return self.create()
|
|
|
|
|
|
|
|
def exists(self):
|
2018-09-13 19:26:17 +00:00
|
|
|
if not self.pool_exist():
|
2018-11-01 20:35:56 +00:00
|
|
|
raise F5ModuleError('The specified pool does not exist')
|
2018-09-13 19:26:17 +00:00
|
|
|
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/pool/{2}/members/{3}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
2018-11-10 00:48:08 +00:00
|
|
|
transform_name(name=fq_name(self.want.partition, self.want.pool)),
|
2018-09-13 19:26:17 +00:00
|
|
|
transform_name(self.want.partition, self.want.full_name)
|
|
|
|
)
|
|
|
|
resp = self.client.api.get(uri)
|
2018-04-25 14:16:11 +00:00
|
|
|
try:
|
2018-09-13 19:26:17 +00:00
|
|
|
response = resp.json()
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
if resp.status == 404 or 'code' in response and response['code'] == 404:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def pool_exist(self):
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/pool/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
2018-11-10 00:48:08 +00:00
|
|
|
transform_name(name=fq_name(self.want.partition, self.want.pool))
|
2018-04-25 14:16:11 +00:00
|
|
|
)
|
2018-09-13 19:26:17 +00:00
|
|
|
resp = self.client.api.get(uri)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
if resp.status == 404 or 'code' in response and response['code'] == 404:
|
|
|
|
return False
|
|
|
|
return True
|
2015-02-25 23:48:19 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
def node_exists(self):
|
2018-09-13 19:26:17 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/node/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.node_name)
|
2018-04-25 14:16:11 +00:00
|
|
|
)
|
2018-09-13 19:26:17 +00:00
|
|
|
resp = self.client.api.get(uri)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
if resp.status == 404 or 'code' in response and response['code'] == 404:
|
|
|
|
return False
|
|
|
|
return True
|
2018-04-25 14:16:11 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.have = self.read_current_from_device()
|
|
|
|
if not self.should_update():
|
|
|
|
return False
|
|
|
|
if self.module.check_mode:
|
|
|
|
return True
|
|
|
|
self.update_on_device()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
if self.module.check_mode:
|
|
|
|
return True
|
|
|
|
self.remove_from_device()
|
|
|
|
if not self.want.preserve_node:
|
|
|
|
self.remove_node_from_device()
|
|
|
|
if self.exists():
|
|
|
|
raise F5ModuleError("Failed to delete the resource.")
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _set_host_by_name(self):
|
2018-08-21 22:01:52 +00:00
|
|
|
if is_valid_ip(self.want.name):
|
2018-04-25 14:16:11 +00:00
|
|
|
self.want.update({
|
|
|
|
'fqdn': None,
|
|
|
|
'address': self.want.name
|
|
|
|
})
|
2018-08-21 22:01:52 +00:00
|
|
|
else:
|
2018-04-25 14:16:11 +00:00
|
|
|
if not is_valid_hostname(self.want.name):
|
|
|
|
raise F5ModuleError(
|
|
|
|
"'name' is neither a valid IP address or FQDN name."
|
|
|
|
)
|
|
|
|
self.want.update({
|
|
|
|
'fqdn': self.want.name,
|
|
|
|
'address': None
|
|
|
|
})
|
|
|
|
|
|
|
|
def _update_api_state_attributes(self):
|
|
|
|
if self.want.state == 'forced_offline':
|
|
|
|
self.want.update({
|
|
|
|
'state': 'user-down',
|
|
|
|
'session': 'user-disabled',
|
|
|
|
})
|
|
|
|
elif self.want.state == 'disabled':
|
|
|
|
self.want.update({
|
|
|
|
'state': 'user-up',
|
|
|
|
'session': 'user-disabled',
|
|
|
|
})
|
|
|
|
elif self.want.state in ['present', 'enabled']:
|
|
|
|
self.want.update({
|
|
|
|
'state': 'user-up',
|
|
|
|
'session': 'user-enabled',
|
|
|
|
})
|
|
|
|
|
|
|
|
def _update_address_with_existing_nodes(self):
|
|
|
|
try:
|
|
|
|
have = self.read_current_node_from_device(self.want.node_name)
|
|
|
|
|
|
|
|
if self.want.fqdn_auto_populate and self.want.reuse_nodes:
|
|
|
|
self.module.warn("'fqdn_auto_populate' is discarded in favor of the re-used node's auto-populate setting.")
|
|
|
|
self.want.update({
|
|
|
|
'fqdn_auto_populate': True if have.fqdn['autopopulate'] == 'enabled' else False
|
|
|
|
})
|
|
|
|
if 'tmName' in have.fqdn:
|
|
|
|
self.want.update({
|
|
|
|
'fqdn': have.fqdn['tmName'],
|
|
|
|
'address': 'any6'
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
self.want.update({
|
|
|
|
'address': have.address
|
|
|
|
})
|
|
|
|
except Exception:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def create(self):
|
|
|
|
if self.want.reuse_nodes:
|
|
|
|
self._update_address_with_existing_nodes()
|
2018-11-10 00:48:08 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
if self.want.name and not any(x for x in [self.want.address, self.want.fqdn_name]):
|
|
|
|
self._set_host_by_name()
|
|
|
|
|
2018-11-10 00:48:08 +00:00
|
|
|
if self.want.ip_encapsulation == '':
|
|
|
|
self.changes.update({'inherit_profile': 'enabled'})
|
|
|
|
self.changes.update({'profiles': []})
|
|
|
|
elif self.want.ip_encapsulation:
|
|
|
|
# Read the current list of tunnels so that IP encapsulation
|
|
|
|
# checking can take place.
|
|
|
|
tunnels_gre = self.read_current_tunnels_from_device('gre')
|
|
|
|
tunnels_ipip = self.read_current_tunnels_from_device('ipip')
|
|
|
|
tunnels = tunnels_gre + tunnels_ipip
|
|
|
|
if self.want.ip_encapsulation not in tunnels:
|
|
|
|
raise F5ModuleError(
|
|
|
|
"The specified 'ip_encapsulation' tunnel was not found on the system."
|
|
|
|
)
|
|
|
|
self.changes.update({'inherit_profile': 'disabled'})
|
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
self._update_api_state_attributes()
|
|
|
|
self._set_changed_options()
|
|
|
|
if self.module.check_mode:
|
|
|
|
return True
|
|
|
|
self.create_on_device()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def create_on_device(self):
|
|
|
|
params = self.changes.api_params()
|
2018-09-13 19:26:17 +00:00
|
|
|
params['name'] = self.want.full_name
|
|
|
|
params['partition'] = self.want.partition
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/pool/{2}/members".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
2018-11-10 00:48:08 +00:00
|
|
|
transform_name(name=fq_name(self.want.partition, self.want.pool)),
|
2018-09-13 19:26:17 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
)
|
2018-09-13 19:26:17 +00:00
|
|
|
resp = self.client.api.post(uri, json=params)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError as ex:
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] in [400, 403]:
|
|
|
|
if 'message' in response:
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
else:
|
|
|
|
raise F5ModuleError(resp.content)
|
2015-02-25 23:48:19 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
def update_on_device(self):
|
|
|
|
params = self.changes.api_params()
|
2018-09-13 19:26:17 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/pool/{2}/members/{3}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
2018-11-10 00:48:08 +00:00
|
|
|
transform_name(name=fq_name(self.want.partition, self.want.pool)),
|
2018-09-13 19:26:17 +00:00
|
|
|
transform_name(self.want.partition, self.want.full_name)
|
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
)
|
2018-09-13 19:26:17 +00:00
|
|
|
resp = self.client.api.patch(uri, json=params)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError as ex:
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
if 'message' in response:
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
else:
|
|
|
|
raise F5ModuleError(resp.content)
|
2018-04-25 14:16:11 +00:00
|
|
|
|
|
|
|
def absent(self):
|
|
|
|
if self.exists():
|
|
|
|
return self.remove()
|
|
|
|
elif not self.want.preserve_node and self.node_exists():
|
|
|
|
return self.remove_node_from_device()
|
|
|
|
return False
|
|
|
|
|
|
|
|
def remove_from_device(self):
|
2018-09-13 19:26:17 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/pool/{2}/members/{3}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
2018-11-10 00:48:08 +00:00
|
|
|
transform_name(name=fq_name(self.want.partition, self.want.pool)),
|
2018-09-13 19:26:17 +00:00
|
|
|
transform_name(self.want.partition, self.want.full_name)
|
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
)
|
2018-09-13 19:26:17 +00:00
|
|
|
response = self.client.api.delete(uri)
|
|
|
|
if response.status == 200:
|
|
|
|
return True
|
|
|
|
raise F5ModuleError(response.content)
|
2016-07-08 05:56:09 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
def remove_node_from_device(self):
|
2018-09-13 19:26:17 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/node/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.node_name)
|
2018-04-25 14:16:11 +00:00
|
|
|
)
|
2018-09-13 19:26:17 +00:00
|
|
|
response = self.client.api.delete(uri)
|
|
|
|
if response.status == 200:
|
|
|
|
return True
|
|
|
|
raise F5ModuleError(response.content)
|
2016-07-08 05:56:09 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
def read_current_from_device(self):
|
2018-09-13 19:26:17 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/pool/{2}/members/{3}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
2018-11-10 00:48:08 +00:00
|
|
|
transform_name(name=fq_name(self.want.partition, self.want.pool)),
|
2018-09-13 19:26:17 +00:00
|
|
|
transform_name(self.want.partition, self.want.full_name)
|
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
)
|
2018-09-13 19:26:17 +00:00
|
|
|
resp = self.client.api.get(uri)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError as ex:
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
if 'message' in response:
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
else:
|
|
|
|
raise F5ModuleError(resp.content)
|
2018-11-10 00:48:08 +00:00
|
|
|
|
|
|
|
# Read the current list of tunnels so that IP encapsulation
|
|
|
|
# checking can take place.
|
|
|
|
tunnels_gre = self.read_current_tunnels_from_device('gre')
|
|
|
|
tunnels_ipip = self.read_current_tunnels_from_device('ipip')
|
|
|
|
response['tunnels'] = tunnels_gre + tunnels_ipip
|
|
|
|
|
2018-09-13 19:26:17 +00:00
|
|
|
return ApiParameters(params=response)
|
2015-02-25 23:48:19 +00:00
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
def read_current_node_from_device(self, node):
|
2018-09-13 19:26:17 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/node/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, node)
|
2018-04-25 14:16:11 +00:00
|
|
|
)
|
2018-09-13 19:26:17 +00:00
|
|
|
resp = self.client.api.get(uri)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError as ex:
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
if 'message' in response:
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
else:
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
return NodeApiParameters(params=response)
|
2018-04-25 14:16:11 +00:00
|
|
|
|
2018-11-10 00:48:08 +00:00
|
|
|
def read_current_tunnels_from_device(self, tunnel_type):
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/net/tunnels/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
tunnel_type
|
|
|
|
)
|
|
|
|
resp = self.client.api.get(uri)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError as ex:
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
if 'message' in response:
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
else:
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
if 'items' not in response:
|
|
|
|
return []
|
|
|
|
return [x['fullPath'] for x in response['items']]
|
|
|
|
|
2018-04-25 14:16:11 +00:00
|
|
|
|
|
|
|
class ArgumentSpec(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.supports_check_mode = True
|
|
|
|
argument_spec = dict(
|
|
|
|
pool=dict(required=True),
|
|
|
|
address=dict(aliases=['host', 'ip']),
|
|
|
|
fqdn=dict(
|
|
|
|
aliases=['hostname']
|
|
|
|
),
|
|
|
|
name=dict(),
|
|
|
|
port=dict(type='int', required=True),
|
|
|
|
connection_limit=dict(type='int'),
|
|
|
|
description=dict(),
|
|
|
|
rate_limit=dict(type='int'),
|
|
|
|
ratio=dict(type='int'),
|
|
|
|
preserve_node=dict(type='bool'),
|
|
|
|
priority_group=dict(type='int'),
|
|
|
|
state=dict(
|
|
|
|
default='present',
|
|
|
|
choices=['absent', 'present', 'enabled', 'disabled', 'forced_offline']
|
|
|
|
),
|
|
|
|
partition=dict(
|
|
|
|
default='Common',
|
|
|
|
fallback=(env_fallback, ['F5_PARTITION'])
|
|
|
|
),
|
|
|
|
fqdn_auto_populate=dict(type='bool'),
|
|
|
|
reuse_nodes=dict(type='bool', default=True),
|
2018-11-10 00:48:08 +00:00
|
|
|
availability_requirements=dict(
|
|
|
|
type='dict',
|
|
|
|
options=dict(
|
|
|
|
type=dict(
|
|
|
|
choices=['all', 'at_least'],
|
|
|
|
required=True
|
|
|
|
),
|
|
|
|
at_least=dict(type='int'),
|
|
|
|
),
|
|
|
|
required_if=[
|
|
|
|
['type', 'at_least', ['at_least']],
|
|
|
|
]
|
|
|
|
),
|
|
|
|
monitors=dict(type='list'),
|
|
|
|
ip_encapsulation=dict(),
|
2018-04-25 14:16:11 +00:00
|
|
|
)
|
|
|
|
self.argument_spec = {}
|
|
|
|
self.argument_spec.update(f5_argument_spec)
|
|
|
|
self.argument_spec.update(argument_spec)
|
|
|
|
self.mutually_exclusive = [
|
|
|
|
['address', 'fqdn']
|
|
|
|
]
|
|
|
|
self.required_one_of = [
|
|
|
|
['name', 'address', 'fqdn'],
|
|
|
|
]
|
2015-02-25 23:48:19 +00:00
|
|
|
|
2016-07-08 05:56:09 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def main():
|
2018-04-25 14:16:11 +00:00
|
|
|
spec = ArgumentSpec()
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2015-06-03 06:22:18 +00:00
|
|
|
module = AnsibleModule(
|
2018-04-25 14:16:11 +00:00
|
|
|
argument_spec=spec.argument_spec,
|
2018-09-13 19:26:17 +00:00
|
|
|
supports_check_mode=spec.supports_check_mode,
|
2015-06-03 06:22:18 +00:00
|
|
|
)
|
2018-09-13 19:26:17 +00:00
|
|
|
|
|
|
|
client = F5RestClient(**module.params)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
try:
|
2018-04-25 14:16:11 +00:00
|
|
|
mm = ModuleManager(module=module, client=client)
|
|
|
|
results = mm.exec_module()
|
|
|
|
cleanup_tokens(client)
|
2018-09-13 19:26:17 +00:00
|
|
|
exit_json(module, results, client)
|
2018-04-25 14:16:11 +00:00
|
|
|
except F5ModuleError as ex:
|
|
|
|
cleanup_tokens(client)
|
2018-09-13 19:26:17 +00:00
|
|
|
fail_json(module, ex, client)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-04-25 19:34:18 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|