Merge pull request #574 from Etienne-Carriere/factor_f5

Factor common functions for F5 modules
This commit is contained in:
Brian Coca 2015-06-03 16:28:07 -04:00
commit 5c53271c38
5 changed files with 58 additions and 229 deletions

View file

@ -163,35 +163,10 @@ EXAMPLES = '''
name: "{{ monitorname }}" name: "{{ monitorname }}"
''' '''
try:
import bigsuds
except ImportError:
bigsuds_found = False
else:
bigsuds_found = True
TEMPLATE_TYPE = 'TTYPE_HTTP' TEMPLATE_TYPE = 'TTYPE_HTTP'
DEFAULT_PARENT_TYPE = 'http' DEFAULT_PARENT_TYPE = 'http'
# ===========================================
# bigip_monitor module generic methods.
# these should be re-useable for other monitor types
#
def bigip_api(bigip, user, password):
api = bigsuds.BIGIP(hostname=bigip, username=user, password=password)
return api
def disable_ssl_cert_validation():
# You probably only want to do this for testing and never in production.
# From https://www.python.org/dev/peps/pep-0476/#id29
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
def check_monitor_exists(module, api, monitor, parent): def check_monitor_exists(module, api, monitor, parent):
@ -278,7 +253,6 @@ def set_integer_property(api, monitor, 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):
changed = False changed = False
for str_property in template_string_properties: for str_property in template_string_properties:
if str_property['value'] is not None and not check_string_property(api, monitor, str_property): if str_property['value'] is not None and not check_string_property(api, monitor, str_property):
@ -321,15 +295,8 @@ def set_ipport(api, monitor, ipport):
def main(): def main():
# begin monitor specific stuff # begin monitor specific stuff
argument_spec=f5_argument_spec();
module = AnsibleModule( argument_spec.update( dict(
argument_spec = dict(
server = dict(required=True),
user = dict(required=True),
password = dict(required=True),
validate_certs = dict(default='yes', type='bool'),
partition = dict(default='Common'),
state = dict(default='present', choices=['present', 'absent']),
name = dict(required=True), name = dict(required=True),
parent = dict(default=DEFAULT_PARENT_TYPE), parent = dict(default=DEFAULT_PARENT_TYPE),
parent_partition = dict(default='Common'), parent_partition = dict(default='Common'),
@ -341,20 +308,20 @@ def main():
interval = dict(required=False, type='int'), interval = dict(required=False, type='int'),
timeout = dict(required=False, type='int'), timeout = dict(required=False, type='int'),
time_until_up = dict(required=False, type='int', default=0) time_until_up = dict(required=False, type='int', default=0)
), )
)
module = AnsibleModule(
argument_spec = argument_spec,
supports_check_mode=True supports_check_mode=True
) )
server = module.params['server'] (server,user,password,state,partition,validate_certs) = f5_parse_arguments(module)
user = module.params['user']
password = module.params['password']
validate_certs = module.params['validate_certs']
partition = module.params['partition']
parent_partition = module.params['parent_partition'] parent_partition = module.params['parent_partition']
state = module.params['state']
name = module.params['name'] name = module.params['name']
parent = "/%s/%s" % (parent_partition, module.params['parent']) parent = fq_name(parent_partition, module.params['parent'])
monitor = "/%s/%s" % (partition, name) monitor = fq_name(partition, name)
send = module.params['send'] send = module.params['send']
receive = module.params['receive'] receive = module.params['receive']
receive_disable = module.params['receive_disable'] receive_disable = module.params['receive_disable']
@ -366,11 +333,6 @@ def main():
# end monitor specific stuff # end monitor specific stuff
if not validate_certs:
disable_ssl_cert_validation()
if not bigsuds_found:
module.fail_json(msg="the python bigsuds module is required")
api = bigip_api(server, user, password) api = bigip_api(server, user, password)
monitor_exists = check_monitor_exists(module, api, monitor, parent) monitor_exists = check_monitor_exists(module, api, monitor, parent)
@ -481,5 +443,6 @@ def main():
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.f5 import *
main() main()

View file

@ -181,37 +181,11 @@ EXAMPLES = '''
''' '''
try:
import bigsuds
except ImportError:
bigsuds_found = False
else:
bigsuds_found = True
TEMPLATE_TYPE = DEFAULT_TEMPLATE_TYPE = 'TTYPE_TCP' TEMPLATE_TYPE = DEFAULT_TEMPLATE_TYPE = 'TTYPE_TCP'
TEMPLATE_TYPE_CHOICES = ['tcp', 'tcp_echo', 'tcp_half_open'] TEMPLATE_TYPE_CHOICES = ['tcp', 'tcp_echo', 'tcp_half_open']
DEFAULT_PARENT = DEFAULT_TEMPLATE_TYPE_CHOICE = DEFAULT_TEMPLATE_TYPE.replace('TTYPE_', '').lower() DEFAULT_PARENT = DEFAULT_TEMPLATE_TYPE_CHOICE = DEFAULT_TEMPLATE_TYPE.replace('TTYPE_', '').lower()
# ===========================================
# bigip_monitor module generic methods.
# these should be re-useable for other monitor types
#
def bigip_api(bigip, user, password):
api = bigsuds.BIGIP(hostname=bigip, username=user, password=password)
return api
def disable_ssl_cert_validation():
# You probably only want to do this for testing and never in production.
# From https://www.python.org/dev/peps/pep-0476/#id29
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
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
@ -298,7 +272,6 @@ def set_integer_property(api, monitor, 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):
changed = False changed = False
for str_property in template_string_properties: for str_property in template_string_properties:
if str_property['value'] is not None and not check_string_property(api, monitor, str_property): if str_property['value'] is not None and not check_string_property(api, monitor, str_property):
@ -341,15 +314,8 @@ def set_ipport(api, monitor, ipport):
def main(): def main():
# begin monitor specific stuff # begin monitor specific stuff
argument_spec=f5_argument_spec();
module = AnsibleModule( argument_spec.update(dict(
argument_spec = dict(
server = dict(required=True),
user = dict(required=True),
password = dict(required=True),
validate_certs = dict(default='yes', type='bool'),
partition = dict(default='Common'),
state = dict(default='present', choices=['present', 'absent']),
name = dict(required=True), name = dict(required=True),
type = dict(default=DEFAULT_TEMPLATE_TYPE_CHOICE, choices=TEMPLATE_TYPE_CHOICES), type = dict(default=DEFAULT_TEMPLATE_TYPE_CHOICE, choices=TEMPLATE_TYPE_CHOICES),
parent = dict(default=DEFAULT_PARENT), parent = dict(default=DEFAULT_PARENT),
@ -361,21 +327,21 @@ def main():
interval = dict(required=False, type='int'), interval = dict(required=False, type='int'),
timeout = dict(required=False, type='int'), timeout = dict(required=False, type='int'),
time_until_up = dict(required=False, type='int', default=0) time_until_up = dict(required=False, type='int', default=0)
), )
)
module = AnsibleModule(
argument_spec = argument_spec,
supports_check_mode=True supports_check_mode=True
) )
server = module.params['server'] (server,user,password,state,partition,validate_certs) = f5_parse_arguments(module)
user = module.params['user']
password = module.params['password']
validate_certs = module.params['validate_certs']
partition = module.params['partition']
parent_partition = module.params['parent_partition'] parent_partition = module.params['parent_partition']
state = module.params['state']
name = module.params['name'] name = module.params['name']
type = 'TTYPE_' + module.params['type'].upper() type = 'TTYPE_' + module.params['type'].upper()
parent = "/%s/%s" % (parent_partition, module.params['parent']) parent = fq_name(parent_partition, module.params['parent'])
monitor = "/%s/%s" % (partition, name) monitor = fq_name(partition, name)
send = module.params['send'] send = module.params['send']
receive = module.params['receive'] receive = module.params['receive']
ip = module.params['ip'] ip = module.params['ip']
@ -390,11 +356,6 @@ def main():
# end monitor specific stuff # end monitor specific stuff
if not validate_certs:
disable_ssl_cert_validation()
if not bigsuds_found:
module.fail_json(msg="the python bigsuds module is required")
api = bigip_api(server, user, password) api = bigip_api(server, user, password)
monitor_exists = check_monitor_exists(module, api, monitor, parent) monitor_exists = check_monitor_exists(module, api, monitor, parent)
@ -506,5 +467,6 @@ def main():
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.f5 import *
main() main()

