From fb8c2ee8109410a04f65f94452d3134f6ba3fca8 Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Fri, 29 May 2015 14:19:38 -0500 Subject: [PATCH 1/5] Module for managing F5 wide ip --- network/f5/bigip_gtm_wide_ip.py | 178 ++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 network/f5/bigip_gtm_wide_ip.py diff --git a/network/f5/bigip_gtm_wide_ip.py b/network/f5/bigip_gtm_wide_ip.py new file mode 100644 index 00000000000..1b3a3ed48e5 --- /dev/null +++ b/network/f5/bigip_gtm_wide_ip.py @@ -0,0 +1,178 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# (c) 2015, Michael Perzel +# +# 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_gtm_wide_ip +short_description: "Manages F5 BIG-IP GTM wide ip" +description: + - "Manages F5 BIG-IP GTM wide ip" +version_added: "1.9" +author: 'Michael Perzel' +notes: + - "Requires BIG-IP software version >= 11.4" + - "F5 developed module 'bigsuds' required (see http://devcentral.f5.com)" + - "Best run as a local_action in your playbook" + - "Tested with manager and above account privilege level" + +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: [] + lb_method: + description: + - LB method of wide ip + required: true + choices: ['return_to_dns', 'null', 'round_robin', + 'ratio', 'topology', 'static_persist', 'global_availability', + 'vs_capacity', 'least_conn', 'lowest_rtt', 'lowest_hops', + 'packet_rate', 'cpu', 'hit_ratio', 'qos', 'bps', + 'drop_packet', 'explicit_ip', 'connection_rate', 'vs_score'] + aliases: [] + wide_ip: + description: + - Wide IP name + required: true + default: null + aliases: [] +''' + +EXAMPLES = ''' + - name: Set lb method + local_action: > + bigip_gtm_wide_ip + server=192.168.0.1 + user=admin + password=mysecret + lb_method=round_robin + wide_ip=my_wide_ip +''' + +try: + import bigsuds +except ImportError: + bigsuds_found = False +else: + bigsuds_found = True + +def bigip_api(bigip, user, password): + api = bigsuds.BIGIP(hostname=bigip, username=user, password=password) + return api + +def get_wide_ip_lb_method(api, wide_ip): + lb_method = api.GlobalLB.WideIP.get_lb_method(wide_ips=[wide_ip])[0] + lb_method = lb_method.strip().replace('LB_METHOD_', '').lower() + return lb_method + +def get_wide_ip_pools(api, wide_ip): + try: + return api.GlobalLB.WideIP.get_wideip_pool([wide_ip]) + except Exception, e: + print e + +def wide_ip_exists(api, wide_ip): + # hack to determine if wide_ip exists + result = False + try: + api.GlobalLB.WideIP.get_object_status(wide_ips=[wide_ip]) + result = True + except bigsuds.OperationFailed, e: + if "was not found" in str(e): + result = False + else: + # genuine exception + raise + return result + +def set_wide_ip_lb_method(api, wide_ip, lb_method): + lb_method = "LB_METHOD_%s" % lb_method.strip().upper() + api.GlobalLB.WideIP.set_lb_method(wide_ips=[wide_ip], lb_methods=[lb_method]) + +def main(): + + lb_method_choices = ['return_to_dns', 'null', 'round_robin', + 'ratio', 'topology', 'static_persist', 'global_availability', + 'vs_capacity', 'least_conn', 'lowest_rtt', 'lowest_hops', + 'packet_rate', 'cpu', 'hit_ratio', 'qos', 'bps', + 'drop_packet', 'explicit_ip', 'connection_rate', 'vs_score'] + + module = AnsibleModule( + argument_spec = dict( + server = dict(type='str', required=True), + user = dict(type='str', required=True), + password = dict(type='str', required=True), + lb_method = dict(type='str', required=True, choices=lb_method_choices), + wide_ip = dict(type='str', required=True) + ), + 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'] + wide_ip = module.params['wide_ip'] + lb_method = module.params['lb_method'] + + result = {'changed': False} # default + + try: + api = bigip_api(server, user, password) + + if not wide_ip_exists(api, wide_ip): + module.fail_json(msg="wide ip %s does not exist" % wide_ip) + + if lb_method is not None and lb_method != get_wide_ip_lb_method(api, wide_ip): + if not module.check_mode: + set_wide_ip_lb_method(api, wide_ip, lb_method) + result = {'changed': True} + else: + 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 408214d1a886cd41eb73d3bd35385bed9a44838e Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Mon, 1 Jun 2015 11:22:40 -0500 Subject: [PATCH 2/5] Style updates --- network/f5/bigip_gtm_wide_ip.py | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/network/f5/bigip_gtm_wide_ip.py b/network/f5/bigip_gtm_wide_ip.py index 1b3a3ed48e5..f201e5d9760 100644 --- a/network/f5/bigip_gtm_wide_ip.py +++ b/network/f5/bigip_gtm_wide_ip.py @@ -24,7 +24,7 @@ module: bigip_gtm_wide_ip short_description: "Manages F5 BIG-IP GTM wide ip" description: - "Manages F5 BIG-IP GTM wide ip" -version_added: "1.9" +version_added: "2.0" author: 'Michael Perzel' notes: - "Requires BIG-IP software version >= 11.4" @@ -39,23 +39,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: [] lb_method: description: - LB method of wide ip @@ -65,13 +56,10 @@ options: 'vs_capacity', 'least_conn', 'lowest_rtt', 'lowest_hops', 'packet_rate', 'cpu', 'hit_ratio', 'qos', 'bps', 'drop_packet', 'explicit_ip', 'connection_rate', 'vs_score'] - aliases: [] wide_ip: description: - Wide IP name required: true - default: null - aliases: [] ''' EXAMPLES = ''' From e2fc7b34a7b1f120d4776a41aa31bd37db1b47f2 Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Sat, 4 Jul 2015 07:51:19 -0500 Subject: [PATCH 3/5] Update bigip_api method to use variable name server --- network/f5/bigip_gtm_wide_ip.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/network/f5/bigip_gtm_wide_ip.py b/network/f5/bigip_gtm_wide_ip.py index f201e5d9760..29298114b83 100644 --- a/network/f5/bigip_gtm_wide_ip.py +++ b/network/f5/bigip_gtm_wide_ip.py @@ -80,8 +80,8 @@ except ImportError: else: bigsuds_found = True -def bigip_api(bigip, user, password): - api = bigsuds.BIGIP(hostname=bigip, username=user, password=password) +def bigip_api(server, user, password): + api = bigsuds.BIGIP(hostname=server, username=user, password=password) return api def get_wide_ip_lb_method(api, wide_ip): From c77a4a7108ec9674d0f4d5a367abe037346f3421 Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Fri, 18 Sep 2015 15:31:34 -0500 Subject: [PATCH 4/5] Wrap main() in conditional --- network/f5/bigip_gtm_wide_ip.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/network/f5/bigip_gtm_wide_ip.py b/network/f5/bigip_gtm_wide_ip.py index 29298114b83..c6a49f1fa5a 100644 --- a/network/f5/bigip_gtm_wide_ip.py +++ b/network/f5/bigip_gtm_wide_ip.py @@ -163,4 +163,6 @@ def main(): # import module snippets from ansible.module_utils.basic import * -main() + +if __name__ == '__main__': + main() From af699f847510046fd514f606dc155c85405357e8 Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Mon, 21 Sep 2015 10:18:27 -0500 Subject: [PATCH 5/5] Improve example wide_ip variable --- network/f5/bigip_gtm_wide_ip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/f5/bigip_gtm_wide_ip.py b/network/f5/bigip_gtm_wide_ip.py index c6a49f1fa5a..120921b2f7c 100644 --- a/network/f5/bigip_gtm_wide_ip.py +++ b/network/f5/bigip_gtm_wide_ip.py @@ -70,7 +70,7 @@ EXAMPLES = ''' user=admin password=mysecret lb_method=round_robin - wide_ip=my_wide_ip + wide_ip=my-wide-ip.example.com ''' try: