1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Bugfixes and code style ()

A number of coding conventions have been adopted for new F5 modules
that are in development. To ensure common usage across the modules,
this module needed to be updated to reflect those conventions.

Additionally, this patch fixes a couple bugs in the module that were
preventing it from being idempotent.
This commit is contained in:
Tim Rupp 2016-07-28 08:05:40 -07:00 committed by Matt Clay
parent ef184e93a4
commit 9ca08e5596

View file

@ -23,171 +23,176 @@ DOCUMENTATION = '''
module: bigip_monitor_tcp module: bigip_monitor_tcp
short_description: "Manages F5 BIG-IP LTM tcp monitors" short_description: "Manages F5 BIG-IP LTM tcp monitors"
description: description:
- "Manages F5 BIG-IP LTM tcp monitors via iControl SOAP API" - "Manages F5 BIG-IP LTM tcp monitors via iControl SOAP API"
version_added: "1.4" version_added: "1.4"
author: author:
- Serge van Ginderachter (@srvg) - Serge van Ginderachter (@srvg)
- Tim Rupp (@caphrim007) - Tim Rupp (@caphrim007)
notes: notes:
- "Requires BIG-IP software version >= 11" - "Requires BIG-IP software version >= 11"
- "F5 developed module 'bigsuds' required (see http://devcentral.f5.com)" - "F5 developed module 'bigsuds' required (see http://devcentral.f5.com)"
- "Best run as a local_action in your playbook" - "Best run as a local_action in your playbook"
- "Monitor API documentation: https://devcentral.f5.com/wiki/iControl.LocalLB__Monitor.ashx" - "Monitor API documentation: https://devcentral.f5.com/wiki/iControl.LocalLB__Monitor.ashx"
requirements: requirements:
- bigsuds - bigsuds
options: options:
server: server:
description: description:
- BIG-IP host - BIG-IP host
required: true required: true
default: null default: null
server_port: server_port:
description: description:
- BIG-IP server port - BIG-IP server port
required: false required: false
default: 443 default: 443
version_added: "2.2" version_added: "2.2"
user: user:
description: description:
- BIG-IP username - BIG-IP username
required: true required: true
default: null default: null
password: password:
description: description:
- BIG-IP password - BIG-IP password
required: true required: true
default: null default: null
validate_certs: validate_certs:
description: description:
- If C(no), SSL certificates will not be validated. This should only be used - If C(no), SSL certificates will not be validated. This should only be used
on personally controlled sites. Prior to 2.0, this module would always on personally controlled sites. Prior to 2.0, this module would always
validate on python >= 2.7.9 and never validate on python <= 2.7.8 validate on python >= 2.7.9 and never validate on python <= 2.7.8
required: false required: false
default: 'yes' default: 'yes'
choices: ['yes', 'no'] choices:
version_added: 1.9.1 - yes
state: - no
description: version_added: 2.0
- Monitor state state:
required: false description:
default: 'present' - Monitor state
choices: ['present', 'absent'] required: false
name: default: 'present'
description: choices:
- Monitor name - present
required: true - absent
default: null name:
aliases: ['monitor'] description:
partition: - Monitor name
description: required: true
- Partition for the monitor default: null
required: false aliases:
default: 'Common' - monitor
type: partition:
description: description:
- The template type of this monitor template - Partition for the monitor
required: false required: false
default: 'tcp' default: 'Common'
choices: [ 'TTYPE_TCP', 'TTYPE_TCP_ECHO', 'TTYPE_TCP_HALF_OPEN'] type:
parent: description:
description: - The template type of this monitor template
- The parent template of this monitor template required: false
required: false default: 'tcp'
default: 'tcp' choices:
choices: [ 'tcp', 'tcp_echo', 'tcp_half_open'] - TTYPE_TCP
parent_partition: - TTYPE_TCP_ECHO
description: - TTYPE_TCP_HALF_OPEN
- Partition for the parent monitor parent:
required: false description:
default: 'Common' - The parent template of this monitor template
send: required: false
description: default: 'tcp'
- The send string for the monitor call choices:
required: true - tcp
default: none - tcp_echo
receive: - tcp_half_open
description: parent_partition:
- The receive string for the monitor call description:
required: true - Partition for the parent monitor
default: none required: false
ip: default: 'Common'
description: send:
- IP address part of the ipport definition. The default API setting description:
is "0.0.0.0". - The send string for the monitor call
required: false required: true
default: none default: none
port: receive:
description: description:
- port address part op the ipport definition. The default API - The receive string for the monitor call
setting is 0. required: true
required: false default: none
default: none ip:
interval: description:
description: - IP address part of the ipport definition. The default API setting
- The interval specifying how frequently the monitor instance is "0.0.0.0".
of this template will run. By default, this interval is used for up and required: false
down states. The default API setting is 5. default: none
required: false port:
default: none description:
timeout: - Port address part op the ipport definition. The default API
description: setting is 0.
- The number of seconds in which the node or service must respond to required: false
the monitor request. If the target responds within the set time default: none
period, it is considered up. If the target does not respond within interval:
the set time period, it is considered down. You can change this description:
number to any number you want, however, it should be 3 times the - The interval specifying how frequently the monitor instance
interval number of seconds plus 1 second. The default API setting of this template will run. By default, this interval is used for up and
is 16. down states. The default API setting is 5.
required: false required: false
default: none default: none
time_until_up: timeout:
description: description:
- Specifies the amount of time in seconds after the first successful - The number of seconds in which the node or service must respond to
response before a node will be marked up. A value of 0 will cause a the monitor request. If the target responds within the set time
node to be marked up immediately after a valid response is received period, it is considered up. If the target does not respond within
from the node. The default API setting is 0. the set time period, it is considered down. You can change this
required: false number to any number you want, however, it should be 3 times the
default: none interval number of seconds plus 1 second. The default API setting
is 16.
required: false
default: none
time_until_up:
description:
- Specifies the amount of time in seconds after the first successful
response before a node will be marked up. A value of 0 will cause a
node to be marked up immediately after a valid response is received
from the node. The default API setting is 0.
required: false
default: none
''' '''
EXAMPLES = ''' EXAMPLES = '''
- name: Create TCP Monitor
bigip_monitor_tcp:
state: "present"
server: "lb.mydomain.com"
user: "admin"
password: "secret"
name: "my_tcp_monitor"
type: "tcp"
send: "tcp string to send"
receive: "tcp string to receive"
delegate_to: localhost
- name: BIGIP F5 | Create TCP Monitor - name: Create TCP half open Monitor
local_action: bigip_monitor_tcp:
module: bigip_monitor_tcp state: "present"
state: present server: "lb.mydomain.com"
server: "{{ f5server }}" user: "admin"
user: "{{ f5user }}" password: "secret"
password: "{{ f5password }}" name: "my_tcp_monitor"
name: "{{ item.monitorname }}" type: "tcp"
type: tcp send: "tcp string to send"
send: "{{ item.send }}" receive: "http string to receive"
receive: "{{ item.receive }}" delegate_to: localhost
with_items: f5monitors-tcp
- name: BIGIP F5 | Create TCP half open Monitor
local_action:
module: bigip_monitor_tcp
state: present
server: "{{ f5server }}"
user: "{{ f5user }}"
password: "{{ f5password }}"
name: "{{ item.monitorname }}"
type: tcp
send: "{{ item.send }}"
receive: "{{ item.receive }}"
with_items: f5monitors-halftcp
- name: BIGIP F5 | Remove TCP Monitor
local_action:
module: bigip_monitor_tcp
state: absent
server: "{{ f5server }}"
user: "{{ f5user }}"
password: "{{ f5password }}"
name: "{{ monitorname }}"
with_flattened:
- f5monitors-tcp
- f5monitors-halftcp
- name: Remove TCP Monitor
bigip_monitor_tcp:
state: "absent"
server: "lb.mydomain.com"
user: "admin"
password: "secret"
name: "my_tcp_monitor"
''' '''
TEMPLATE_TYPE = DEFAULT_TEMPLATE_TYPE = 'TTYPE_TCP' TEMPLATE_TYPE = DEFAULT_TEMPLATE_TYPE = 'TTYPE_TCP'
@ -196,7 +201,6 @@ DEFAULT_PARENT = DEFAULT_TEMPLATE_TYPE_CHOICE = DEFAULT_TEMPLATE_TYPE.replace('T
def check_monitor_exists(module, api, monitor, parent): def check_monitor_exists(module, api, monitor, parent):
# hack to determine if monitor exists # hack to determine if monitor exists
result = False result = False
try: try:
@ -206,7 +210,7 @@ def check_monitor_exists(module, api, monitor, parent):
result = True result = True
else: else:
module.fail_json(msg='Monitor already exists, but has a different type (%s) or parent(%s)' % (ttype, parent)) module.fail_json(msg='Monitor already exists, but has a different type (%s) or parent(%s)' % (ttype, parent))
except bigsuds.OperationFailed, e: except bigsuds.OperationFailed as e:
if "was not found" in str(e): if "was not found" in str(e):
result = False result = False
else: else:
@ -216,10 +220,15 @@ def check_monitor_exists(module, api, monitor, parent):
def create_monitor(api, monitor, template_attributes): def create_monitor(api, monitor, template_attributes):
try: try:
api.LocalLB.Monitor.create_template(templates=[{'template_name': monitor, 'template_type': TEMPLATE_TYPE}], template_attributes=[template_attributes]) api.LocalLB.Monitor.create_template(
except bigsuds.OperationFailed, e: templates=[{
'template_name': monitor,
'template_type': TEMPLATE_TYPE
}],
template_attributes=[template_attributes]
)
except bigsuds.OperationFailed as e:
if "already exists" in str(e): if "already exists" in str(e):
return False return False
else: else:
@ -229,10 +238,9 @@ def create_monitor(api, monitor, template_attributes):
def delete_monitor(api, monitor): def delete_monitor(api, monitor):
try: try:
api.LocalLB.Monitor.delete_template(template_names=[monitor]) api.LocalLB.Monitor.delete_template(template_names=[monitor])
except bigsuds.OperationFailed, e: except bigsuds.OperationFailed as e:
# maybe it was deleted since we checked # maybe it was deleted since we checked
if "was not found" in str(e): if "was not found" in str(e):
return False return False
@ -243,41 +251,46 @@ def delete_monitor(api, monitor):
def check_string_property(api, monitor, str_property): def check_string_property(api, monitor, str_property):
try: try:
return str_property == api.LocalLB.Monitor.get_template_string_property([monitor], [str_property['type']])[0] template_prop = api.LocalLB.Monitor.get_template_string_property(
except bigsuds.OperationFailed, e: [monitor], [str_property['type']]
)[0]
return str_property == template_prop
except bigsuds.OperationFailed as e:
# happens in check mode if not created yet # happens in check mode if not created yet
if "was not found" in str(e): if "was not found" in str(e):
return True return True
else: else:
# genuine exception # genuine exception
raise raise
return True
def set_string_property(api, monitor, str_property): def set_string_property(api, monitor, str_property):
api.LocalLB.Monitor.set_template_string_property(
api.LocalLB.Monitor.set_template_string_property(template_names=[monitor], values=[str_property]) template_names=[monitor],
values=[str_property]
)
def check_integer_property(api, monitor, int_property): def check_integer_property(api, monitor, int_property):
try: try:
return int_property == api.LocalLB.Monitor.get_template_integer_property([monitor], [int_property['type']])[0] return int_property == api.LocalLB.Monitor.get_template_integer_property(
except bigsuds.OperationFailed, e: [monitor], [int_property['type']]
)[0]
except bigsuds.OperationFailed as e:
# happens in check mode if not created yet # happens in check mode if not created yet
if "was not found" in str(e): if "was not found" in str(e):
return True return True
else: else:
# genuine exception # genuine exception
raise raise
return True
def set_integer_property(api, monitor, int_property): def set_integer_property(api, monitor, int_property):
api.LocalLB.Monitor.set_template_integer_property(
api.LocalLB.Monitor.set_template_integer_property(template_names=[monitor], values=[int_property]) template_names=[monitor],
values=[int_property]
)
def update_monitor_properties(api, module, monitor, template_string_properties, template_integer_properties): def update_monitor_properties(api, module, monitor, template_string_properties, template_integer_properties):
@ -287,6 +300,7 @@ def update_monitor_properties(api, module, monitor, template_string_properties,
if not module.check_mode: if not module.check_mode:
set_string_property(api, monitor, str_property) set_string_property(api, monitor, str_property)
changed = True changed = True
for int_property in template_integer_properties: for int_property in template_integer_properties:
if int_property['value'] is not None and not check_integer_property(api, monitor, int_property): if int_property['value'] is not None and not check_integer_property(api, monitor, int_property):
if not module.check_mode: if not module.check_mode:
@ -297,56 +311,47 @@ def update_monitor_properties(api, module, monitor, template_string_properties,
def get_ipport(api, monitor): def get_ipport(api, monitor):
return api.LocalLB.Monitor.get_template_destination(template_names=[monitor])[0] return api.LocalLB.Monitor.get_template_destination(template_names=[monitor])[0]
def set_ipport(api, monitor, ipport): def set_ipport(api, monitor, ipport):
try: try:
api.LocalLB.Monitor.set_template_destination(template_names=[monitor], destinations=[ipport]) api.LocalLB.Monitor.set_template_destination(
template_names=[monitor], destinations=[ipport]
)
return True, "" return True, ""
except bigsuds.OperationFailed, e: except bigsuds.OperationFailed as e:
if "Cannot modify the address type of monitor" in str(e): if "Cannot modify the address type of monitor" in str(e):
return False, "Cannot modify the address type of monitor if already assigned to a pool." return False, "Cannot modify the address type of monitor if already assigned to a pool."
else: else:
# genuine exception # genuine exception
raise raise
# ===========================================
# main loop
#
# writing a module for other monitor types should
# only need an updated main() (and monitor specific functions)
def main(): def main():
argument_spec = f5_argument_spec()
# begin monitor specific stuff meta_args = dict(
argument_spec=f5_argument_spec() name=dict(required=True),
argument_spec.update(dict( type=dict(default=DEFAULT_TEMPLATE_TYPE_CHOICE, choices=TEMPLATE_TYPE_CHOICES),
name = dict(required=True), parent=dict(default=DEFAULT_PARENT),
type = dict(default=DEFAULT_TEMPLATE_TYPE_CHOICE, choices=TEMPLATE_TYPE_CHOICES), parent_partition=dict(default='Common'),
parent = dict(default=DEFAULT_PARENT), send=dict(required=False),
parent_partition = dict(default='Common'), receive=dict(required=False),
send = dict(required=False), ip=dict(required=False),
receive = dict(required=False), port=dict(required=False, type='int'),
ip = dict(required=False), interval=dict(required=False, type='int'),
port = dict(required=False, type='int'), timeout=dict(required=False, type='int'),
interval = dict(required=False, type='int'), time_until_up=dict(required=False, type='int', default=0)
timeout = dict(required=False, type='int'),
time_until_up = dict(required=False, type='int', default=0)
)
) )
argument_spec.update(meta_args)
module = AnsibleModule( module = AnsibleModule(
argument_spec = argument_spec, argument_spec=argument_spec,
supports_check_mode=True supports_check_mode=True
) )
if not bigsuds_found:
module.fail_json(msg="the python bigsuds module is required")
if module.params['validate_certs']: if module.params['validate_certs']:
import ssl import ssl
if not hasattr(ssl, 'SSLContext'): if not hasattr(ssl, 'SSLContext'):
@ -382,26 +387,27 @@ def main():
api = bigip_api(server, user, password, validate_certs, port=server_port) api = bigip_api(server, user, password, validate_certs, port=server_port)
monitor_exists = check_monitor_exists(module, api, monitor, parent) monitor_exists = check_monitor_exists(module, api, monitor, parent)
# ipport is a special setting # ipport is a special setting
if monitor_exists: # make sure to not update current settings if not asked if monitor_exists:
# make sure to not update current settings if not asked
cur_ipport = get_ipport(api, monitor) cur_ipport = get_ipport(api, monitor)
if ip is None: if ip is None:
ip = cur_ipport['ipport']['address'] ip = cur_ipport['ipport']['address']
if port is None: if port is None:
port = cur_ipport['ipport']['port'] port = cur_ipport['ipport']['port']
else: # use API defaults if not defined to create it else:
if interval is None: # use API defaults if not defined to create it
if interval is None:
interval = 5 interval = 5
if timeout is None: if timeout is None:
timeout = 16 timeout = 16
if ip is None: if ip is None:
ip = '0.0.0.0' ip = '0.0.0.0'
if port is None: if port is None:
port = 0 port = 0
if send is None: if send is None:
send = '' send = ''
if receive is None: if receive is None:
receive = '' receive = ''
# define and set address type # define and set address type
@ -414,76 +420,90 @@ def main():
else: else:
address_type = 'ATYPE_UNSET' address_type = 'ATYPE_UNSET'
ipport = {'address_type': address_type, ipport = {
'ipport': {'address': ip, 'address_type': address_type,
'port': port}} 'ipport': {
'address': ip,
'port': port
}
}
template_attributes = {'parent_template': parent, template_attributes = {
'interval': interval, 'parent_template': parent,
'timeout': timeout, 'interval': interval,
'dest_ipport': ipport, 'timeout': timeout,
'is_read_only': False, 'dest_ipport': ipport,
'is_directly_usable': True} 'is_read_only': False,
'is_directly_usable': True
}
# monitor specific stuff # monitor specific stuff
if type == 'TTYPE_TCP': if type == 'TTYPE_TCP':
template_string_properties = [{'type': 'STYPE_SEND', template_string_properties = [
'value': send}, {
{'type': 'STYPE_RECEIVE', 'type': 'STYPE_SEND',
'value': receive}] 'value': send
},
{
'type': 'STYPE_RECEIVE',
'value': receive
}
]
else: else:
template_string_properties = [] template_string_properties = []
template_integer_properties = [{'type': 'ITYPE_INTERVAL', template_integer_properties = [
'value': interval}, {
{'type': 'ITYPE_TIMEOUT', 'type': 'ITYPE_INTERVAL',
'value': timeout}, 'value': interval
{'type': 'ITYPE_TIME_UNTIL_UP', },
'value': interval}] {
'type': 'ITYPE_TIMEOUT',
'value': timeout
},
{
'type': 'ITYPE_TIME_UNTIL_UP',
'value': time_until_up
}
]
# main logic, monitor generic # main logic, monitor generic
try: try:
result = {'changed': False} # default result = {'changed': False} # default
if state == 'absent': if state == 'absent':
if monitor_exists: if monitor_exists:
if not module.check_mode: if not module.check_mode:
# possible race condition if same task # possible race condition if same task
# on other node deleted it first # on other node deleted it first
result['changed'] |= delete_monitor(api, monitor) result['changed'] |= delete_monitor(api, monitor)
else: else:
result['changed'] |= True result['changed'] |= True
else:
else: # state present # check for monitor itself
## check for monitor itself if not monitor_exists:
if not monitor_exists: # create it if not module.check_mode:
if not module.check_mode:
# again, check changed status here b/c race conditions # again, check changed status here b/c race conditions
# if other task already created it # if other task already created it
result['changed'] |= create_monitor(api, monitor, template_attributes) result['changed'] |= create_monitor(api, monitor, template_attributes)
else: else:
result['changed'] |= True result['changed'] |= True
## check for monitor parameters # check for monitor parameters
# whether it already existed, or was just created, now update # whether it already existed, or was just created, now update
# the update functions need to check for check mode but # the update functions need to check for check mode but
# cannot update settings if it doesn't exist which happens in check mode # cannot update settings if it doesn't exist which happens in check mode
if monitor_exists and not module.check_mode: result['changed'] |= update_monitor_properties(api, module, monitor,
result['changed'] |= update_monitor_properties(api, module, monitor, template_string_properties,
template_string_properties, template_integer_properties)
template_integer_properties)
# else assume nothing changed
# we just have to update the ipport if monitor already exists and it's different # we just have to update the ipport if monitor already exists and it's different
if monitor_exists and cur_ipport != ipport: if monitor_exists and cur_ipport != ipport:
set_ipport(api, monitor, ipport) set_ipport(api, monitor, ipport)
result['changed'] |= True result['changed'] |= True
#else: monitor doesn't exist (check mode) or ipport is already ok # else: monitor doesn't exist (check mode) or ipport is already ok
except Exception as e:
except Exception, e:
module.fail_json(msg="received exception: %s" % e) module.fail_json(msg="received exception: %s" % e)
module.exit_json(**result) module.exit_json(**result)