From 9139ac54607be1ba568102854aa241be85cce6c8 Mon Sep 17 00:00:00 2001 From: Etienne CARRIERE Date: Sun, 10 May 2015 01:32:16 +0200 Subject: [PATCH 1/9] Creation of bigip_virtual_server module --- network/f5/bigip_virtual_server.py | 453 +++++++++++++++++++++++++++++ 1 file changed, 453 insertions(+) create mode 100644 network/f5/bigip_virtual_server.py diff --git a/network/f5/bigip_virtual_server.py b/network/f5/bigip_virtual_server.py new file mode 100644 index 00000000000..30b2c7008bb --- /dev/null +++ b/network/f5/bigip_virtual_server.py @@ -0,0 +1,453 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# (c) 2015, Etienne Carriere +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +DOCUMENTATION = ''' +--- +module: bigip_virtual_server +short_description: "Manages F5 BIG-IP LTM virtual servers" +description: + - "Manages F5 BIG-IP LTM virtual servers via iControl SOAP API" +version_added: "2.0" +author: Etienne Carriere +notes: + - "Requires BIG-IP software version >= 11" + - "F5 developed module 'bigsuds' required (see http://devcentral.f5.com)" + - "Best run as a local_action in your playbook" +requirements: + - bigsuds +options: + server: + description: + - BIG-IP host + required: true + default: null + choices: [] + aliases: [] + user: + description: + - BIG-IP username + required: true + default: null + choices: [] + aliases: [] + password: + description: + - BIG-IP password + required: true + default: null + choices: [] + aliases: [] + validate_certs: + description: + - If C(no), SSL certificates will not be validated. This should only be used + on personally controlled sites using self-signed certificates. + required: false + default: 'yes' + choices: ['yes', 'no'] + version_added: 2.0 + state: + description: + - Pool member state + required: true + default: present + choices: ['present', 'absent', 'enabled', 'disabled'] + aliases: [] + partition: + description: + - Partition + required: false + default: 'Common' + choices: [] + aliases: [] + name: + description: + - "Virtual server name." + required: true + default: null + choices: [] + aliases: ['vs'] + destination: + description: + - "Destination IP of the virtual server (only host is currently supported) . Required when state=present and vs does not exist. Error when state=absent." + required: true + default: null + choices: [] + aliases: ['address', 'ip'] + port: + description: + - "Port of the virtual server . Required when state=present and vs does not exist" + required: true + default: null + choices: [] + aliases: [] + all_profiles: + description: + - "List of all Profiles (HTTP,ClientSSL,ServerSSL,etc) that must be used by the virtual server" + required: false + default: null + choices: [] + aliases: [] + pool: + description: + - "Default pool for the virtual server" + required: false + default: null + choices: [] + aliases: [] + snat: + description: + - "Source network address policy" + required: false + default: None + choices: [] + aliases: [] + + description: + description: + - "Virtual server description." + required: false + default: null + choices: [] +''' + +EXAMPLES = ''' + +## playbook task examples: + +--- +# file bigip-test.yml +# ... + - name: Add VS + local_action: + module: bigip_virtual_server + server: lb.mydomain.net + user: admin + password: secret + state: present + partition: MyPartition + name: myvirtualserver + destination: "{{ ansible_default_ipv4["address"] }}" + port: 443 + pool: "{{ mypool }}" + snat: Automap + description: Test Virtual Server + all_profiles: + - http + - clientssl + + - name: Modify Port of the Virtual Server + local_action: + module: bigip_virtual_server + server: lb.mydomain.net + user: admin + password: secret + state: present + partition: MyPartition + name: myvirtualserver + port: 8080 + + - name: Delete pool + local_action: + module: bigip_virtual_server + server: lb.mydomain.net + user: admin + password: secret + state: absent + partition: MyPartition + name: myvirtualserver +''' + +try: + import bigsuds +except ImportError: + bigsuds_found = False +else: + bigsuds_found = True + +# ========================== +# bigip_node module specific +# + +# map of state values +STATES={'enabled': 'STATE_ENABLED', + 'disabled': 'STATE_DISABLED'} +STATUSES={'enabled': 'SESSION_STATUS_ENABLED', + 'disabled': 'SESSION_STATUS_DISABLED', + 'offline': 'SESSION_STATUS_FORCED_DISABLED'} + +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 fq_name(partition,name): + if name is None: + return None + if name[0] is '/': + return name + else: + return '/%s/%s' % (partition,name) + +def fq_list_names(partition,list_names): + if list_names is None: + return None + return map(lambda x: fq_name(partition,x),list_names) + + +def vs_exists(api, vs): + # hack to determine if pool exists + result = False + try: + api.LocalLB.VirtualServer.get_object_status(virtual_servers=[vs]) + result = True + except bigsuds.OperationFailed, e: + if "was not found" in str(e): + result = False + else: + # genuine exception + raise + return result + +def vs_create(api,name,destination,port,pool): + _profiles=[[{'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': 'tcp'}]] + try: + api.LocalLB.VirtualServer.create( + definitions = [{'name': [name], 'address': [destination], 'port': port, 'protocol': 'PROTOCOL_TCP'}], + wildmasks = ['255.255.255.255'], + resources = [{'type': 'RESOURCE_TYPE_POOL', 'default_pool_name': pool}], + profiles = _profiles) + result = True + desc = 0 + except Exception, e : + print e.args + +def vs_remove(api,name): + api.LocalLB.VirtualServer.delete_virtual_server(virtual_servers = [name ]) + +def get_profiles(api,name): + return api.LocalLB.VirtualServer.get_profile(virtual_servers = [name])[0] + + + +def set_profiles(api,name,profiles_list): + if profiles_list is None: + return False + current_profiles=map(lambda x:x['profile_name'], get_profiles(api,name)) + to_add_profiles=[] + for x in profiles_list: + if x not in current_profiles: + to_add_profiles.append({'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': x}) + to_del_profiles=[] + for x in current_profiles: + if (x not in profiles_list) and (x!= "/Common/tcp"): + to_del_profiles.append({'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': x}) + changed=False + if len(to_del_profiles)>0: + api.LocalLB.VirtualServer.remove_profile(virtual_servers = [name],profiles = [to_del_profiles]) + changed=True + if len(to_add_profiles)>0: + api.LocalLB.VirtualServer.add_profile(virtual_servers = [name],profiles= [to_add_profiles]) + changed=True + return changed + + +def set_snat(api,name,snat): + current_state=get_snat_type(api,name) + update = False + if snat is None: + return update + if snat == 'None' and current_state != 'SRC_TRANS_NONE': + api.LocalLB.VirtualServer.set_source_address_translation_none(virtual_servers = [name]) + update = True + if snat == 'Automap' and current_state != 'SRC_TRANS_AUTOMAP': + api.LocalLB.VirtualServer.set_source_address_translation_automap(virtual_servers = [name]) + update = True + return update + +def get_snat_type(api,name): + return api.LocalLB.VirtualServer.get_source_address_translation_type(virtual_servers = [name])[0] + + +def get_pool(api,name): + return api.LocalLB.VirtualServer.get_default_pool_name(virtual_servers = [name])[0] + +def set_pool(api,name,pool): + current_pool = get_pool (api,name) + updated=False + if pool is not None and (pool != current_pool): + api.LocalLB.VirtualServer.set_default_pool_name(virtual_servers = [name],default_pools = [pool]) + updated=True + return updated + + + +def get_destination(api,name): + return api.LocalLB.VirtualServer.get_destination_v2(virtual_servers = [name])[0] + +def set_destination(api,name,destination,port): + current_destination = get_destination(api,name) + updated=False + if (destination is not None and port is not None) and (destination != current_destination['address'] or port != current_destination['port']): + api.LocalLB.VirtualServer.set_destination_v2(virtual_servers = [name],destinations=[{'address': destination, 'port':port}]) + updated=True + return updated + + +def get_description(api,name): + return api.LocalLB.VirtualServer.get_description(virtual_servers = [name])[0] + +def set_description(api,name,description): + current_description = get_description(api,name) + updated=False + if description is not None and current_description != description: + api.LocalLB.VirtualServer.set_description(virtual_servers =[name],descriptions=[description]) + updated=True + return updated + + +def main(): + module = AnsibleModule( + argument_spec = 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', 'disabled', 'enabled']), + partition = dict(type='str', default='Common'), + name = dict(type='str', required=True,aliases=['vs']), + destination = dict(type='str', aliases=['address', 'ip']), + port = dict(type='int'), + all_profiles = dict(type='list'), + pool=dict(type='str'), + description = dict(type='str'), + snat=dict(type='str') + ), + supports_check_mode=True + ) + + if not bigsuds_found: + 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'] + partition = module.params['partition'] + name = fq_name(partition,module.params['name']) + destination=module.params['destination'] + port=module.params['port'] + all_profiles=fq_list_names(partition,module.params['all_profiles']) + pool=fq_name(partition,module.params['pool']) + description = module.params['description'] + snat = module.params['snat'] + if not validate_certs: + disable_ssl_cert_validation() + + if 1 > port > 65535: + module.fail_json(msg="valid ports must be in range 1 - 65535") + + try: + api = bigip_api(server, user, password) + result = {'changed': False} # default + + if state == 'absent': + if not module.check_mode: + if vs_exists(api,name): + # hack to handle concurrent runs of module + # pool might be gone before we actually remove + try: + vs_remove(api,name) + result = {'changed' : True, 'deleted' : name } + except bigsuds.OperationFailed, e: + if "was not found" in str(e): + result['changed']= False + else: + raise + else: + # check-mode return value + result = {'changed': True} + + elif state == 'present': + update = False + if not vs_exists(api, name): + if (not destination) or (not port): + module.fail_json(msg="both destination and port must be supplied to create a VS") + if not module.check_mode: + # a bit of a hack to handle concurrent runs of this module. + # even though we've checked the pool doesn't exist, + # it may exist by the time we run create_pool(). + # this catches the exception and does something smart + # about it! + try: + vs_create(api,name,destination,port,pool) + result = {'changed': True} + except bigsuds.OperationFailed, e: + if "already exists" in str(e): + update = True + else: + raise + else: + set_profiles(api,name,all_profiles) + set_snat(api,name,snat) + set_description(api,name,description) + else: + # check-mode return value + result = {'changed': True} + else: + update = True + if update: + # VS exists + if not module.check_mode: + # Have a transaction for all the changes + api.System.Session.start_transaction() + result['changed']|=set_destination(api,name,fq_name(partition,destination),port) + result['changed']|=set_pool(api,name,pool) + result['changed']|=set_description(api,name,description) + result['changed']|=set_snat(api,name,snat) + result['changed']|=set_profiles(api,name,all_profiles) + api.System.Session.submit_transaction() + else: + # check-mode return value + result = {'changed': True} + + elif state in ('disabled', 'enabled'): + if name is None: + module.fail_json(msg="name parameter required when " \ + "state=enabled/disabled") + if not module.check_mode: + pass + else: + # check-mode return value + result = {'changed': True} + + except Exception, e: + module.fail_json(msg="received exception: %s" % e) + + module.exit_json(**result) +# import module snippets +from ansible.module_utils.basic import * +main() + From 24ae114a2aef7b86fe9db5094091429a01831f00 Mon Sep 17 00:00:00 2001 From: Etienne CARRIERE Date: Thu, 4 Jun 2015 08:30:32 +0200 Subject: [PATCH 2/9] Factor F5 virtual_server module with the common functions --- network/f5/bigip_virtual_server.py | 59 +++++------------------------- 1 file changed, 10 insertions(+), 49 deletions(-) diff --git a/network/f5/bigip_virtual_server.py b/network/f5/bigip_virtual_server.py index 30b2c7008bb..dda9e51dbad 100644 --- a/network/f5/bigip_virtual_server.py +++ b/network/f5/bigip_virtual_server.py @@ -174,15 +174,9 @@ EXAMPLES = ''' name: myvirtualserver ''' -try: - import bigsuds -except ImportError: - bigsuds_found = False -else: - bigsuds_found = True # ========================== -# bigip_node module specific +# bigip_virtual_server module specific # # map of state values @@ -192,30 +186,6 @@ STATUSES={'enabled': 'SESSION_STATUS_ENABLED', 'disabled': 'SESSION_STATUS_DISABLED', 'offline': 'SESSION_STATUS_FORCED_DISABLED'} -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 fq_name(partition,name): - if name is None: - return None - if name[0] is '/': - return name - else: - return '/%s/%s' % (partition,name) - -def fq_list_names(partition,list_names): - if list_names is None: - return None - return map(lambda x: fq_name(partition,x),list_names) - - def vs_exists(api, vs): # hack to determine if pool exists result = False @@ -328,15 +298,10 @@ def set_description(api,name,description): def main(): - module = AnsibleModule( - argument_spec = 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'), + argument_spec = f5_argument_spec() + argument_spec.update( dict( state = dict(type='str', default='present', choices=['present', 'absent', 'disabled', 'enabled']), - partition = dict(type='str', default='Common'), name = dict(type='str', required=True,aliases=['vs']), destination = dict(type='str', aliases=['address', 'ip']), port = dict(type='int'), @@ -344,18 +309,15 @@ def main(): pool=dict(type='str'), description = dict(type='str'), snat=dict(type='str') - ), + ) + ) + + module = AnsibleModule( + argument_spec = argument_spec, supports_check_mode=True ) - if not bigsuds_found: - 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'] - partition = module.params['partition'] + (server,user,password,state,partition,validate_certs) = f5_parse_arguments(module) name = fq_name(partition,module.params['name']) destination=module.params['destination'] port=module.params['port'] @@ -363,8 +325,6 @@ def main(): pool=fq_name(partition,module.params['pool']) description = module.params['description'] snat = module.params['snat'] - if not validate_certs: - disable_ssl_cert_validation() if 1 > port > 65535: module.fail_json(msg="valid ports must be in range 1 - 65535") @@ -449,5 +409,6 @@ def main(): module.exit_json(**result) # import module snippets from ansible.module_utils.basic import * +from ansible.module_utils.f5 import * main() From ca366059d33d3986e34f499db634c1497b7fd5f6 Mon Sep 17 00:00:00 2001 From: Etienne CARRIERE Date: Fri, 5 Jun 2015 20:43:38 +0200 Subject: [PATCH 3/9] Documentation fixes --- network/f5/bigip_virtual_server.py | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/network/f5/bigip_virtual_server.py b/network/f5/bigip_virtual_server.py index dda9e51dbad..e6402833cb5 100644 --- a/network/f5/bigip_virtual_server.py +++ b/network/f5/bigip_virtual_server.py @@ -37,23 +37,14 @@ options: description: - BIG-IP host required: true - default: null - choices: [] - aliases: [] user: description: - BIG-IP username required: true - default: null - choices: [] - aliases: [] password: description: - BIG-IP password required: true - default: null - choices: [] - aliases: [] validate_certs: description: - If C(no), SSL certificates will not be validated. This should only be used @@ -61,11 +52,10 @@ options: required: false default: 'yes' choices: ['yes', 'no'] - version_added: 2.0 state: description: - Pool member state - required: true + required: false default: present choices: ['present', 'absent', 'enabled', 'disabled'] aliases: [] @@ -74,57 +64,41 @@ options: - Partition required: false default: 'Common' - choices: [] - aliases: [] name: description: - "Virtual server name." required: true - default: null - choices: [] aliases: ['vs'] destination: description: - "Destination IP of the virtual server (only host is currently supported) . Required when state=present and vs does not exist. Error when state=absent." required: true - default: null - choices: [] aliases: ['address', 'ip'] port: description: - "Port of the virtual server . Required when state=present and vs does not exist" required: true - default: null - choices: [] - aliases: [] all_profiles: description: - "List of all Profiles (HTTP,ClientSSL,ServerSSL,etc) that must be used by the virtual server" required: false default: null - choices: [] - aliases: [] pool: description: - "Default pool for the virtual server" required: false default: null - choices: [] - aliases: [] snat: description: - "Source network address policy" required: false default: None - choices: [] - aliases: [] description: description: - "Virtual server description." required: false default: null - choices: [] ''' EXAMPLES = ''' From 3d9fd24d62b4513495dc2606022691683473332b Mon Sep 17 00:00:00 2001 From: Etienne CARRIERE Date: Wed, 1 Jul 2015 22:26:32 +0200 Subject: [PATCH 4/9] Localize exceptions for F5 LTM virtual server module --- network/f5/bigip_virtual_server.py | 127 ++++++++++++++++------------- 1 file changed, 72 insertions(+), 55 deletions(-) diff --git a/network/f5/bigip_virtual_server.py b/network/f5/bigip_virtual_server.py index e6402833cb5..09e9bf0beb5 100644 --- a/network/f5/bigip_virtual_server.py +++ b/network/f5/bigip_virtual_server.py @@ -196,39 +196,44 @@ def get_profiles(api,name): def set_profiles(api,name,profiles_list): - if profiles_list is None: - return False - current_profiles=map(lambda x:x['profile_name'], get_profiles(api,name)) - to_add_profiles=[] - for x in profiles_list: - if x not in current_profiles: - to_add_profiles.append({'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': x}) - to_del_profiles=[] - for x in current_profiles: - if (x not in profiles_list) and (x!= "/Common/tcp"): - to_del_profiles.append({'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': x}) - changed=False - if len(to_del_profiles)>0: - api.LocalLB.VirtualServer.remove_profile(virtual_servers = [name],profiles = [to_del_profiles]) - changed=True - if len(to_add_profiles)>0: - api.LocalLB.VirtualServer.add_profile(virtual_servers = [name],profiles= [to_add_profiles]) - changed=True - return changed - + updated=False + try: + if profiles_list is None: + return False + current_profiles=map(lambda x:x['profile_name'], get_profiles(api,name)) + to_add_profiles=[] + for x in profiles_list: + if x not in current_profiles: + to_add_profiles.append({'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': x}) + to_del_profiles=[] + for x in current_profiles: + if (x not in profiles_list) and (x!= "/Common/tcp"): + to_del_profiles.append({'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': x}) + if len(to_del_profiles)>0: + api.LocalLB.VirtualServer.remove_profile(virtual_servers = [name],profiles = [to_del_profiles]) + updated=True + if len(to_add_profiles)>0: + api.LocalLB.VirtualServer.add_profile(virtual_servers = [name],profiles= [to_add_profiles]) + updated=True + return updated + except bigsuds.OperationFailed, e: + raise Exception('Error on setting profiles : %s' % e) def set_snat(api,name,snat): - current_state=get_snat_type(api,name) - update = False - if snat is None: - return update - if snat == 'None' and current_state != 'SRC_TRANS_NONE': - api.LocalLB.VirtualServer.set_source_address_translation_none(virtual_servers = [name]) - update = True - if snat == 'Automap' and current_state != 'SRC_TRANS_AUTOMAP': - api.LocalLB.VirtualServer.set_source_address_translation_automap(virtual_servers = [name]) - update = True - return update + updated = False + try: + current_state=get_snat_type(api,name) + if snat is None: + return update + if snat == 'None' and current_state != 'SRC_TRANS_NONE': + api.LocalLB.VirtualServer.set_source_address_translation_none(virtual_servers = [name]) + updated = True + if snat == 'Automap' and current_state != 'SRC_TRANS_AUTOMAP': + api.LocalLB.VirtualServer.set_source_address_translation_automap(virtual_servers = [name]) + updated = True + return updated + except bigsuds.OperationFailed, e: + raise Exception('Error on setting snat : %s' % e) def get_snat_type(api,name): return api.LocalLB.VirtualServer.get_source_address_translation_type(virtual_servers = [name])[0] @@ -238,37 +243,46 @@ def get_pool(api,name): return api.LocalLB.VirtualServer.get_default_pool_name(virtual_servers = [name])[0] def set_pool(api,name,pool): - current_pool = get_pool (api,name) updated=False - if pool is not None and (pool != current_pool): - api.LocalLB.VirtualServer.set_default_pool_name(virtual_servers = [name],default_pools = [pool]) - updated=True - return updated - + try: + current_pool = get_pool (api,name) + if pool is not None and (pool != current_pool): + api.LocalLB.VirtualServer.set_default_pool_name(virtual_servers = [name],default_pools = [pool]) + updated=True + return updated + except bigsuds.OperationFailed, e: + raise Exception('Error on setting pool : %s' % e) def get_destination(api,name): return api.LocalLB.VirtualServer.get_destination_v2(virtual_servers = [name])[0] def set_destination(api,name,destination,port): - current_destination = get_destination(api,name) updated=False - if (destination is not None and port is not None) and (destination != current_destination['address'] or port != current_destination['port']): - api.LocalLB.VirtualServer.set_destination_v2(virtual_servers = [name],destinations=[{'address': destination, 'port':port}]) - updated=True - return updated + try: + current_destination = get_destination(api,name) + if (destination is not None and port is not None) and (destination != current_destination['address'] or port != current_destination['port']): + api.LocalLB.VirtualServer.set_destination_v2(virtual_servers = [name],destinations=[{'address': destination, 'port':port}]) + updated=True + return updated + except bigsuds.OperationFailed, e: + raise Exception('Error on setting destination : %s'% e ) + def get_description(api,name): return api.LocalLB.VirtualServer.get_description(virtual_servers = [name])[0] def set_description(api,name,description): - current_description = get_description(api,name) updated=False - if description is not None and current_description != description: - api.LocalLB.VirtualServer.set_description(virtual_servers =[name],descriptions=[description]) - updated=True - return updated + try: + current_description = get_description(api,name) + if description is not None and current_description != description: + api.LocalLB.VirtualServer.set_description(virtual_servers =[name],descriptions=[description]) + updated=True + return updated + except bigsuds.OperationFailed, e: + raise Exception('Error on setting description : %s ' % e) def main(): @@ -342,7 +356,7 @@ def main(): if "already exists" in str(e): update = True else: - raise + raise Exception('Error on creating Virtual Server : %s' % e) else: set_profiles(api,name,all_profiles) set_snat(api,name,snat) @@ -356,13 +370,16 @@ def main(): # VS exists if not module.check_mode: # Have a transaction for all the changes - api.System.Session.start_transaction() - result['changed']|=set_destination(api,name,fq_name(partition,destination),port) - result['changed']|=set_pool(api,name,pool) - result['changed']|=set_description(api,name,description) - result['changed']|=set_snat(api,name,snat) - result['changed']|=set_profiles(api,name,all_profiles) - api.System.Session.submit_transaction() + try: + api.System.Session.start_transaction() + result['changed']|=set_destination(api,name,fq_name(partition,destination),port) + result['changed']|=set_pool(api,name,pool) + result['changed']|=set_description(api,name,description) + result['changed']|=set_snat(api,name,snat) + result['changed']|=set_profiles(api,name,all_profiles) + api.System.Session.submit_transaction() + except Exception,e: + raise Exception("Error on updating Virtual Server : %s" % e) else: # check-mode return value result = {'changed': True} From 757fc291c1a3a793e9d4acdfe90e27c87ac7c53e Mon Sep 17 00:00:00 2001 From: Etienne CARRIERE Date: Thu, 2 Jul 2015 07:35:19 +0200 Subject: [PATCH 5/9] Rework on Exception management --- network/f5/bigip_virtual_server.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/network/f5/bigip_virtual_server.py b/network/f5/bigip_virtual_server.py index 09e9bf0beb5..84cfccf2958 100644 --- a/network/f5/bigip_virtual_server.py +++ b/network/f5/bigip_virtual_server.py @@ -176,16 +176,23 @@ def vs_exists(api, vs): def vs_create(api,name,destination,port,pool): _profiles=[[{'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': 'tcp'}]] + created = False + # a bit of a hack to handle concurrent runs of this module. + # even though we've checked the vs doesn't exist, + # it may exist by the time we run create_vs(). + # this catches the exception and does something smart + # about it! try: api.LocalLB.VirtualServer.create( definitions = [{'name': [name], 'address': [destination], 'port': port, 'protocol': 'PROTOCOL_TCP'}], wildmasks = ['255.255.255.255'], resources = [{'type': 'RESOURCE_TYPE_POOL', 'default_pool_name': pool}], profiles = _profiles) - result = True - desc = 0 - except Exception, e : - print e.args + created = True + return created + except bigsudsOperationFailed, e : + if "already exists" not in str(e): + raise Exception('Error on creating Virtual Server : %s' % e) def vs_remove(api,name): api.LocalLB.VirtualServer.delete_virtual_server(virtual_servers = [name ]) @@ -351,16 +358,12 @@ def main(): # about it! try: vs_create(api,name,destination,port,pool) - result = {'changed': True} - except bigsuds.OperationFailed, e: - if "already exists" in str(e): - update = True - else: - raise Exception('Error on creating Virtual Server : %s' % e) - else: set_profiles(api,name,all_profiles) set_snat(api,name,snat) set_description(api,name,description) + result = {'changed': True} + except bigsuds.OperationFailed, e: + raise Exception('Error on creating Virtual Server : %s' % e) else: # check-mode return value result = {'changed': True} From fc9078229c27f0bccff32c7cee427a1a2b62ca0f Mon Sep 17 00:00:00 2001 From: Etienne CARRIERE Date: Thu, 2 Jul 2015 08:20:29 +0200 Subject: [PATCH 6/9] Add "Default Persistence profile" support --- network/f5/bigip_virtual_server.py | 34 ++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/network/f5/bigip_virtual_server.py b/network/f5/bigip_virtual_server.py index 84cfccf2958..0c47eb20943 100644 --- a/network/f5/bigip_virtual_server.py +++ b/network/f5/bigip_virtual_server.py @@ -93,7 +93,11 @@ options: - "Source network address policy" required: false default: None - + default_persistence_profile: + description: + - "Default Profile which manages the session persistence" + required: false + default: None description: description: - "Virtual server description." @@ -291,6 +295,28 @@ def set_description(api,name,description): except bigsuds.OperationFailed, e: raise Exception('Error on setting description : %s ' % e) +def get_persistence_profiles(api,name): + return api.LocalLB.VirtualServer.get_persistence_profile(virtual_servers = [name])[0] + +def set_default_persistence_profiles(api,name,persistence_profile): + updated=False + if persistence_profile is None: + return updated + try: + current_persistence_profiles = get_persistence_profiles(api,name) + default=None + for profile in current_persistence_profiles: + if profile['default_profile']: + default=profile['profile_name'] + break + if default is not None and default != persistence_profile: + api.LocalLB.VirtualServer.remove_persistence_profile(virtual_servers=[name],profiles=[[{'profile_name':default,'default_profile' : True}]]) + if default != persistence_profile: + api.LocalLB.VirtualServer.add_persistence_profile(virtual_servers=[name],profiles=[[{'profile_name':persistence_profile,'default_profile' : True}]]) + updated=True + return updated + except bigsuds.OperationFailed, e: + raise Exception('Error on setting default persistence profile : %s' % e) def main(): argument_spec = f5_argument_spec() @@ -303,7 +329,8 @@ def main(): all_profiles = dict(type='list'), pool=dict(type='str'), description = dict(type='str'), - snat=dict(type='str') + snat=dict(type='str'), + default_persistence_profile=dict(type='str') ) ) @@ -320,6 +347,7 @@ def main(): pool=fq_name(partition,module.params['pool']) description = module.params['description'] snat = module.params['snat'] + default_persistence_profile=fq_name(partition,module.params['default_persistence_profile']) if 1 > port > 65535: module.fail_json(msg="valid ports must be in range 1 - 65535") @@ -361,6 +389,7 @@ def main(): set_profiles(api,name,all_profiles) set_snat(api,name,snat) set_description(api,name,description) + set_default_persistence_profiles(api,name,default_persistence_profile) result = {'changed': True} except bigsuds.OperationFailed, e: raise Exception('Error on creating Virtual Server : %s' % e) @@ -380,6 +409,7 @@ def main(): result['changed']|=set_description(api,name,description) result['changed']|=set_snat(api,name,snat) result['changed']|=set_profiles(api,name,all_profiles) + result['changed']|=set_default_persistence_profiles(api,name,default_persistence_profile) api.System.Session.submit_transaction() except Exception,e: raise Exception("Error on updating Virtual Server : %s" % e) From 6cdfe07518af82d75fdbbbb36bade26349dd7b6e Mon Sep 17 00:00:00 2001 From: Etienne CARRIERE Date: Thu, 1 Oct 2015 20:59:28 +0200 Subject: [PATCH 7/9] Take review comments in accounts (pull request :582) --- network/f5/bigip_virtual_server.py | 44 ++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/network/f5/bigip_virtual_server.py b/network/f5/bigip_virtual_server.py index 0c47eb20943..2387a40a79b 100644 --- a/network/f5/bigip_virtual_server.py +++ b/network/f5/bigip_virtual_server.py @@ -25,7 +25,7 @@ short_description: "Manages F5 BIG-IP LTM virtual servers" description: - "Manages F5 BIG-IP LTM virtual servers via iControl SOAP API" version_added: "2.0" -author: Etienne Carriere +author: Etienne Carriere (@Etienne-Carriere) notes: - "Requires BIG-IP software version >= 11" - "F5 developed module 'bigsuds' required (see http://devcentral.f5.com)" @@ -71,23 +71,24 @@ options: aliases: ['vs'] destination: description: - - "Destination IP of the virtual server (only host is currently supported) . Required when state=present and vs does not exist. Error when state=absent." + - "Destination IP of the virtual server (only host is currently supported) . Required when state=present and vs does not exist." required: true aliases: ['address', 'ip'] port: description: - "Port of the virtual server . Required when state=present and vs does not exist" - required: true + required: false + default: None all_profiles: description: - "List of all Profiles (HTTP,ClientSSL,ServerSSL,etc) that must be used by the virtual server" required: false - default: null + default: None pool: description: - "Default pool for the virtual server" required: false - default: null + default: None snat: description: - "Source network address policy" @@ -102,7 +103,7 @@ options: description: - "Virtual server description." required: false - default: null + default: None ''' EXAMPLES = ''' @@ -121,7 +122,7 @@ EXAMPLES = ''' state: present partition: MyPartition name: myvirtualserver - destination: "{{ ansible_default_ipv4["address"] }}" + destination: "{{ ansible_default_ipv4['address'] }}" port: 443 pool: "{{ mypool }}" snat: Automap @@ -194,7 +195,7 @@ def vs_create(api,name,destination,port,pool): profiles = _profiles) created = True return created - except bigsudsOperationFailed, e : + except bigsuds.OperationFailed, e: if "already exists" not in str(e): raise Exception('Error on creating Virtual Server : %s' % e) @@ -235,7 +236,7 @@ def set_snat(api,name,snat): try: current_state=get_snat_type(api,name) if snat is None: - return update + return updated if snat == 'None' and current_state != 'SRC_TRANS_NONE': api.LocalLB.VirtualServer.set_source_address_translation_none(virtual_servers = [name]) updated = True @@ -268,18 +269,30 @@ def set_pool(api,name,pool): def get_destination(api,name): return api.LocalLB.VirtualServer.get_destination_v2(virtual_servers = [name])[0] -def set_destination(api,name,destination,port): +def set_destination(api,name,destination): updated=False try: current_destination = get_destination(api,name) - if (destination is not None and port is not None) and (destination != current_destination['address'] or port != current_destination['port']): - api.LocalLB.VirtualServer.set_destination_v2(virtual_servers = [name],destinations=[{'address': destination, 'port':port}]) + if destination is not None and destination != current_destination['address']: + api.LocalLB.VirtualServer.set_destination_v2(virtual_servers = [name],destinations=[{'address': destination, 'port': current_destination['port']}]) updated=True return updated except bigsuds.OperationFailed, e: raise Exception('Error on setting destination : %s'% e ) +def set_port(api,name,port): + updated=False + try: + current_destination = get_destination(api,name) + if port is not None and port != current_destination['port']: + api.LocalLB.VirtualServer.set_destination_v2(virtual_servers = [name],destinations=[{'address': current_destination['address'], 'port': port}]) + updated=True + return updated + except bigsuds.OperationFailed, e: + raise Exception('Error on setting port : %s'% e ) + + def get_description(api,name): return api.LocalLB.VirtualServer.get_description(virtual_servers = [name])[0] @@ -404,7 +417,8 @@ def main(): # Have a transaction for all the changes try: api.System.Session.start_transaction() - result['changed']|=set_destination(api,name,fq_name(partition,destination),port) + result['changed']|=set_destination(api,name,fq_name(partition,destination)) + result['changed']|=set_port(api,name,port) result['changed']|=set_pool(api,name,pool) result['changed']|=set_description(api,name,description) result['changed']|=set_snat(api,name,snat) @@ -434,5 +448,7 @@ def main(): # import module snippets from ansible.module_utils.basic import * from ansible.module_utils.f5 import * -main() + +if __name__ == '__main__': + main() From be9058201fce7b0a2ed0f6bfbcad48e4fe81bdcb Mon Sep 17 00:00:00 2001 From: Etienne CARRIERE Date: Wed, 18 Nov 2015 06:09:28 +0100 Subject: [PATCH 8/9] Apply changes according to the review --- network/f5/bigip_virtual_server.py | 40 +++++++++++++++--------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/network/f5/bigip_virtual_server.py b/network/f5/bigip_virtual_server.py index 2387a40a79b..043a44c6ca8 100644 --- a/network/f5/bigip_virtual_server.py +++ b/network/f5/bigip_virtual_server.py @@ -237,10 +237,10 @@ def set_snat(api,name,snat): current_state=get_snat_type(api,name) if snat is None: return updated - if snat == 'None' and current_state != 'SRC_TRANS_NONE': + elif snat == 'None' and current_state != 'SRC_TRANS_NONE': api.LocalLB.VirtualServer.set_source_address_translation_none(virtual_servers = [name]) updated = True - if snat == 'Automap' and current_state != 'SRC_TRANS_AUTOMAP': + elif snat == 'Automap' and current_state != 'SRC_TRANS_AUTOMAP': api.LocalLB.VirtualServer.set_source_address_translation_automap(virtual_servers = [name]) updated = True return updated @@ -370,21 +370,21 @@ def main(): result = {'changed': False} # default if state == 'absent': - if not module.check_mode: - if vs_exists(api,name): - # hack to handle concurrent runs of module - # pool might be gone before we actually remove - try: - vs_remove(api,name) - result = {'changed' : True, 'deleted' : name } - except bigsuds.OperationFailed, e: - if "was not found" in str(e): - result['changed']= False - else: - raise - else: - # check-mode return value - result = {'changed': True} + if not module.check_mode: + if vs_exists(api,name): + # hack to handle concurrent runs of module + # pool might be gone before we actually remove + try: + vs_remove(api,name) + result = {'changed' : True, 'deleted' : name } + except bigsuds.OperationFailed, e: + if "was not found" in str(e): + result['changed']= False + else: + raise + else: + # check-mode return value + result = {'changed': True} elif state == 'present': update = False @@ -393,8 +393,8 @@ def main(): module.fail_json(msg="both destination and port must be supplied to create a VS") if not module.check_mode: # a bit of a hack to handle concurrent runs of this module. - # even though we've checked the pool doesn't exist, - # it may exist by the time we run create_pool(). + # even though we've checked the virtual_server doesn't exist, + # it may exist by the time we run virtual_server(). # this catches the exception and does something smart # about it! try: @@ -405,7 +405,7 @@ def main(): set_default_persistence_profiles(api,name,default_persistence_profile) result = {'changed': True} except bigsuds.OperationFailed, e: - raise Exception('Error on creating Virtual Server : %s' % e) + raise Exception('Error on creating Virtual Server : %s' % e) else: # check-mode return value result = {'changed': True} From aaf16fe3376773ab50e9c7b98363e294d38eedc4 Mon Sep 17 00:00:00 2001 From: Etienne CARRIERE Date: Wed, 18 Nov 2015 06:52:03 +0100 Subject: [PATCH 9/9] Developpement of enabled/disabled state --- network/f5/bigip_virtual_server.py | 35 ++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/network/f5/bigip_virtual_server.py b/network/f5/bigip_virtual_server.py index 043a44c6ca8..f7e243c5d6e 100644 --- a/network/f5/bigip_virtual_server.py +++ b/network/f5/bigip_virtual_server.py @@ -54,9 +54,13 @@ options: choices: ['yes', 'no'] state: description: - - Pool member state + - Virtual Server state required: false default: present + description: + - Absent : delete the VS if present + - present (and its synonym enabled) : create if needed the VS and set state to enabled + - absent : create if needed the VS and set state to disabled choices: ['present', 'absent', 'enabled', 'disabled'] aliases: [] partition: @@ -292,7 +296,22 @@ def set_port(api,name,port): except bigsuds.OperationFailed, e: raise Exception('Error on setting port : %s'% e ) +def get_state(api,name): + return api.LocalLB.VirtualServer.get_enabled_state(virtual_servers = [name])[0] +def set_state(api,name,state): + updated=False + try: + current_state=get_state(api,name) + # We consider that being present is equivalent to enabled + if state == 'present': + state='enabled' + if STATES[state] != current_state: + api.LocalLB.VirtualServer.set_enabled_state(virtual_servers=[name],states=[STATES[state]]) + updated=True + return updated + except bigsuds.OperationFailed, e: + raise Exception('Error on setting state : %s'% e ) def get_description(api,name): return api.LocalLB.VirtualServer.get_description(virtual_servers = [name])[0] @@ -386,7 +405,7 @@ def main(): # check-mode return value result = {'changed': True} - elif state == 'present': + else: update = False if not vs_exists(api, name): if (not destination) or (not port): @@ -403,6 +422,7 @@ def main(): set_snat(api,name,snat) set_description(api,name,description) set_default_persistence_profiles(api,name,default_persistence_profile) + set_state(api,name,state) result = {'changed': True} except bigsuds.OperationFailed, e: raise Exception('Error on creating Virtual Server : %s' % e) @@ -424,6 +444,7 @@ def main(): result['changed']|=set_snat(api,name,snat) result['changed']|=set_profiles(api,name,all_profiles) result['changed']|=set_default_persistence_profiles(api,name,default_persistence_profile) + result['changed']|=set_state(api,name,state) api.System.Session.submit_transaction() except Exception,e: raise Exception("Error on updating Virtual Server : %s" % e) @@ -431,16 +452,6 @@ def main(): # check-mode return value result = {'changed': True} - elif state in ('disabled', 'enabled'): - if name is None: - module.fail_json(msg="name parameter required when " \ - "state=enabled/disabled") - if not module.check_mode: - pass - else: - # check-mode return value - result = {'changed': True} - except Exception, e: module.fail_json(msg="received exception: %s" % e)