View file

@ -188,27 +188,6 @@ EXAMPLES = '''
''' '''
try:
import bigsuds
except ImportError:
bigsuds_found = False
else:
bigsuds_found = True
# ==========================
# bigip_node module specific
#
def bigip_api(bigip, user, password):
api = bigsuds.BIGIP(hostname=bigip, username=user, password=password)
return api
def disable_ssl_cert_validation():
# You probably only want to do this for testing and never in production.
# From https://www.python.org/dev/peps/pep-0476/#id29
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
def node_exists(api, address): def node_exists(api, address):
# hack to determine if node exists # hack to determine if node exists
result = False result = False
@ -283,42 +262,30 @@ def get_node_monitor_status(api, name):
def main(): def main():
module = AnsibleModule( argument_spec=f5_argument_spec();
argument_spec = dict( argument_spec.update(dict(
server = dict(type='str', required=True),
user = dict(type='str', required=True),
password = dict(type='str', required=True),
validate_certs = dict(default='yes', type='bool'),
state = dict(type='str', default='present', choices=['present', 'absent']),
session_state = dict(type='str', choices=['enabled', 'disabled']), session_state = dict(type='str', choices=['enabled', 'disabled']),
monitor_state = dict(type='str', choices=['enabled', 'disabled']), monitor_state = dict(type='str', choices=['enabled', 'disabled']),
partition = dict(type='str', default='Common'),
name = dict(type='str', required=True), name = dict(type='str', required=True),
host = dict(type='str', aliases=['address', 'ip']), host = dict(type='str', aliases=['address', 'ip']),
description = dict(type='str') description = dict(type='str')
), )
)
module = AnsibleModule(
argument_spec = argument_spec,
supports_check_mode=True supports_check_mode=True
) )
if not bigsuds_found: (server,user,password,state,partition,validate_certs) = f5_parse_arguments(module)
module.fail_json(msg="the python bigsuds module is required")
server = module.params['server']
user = module.params['user']
password = module.params['password']
validate_certs = module.params['validate_certs']
state = module.params['state']
session_state = module.params['session_state'] session_state = module.params['session_state']
monitor_state = module.params['monitor_state'] monitor_state = module.params['monitor_state']
partition = module.params['partition']
host = module.params['host'] host = module.params['host']
name = module.params['name'] name = module.params['name']
address = "/%s/%s" % (partition, name) address = fq_name(partition, name)
description = module.params['description'] description = module.params['description']
if not validate_certs:
disable_ssl_cert_validation()
if state == 'absent' and host is not None: if state == 'absent' and host is not None:
module.fail_json(msg="host parameter invalid when state=absent") module.fail_json(msg="host parameter invalid when state=absent")
@ -410,5 +377,6 @@ def main():
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.f5 import *
main() main()

View file

@ -228,27 +228,6 @@ EXAMPLES = '''
''' '''
try:
import bigsuds
except ImportError:
bigsuds_found = False
else:
bigsuds_found = True
# ===========================================
# bigip_pool module specific support methods.
#
def bigip_api(bigip, user, password):
api = bigsuds.BIGIP(hostname=bigip, username=user, password=password)
return api
def disable_ssl_cert_validation():
# You probably only want to do this for testing and never in production.
# From https://www.python.org/dev/peps/pep-0476/#id29
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
def pool_exists(api, pool): def pool_exists(api, pool):
# hack to determine if pool exists # hack to determine if pool exists
result = False result = False
@ -368,15 +347,9 @@ def main():
service_down_choices = ['none', 'reset', 'drop', 'reselect'] service_down_choices = ['none', 'reset', 'drop', 'reselect']
module = AnsibleModule( argument_spec=f5_argument_spec();
argument_spec = dict( argument_spec.update(dict(
server = dict(type='str', required=True),
user = dict(type='str', required=True),
password = dict(type='str', required=True),
validate_certs = dict(default='yes', type='bool'),
state = dict(type='str', default='present', choices=['present', 'absent']),
name = dict(type='str', required=True, aliases=['pool']), name = dict(type='str', required=True, aliases=['pool']),
partition = dict(type='str', default='Common'),
lb_method = dict(type='str', choices=lb_method_choices), lb_method = dict(type='str', choices=lb_method_choices),
monitor_type = dict(type='str', choices=monitor_type_choices), monitor_type = dict(type='str', choices=monitor_type_choices),
quorum = dict(type='int'), quorum = dict(type='int'),
@ -385,21 +358,18 @@ def main():
service_down_action = dict(type='str', choices=service_down_choices), service_down_action = dict(type='str', choices=service_down_choices),
host = dict(type='str', aliases=['address']), host = dict(type='str', aliases=['address']),
port = dict(type='int') port = dict(type='int')
), )
)
module = AnsibleModule(
argument_spec = argument_spec,
supports_check_mode=True supports_check_mode=True
) )
if not bigsuds_found: (server,user,password,state,partition,validate_certs) = f5_parse_arguments(module)
module.fail_json(msg="the python bigsuds module is required")
server = module.params['server']
user = module.params['user']
password = module.params['password']
validate_certs = module.params['validate_certs']
state = module.params['state']
name = module.params['name'] name = module.params['name']
partition = module.params['partition'] pool = fq_name(partition,name)
pool = "/%s/%s" % (partition, name)
lb_method = module.params['lb_method'] lb_method = module.params['lb_method']
if lb_method: if lb_method:
lb_method = lb_method.lower() lb_method = lb_method.lower()
@ -411,16 +381,13 @@ def main():
if monitors: if monitors:
monitors = [] monitors = []
for monitor in module.params['monitors']: for monitor in module.params['monitors']:
if "/" not in monitor: monitors.append(fq_name(partition, monitor))
monitors.append("/%s/%s" % (partition, monitor))
else:
monitors.append(monitor)
slow_ramp_time = module.params['slow_ramp_time'] slow_ramp_time = module.params['slow_ramp_time']
service_down_action = module.params['service_down_action'] service_down_action = module.params['service_down_action']
if service_down_action: if service_down_action:
service_down_action = service_down_action.lower() service_down_action = service_down_action.lower()
host = module.params['host'] host = module.params['host']
address = "/%s/%s" % (partition, host) address = fq_name(partition,host)
port = module.params['port'] port = module.params['port']
if not validate_certs: if not validate_certs:
@ -551,5 +518,6 @@ def main():
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.f5 import *
main() main()

View file

@ -196,27 +196,6 @@ EXAMPLES = '''
''' '''
try:
import bigsuds
except ImportError:
bigsuds_found = False
else:
bigsuds_found = True
# ===========================================
# bigip_pool_member module specific support methods.
#
def bigip_api(bigip, user, password):
api = bigsuds.BIGIP(hostname=bigip, username=user, password=password)
return api
def disable_ssl_cert_validation():
# You probably only want to do this for testing and never in production.
# From https://www.python.org/dev/peps/pep-0476/#id29
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
def pool_exists(api, pool): def pool_exists(api, pool):
# hack to determine if pool exists # hack to determine if pool exists
result = False result = False
@ -327,49 +306,37 @@ def get_member_monitor_status(api, pool, address, port):
return result return result
def main(): def main():
module = AnsibleModule( argument_spec = f5_argument_spec();
argument_spec = dict( argument_spec.update(dict(
server = dict(type='str', required=True),
user = dict(type='str', required=True),
password = dict(type='str', required=True),
validate_certs = dict(default='yes', type='bool'),
state = dict(type='str', default='present', choices=['present', 'absent']),
session_state = dict(type='str', choices=['enabled', 'disabled']), session_state = dict(type='str', choices=['enabled', 'disabled']),
monitor_state = dict(type='str', choices=['enabled', 'disabled']), monitor_state = dict(type='str', choices=['enabled', 'disabled']),
pool = dict(type='str', required=True), pool = dict(type='str', required=True),
partition = dict(type='str', default='Common'),
host = dict(type='str', required=True, aliases=['address', 'name']), host = dict(type='str', required=True, aliases=['address', 'name']),
port = dict(type='int', required=True), port = dict(type='int', required=True),
connection_limit = dict(type='int'), connection_limit = dict(type='int'),
description = dict(type='str'), description = dict(type='str'),
rate_limit = dict(type='int'), rate_limit = dict(type='int'),
ratio = dict(type='int') ratio = dict(type='int')
), )
)
module = AnsibleModule(
argument_spec = argument_spec,
supports_check_mode=True supports_check_mode=True
) )
if not bigsuds_found: (server,user,password,state,partition,validate_certs) = f5_parse_arguments(module)
module.fail_json(msg="the python bigsuds module is required")
server = module.params['server']
user = module.params['user']
password = module.params['password']
validate_certs = module.params['validate_certs']
state = module.params['state']
session_state = module.params['session_state'] session_state = module.params['session_state']
monitor_state = module.params['monitor_state'] monitor_state = module.params['monitor_state']
partition = module.params['partition'] pool = fq_name(partition, module.params['pool'])
pool = "/%s/%s" % (partition, module.params['pool'])
connection_limit = module.params['connection_limit'] connection_limit = module.params['connection_limit']
description = module.params['description'] description = module.params['description']
rate_limit = module.params['rate_limit'] rate_limit = module.params['rate_limit']
ratio = module.params['ratio'] ratio = module.params['ratio']
host = module.params['host'] host = module.params['host']
address = "/%s/%s" % (partition, host) address = fq_name(partition, host)
port = module.params['port'] port = module.params['port']
if not validate_certs:
disable_ssl_cert_validation()
# sanity check user supplied values # sanity check user supplied values
@ -457,5 +424,6 @@ def main():
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.f5 import *
main() main()