diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b698b9d..bf414ba0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ FEATURES: IMPROVEMENTS: +- Improve module ali_slb_vsg and ali_slb_vsg_facts ([#148](https://github.com/alibaba/ansible-provider/pull/148)) - Improve module ali_slb_lb and ali_slb_lb_facts ([#147](https://github.com/alibaba/ansible-provider/pull/147)) - Improve ecs instance and eni docs ([#146](https://github.com/alibaba/ansible-provider/pull/146)) - Improve module ali_eip and add its testcase ([#145](https://github.com/alibaba/ansible-provider/pull/145)) diff --git a/lib/ansible/modules/cloud/alicloud/ali_slb_lb_facts.py b/lib/ansible/modules/cloud/alicloud/ali_slb_lb_facts.py index 00f8b0c1..7d6c2b6e 100644 --- a/lib/ansible/modules/cloud/alicloud/ali_slb_lb_facts.py +++ b/lib/ansible/modules/cloud/alicloud/ali_slb_lb_facts.py @@ -80,6 +80,11 @@ returned: when success type: list sample: ["lb-dj1oi1h5l74hg22gsnugf", "lb-dj1t1xwn0y9zcr90e52i2"] +names: + description: List names of being fetched slb. + returned: when success + type: list + sample: ["ansible-foo", "ansible-bar"] load_balancers: description: - info about the server load balancer that was created or deleted. @@ -278,6 +283,7 @@ def main(): lb_ids.append(value) lbs = [] ids = [] + names = [] try: slb = slb_connect(module) @@ -286,24 +292,23 @@ def main(): ids_tmp = lb_ids[index:index + 10] if not ids_tmp: break - id_str = "" - for id in ids_tmp: - id_str += id + ',' - filters['load_balancer_id'] = id_str[:-1] + filters['load_balancer_id'] = ",".join(ids_tmp) for lb in slb.describe_load_balancers(**filters): if name_prefix and not str(lb.load_balancer_name).startswith(name_prefix): continue lbs.append(lb.read()) ids.append(lb.load_balancer_id) + names.append(lb.load_balancer_name) else: for lb in slb.describe_load_balancers(**filters): if name_prefix and not str(lb.load_balancer_name).startswith(name_prefix): continue lbs.append(lb.read()) ids.append(lb.load_balancer_id) + names.append(lb.load_balancer_name) - module.exit_json(changed=False, load_balancers=lbs, ids=ids) + module.exit_json(changed=False, load_balancers=lbs, ids=ids, names=names) except Exception as e: module.fail_json(msg="Unable to describe server load balancers, and got an error: {0}.".format(e)) diff --git a/lib/ansible/modules/cloud/alicloud/ali_slb_vsg.py b/lib/ansible/modules/cloud/alicloud/ali_slb_vsg.py index e70fe21b..8f794774 100644 --- a/lib/ansible/modules/cloud/alicloud/ali_slb_vsg.py +++ b/lib/ansible/modules/cloud/alicloud/ali_slb_vsg.py @@ -27,226 +27,173 @@ DOCUMENTATION = ''' --- module: ali_slb_vsg -version_added: "1.5.0" +version_added: "2.8" short_description: Create, Delete VServerGroup and Modify its name or backend servers. description: - - Create, Delete VServerGroup and Modify its name or backend servers. + - Create and delete a VServer group + - Add or remove backend servers or network interfaces to/from the VServer group options: state: description: - - The state of the instance after operating. + - Create and delete a VServer group. default: 'present' - choices: [ 'present', 'absent', 'list'] + choices: ['present', 'absent'] load_balancer_id: description: - - The unique ID of an Server Load Balancer instance, It is required when need to create a new vserver group. + - The Server Load Balancer instance ID. + This is used in combination with C(name) to determine if a VServer group already exists. + required: True + aliases: ['lb_id'] vserver_group_name: description: - - Virtual server group name - aliases: [ 'group_name' ] + - Virtual server group name. + This is used in conjunction with the C(load_balancer_id) to ensure idempotence. + required: True + aliases: [ 'group_name', 'name' ] backend_servers: description: - - List of backend servers that need to be added. - suboptions: - instance_id: - description: - - The ID of backend server. - required: true - port: - description: - - Port used to backend server - required: true - choices: [1~65536] - weight: - description: - - The weigth of the backend server - default: 100 - choices: [1~101] + - List of that need to be added or. + - List of hash/dictionaries backend servers or network interfaces to add in this group (see example). + If none are supplied, no backend servers will be enabled. Each server has several keys and refer to + https://www.alibabacloud.com/help/doc-detail/35215.htm. Each key should be format as under_score. + Currently the valid keys including "server_id", "port", "weight" and "type". + purge_backend_servers: + description: + - Purge existing backend servers or ENIs on VServer group that are not found in backend_servers. + - If True, existing servers or ENIs will be purged from the resource to match exactly what is defined by + I(backend_servers). If the I(backend_servers) is not set then servers will not be modified. + - If True, it means you have to specify all the desired backend servers or ENIs on each task affecting a VServer group. + default: False + type: bool vserver_group_id: description: - - Virtual server group id. It is required when need to operate an existing vserver group. + - (Deprecated) Virtual server group id. aliases: [ 'group_id' ] + multi_ok: + description: + - By default the module will not create another Load Balancer if there is another Load Balancer + with the same I(name). Specify this as true if you want duplicate Load Balancers created. + default: False + type: bool requirements: - "python >= 2.6" - - "footmark >= 1.1.16" + - "footmark >= 1.9.0" extends_documentation_fragment: - alicloud author: - "He Guimin (@xiaozhu36)" - - "Liu Qiang" ''' EXAMPLES = ''' -# basic provisioning example to create VServer Group in SLB +# Note: These examples do not set authentication details, see the Alibaba Cloud Guide for details. - name: Create VServer Group in SLB - hosts: localhost - connection: local - vars: - alicloud_access_key: - alicloud_secret_key: - alicloud_region: ap-southeast-1 - load_balancer_id: - vserver_group_name: test + ali_slb_vsg: + load_balancer_id: 'lb-cnqnc234' + name: 'ansible-vsg' + +- name: Add backend servers to vserver group + ali_slb_vsg: + load_balancer_id: 'lb-cnqnc234' + name: 'ansible-vsg' backend_servers: - - instance_id: + - instance_id: 'i-f2n3cn34c' port: 8080 weight: 100 - tasks: - - name: Create VServer Group in SLB - ali_slb_vsg: - alicloud_access_key: '{{ alicloud_access_key }}' - alicloud_secret_key: '{{ alicloud_secret_key }}' - alicloud_region: '{{ alicloud_region }}' - load_balancer_id: '{{ load_balancer_id }}' - vserver_group_name: '{{ vserver_group_name }}' - backend_servers: '{{ backend_servers }}' - state: present - register: result - - debug: var=result - -# basic provisioning example to set VServer Group attribute in SLB -- name: set VServer Group attribute in SLB - hosts: localhost - connection: local - vars: - alicloud_access_key: - alicloud_secret_key: - alicloud_region: ap-southeast-1 - load_balancer_id: - vserver_group_name: test - backend_servers: - - instance_id: - port: 8080 + type: ecs + - instance_id: 'eni-n34cjf4vd' + port: 8081 weight: 100 - tasks: - - name: Set VServer Group attribute in SLB - ali_slb_vsg: - alicloud_access_key: '{{ alicloud_access_key }}' - alicloud_secret_key: '{{ alicloud_secret_key }}' - alicloud_region: '{{ alicloud_region }}' - load_balancer_id: '{{ load_balancer_id }}' - vserver_group_name: '{{ vserver_group_name }}' - backend_servers: '{{ backend_servers }}' - state: present - register: result - - debug: var=result - -# basic provisioning example to add VServer Group Backend Servers in SLB -- name: Add VServer Group Backend Servers in SLB - hosts: localhost - connection: local - vars: - alicloud_access_key: - alicloud_secret_key: - alicloud_region: ap-southeast-1 - vserver_group_id: + type: eni + +- name: Purge backend servers from vserver group + ali_slb_vsg: + load_balancer_id: 'lb-cnqnc234' + name: 'ansible-vsg' backend_servers: - - instance_id: + - instance_id: 'eni-f2n3cn34c' port: 8080 weight: 100 - tasks: - - name: Add VServer Group Backend Servers in SLB - ali_slb_vsg: - alicloud_access_key: '{{ alicloud_access_key }}' - alicloud_secret_key: '{{ alicloud_secret_key }}' - alicloud_region: '{{ alicloud_region }}' - vserver_group_id: '{{ vserver_group_id }}' - backend_servers: '{{ backend_servers }}' - state: present - register: result - - debug: var=result - -# basic provisioning example to remove VServer Group Backend Servers in SLB -- name: remove VServer Group Backend Servers in SLB - hosts: localhost - connection: local - vars: - alicloud_access_key: - alicloud_secret_key: - alicloud_region: ap-southeast-1 - vserver_group_id: - backend_servers: - - instance_id: - port: 8080 - tasks: - - name: remove VServer Group Backend Servers in SLB - ali_slb_vsg: - alicloud_access_key: '{{ alicloud_access_key }}' - alicloud_secret_key: '{{ alicloud_secret_key }}' - alicloud_region: '{{ alicloud_region }}' - vserver_group_id: '{{ vserver_group_id }}' - backend_servers: '{{ backend_servers }}' - state: absent - register: result - - debug: var=result - -# basic provisioning example to describe VServer Group in SLB -- name: describe VServer Group - hosts: localhost - connection: local - vars: - alicloud_access_key: - alicloud_secret_key: - alicloud_region: ap-southeast-1 - vserver_group_id: - tasks: - - name: Describe VServer Group in SLB - ali_slb_vsg: - alicloud_access_key: '{{ alicloud_access_key }}' - alicloud_secret_key: '{{ alicloud_secret_key }}' - alicloud_region: '{{ alicloud_region }}' - vserver_group_id: '{{ vserver_group_id }}' - state: list - register: result - - debug: var=result - -# basic provisioning example to delete VServer Group in SLB -- name: Delete VServer Group - hosts: localhost - connection: local - vars: - alicloud_access_key: - alicloud_secret_key: - alicloud_region: ap-southeast-1 - vserver_group_id: - tasks: - - name: Delete VServer Group in SLB - ali_slb_vsg: - alicloud_access_key: '{{ alicloud_access_key }}' - alicloud_secret_key: '{{ alicloud_secret_key }}' - alicloud_region: '{{ alicloud_region }}' - vserver_group_id: '{{ vserver_group_id }}' - state: absent - register: result - - debug: var=result + type: eni + - instance_id: 'eni-n34cjf4vd' + port: 8081 + weight: 100 + type: eni + purge_backend_servers: True + +- name: Delete VServer Group in SLB + ali_slb_vsg: + load_balancer_id: 'lb-cnqnc234' + name: 'ansible-vsg' + state: absent ''' RETURN = ''' vserver_group: description: - - The info of vserver_group - returned: when success - type: dict - sample: { - "backend_servers": { - "backend_server": [ - { - "port": 80, - "server_id": "i-2ze3ajpeq3y80w4lt4jr", - "weight": 100 - } - ] - }, - "vserver_group_id": "rsp-2zejxvoxensk1", - "vserver_group_name": "Group123" - } + - info about the virtual server group that was created or deleted. + returned: on present + type: complex + contains: + address: + description: The IP address of the loal balancer + returned: always + type: string + sample: "47.94.26.126" + + backend_servers: + description: The load balancer's backend servers + returned: always + type: complex + contains: + port: + description: The backend server port + returned: always + type: int + sample: 22 + server_id: + description: The backend server id + returned: always + type: string + sample: "i-vqunci342" + type: + description: The backend server type, ecs or eni + returned: always + type: string + sample: "ecs" + weight: + description: The backend server weight + returned: always + type: int + sample: 100 + id: + description: The ID of the virtual server group was created. Same as vserver_group_id. + returned: always + type: string + sample: "rsp-2zehblhcv" + vserver_group_id: + description: The ID of the virtual server group was created. + returned: always + type: string + sample: "rsp-2zehblhcv" + vserver_group_name: + description: The name of the virtual server group was created. + returned: always + type: string + sample: "ansible-ali_slb_vsg" + name: + description: The name of the virtual server group was created. + returned: always + type: string + sample: "ansible-ali_slb_vsg" + tags: + description: The load balancer tags + returned: always + type: complex + sample: {} ''' -import time -import sys -from ast import literal_eval from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.alicloud_ecs import get_acs_connection_info, ecs_argument_spec, slb_connect +from ansible.module_utils.alicloud_ecs import ecs_argument_spec, slb_connect HAS_FOOTMARK = False @@ -258,123 +205,149 @@ HAS_FOOTMARK = False -def get_info(obj): - """ - get info from vsg object - :param obj: vsg obj - :return: res: info of vsg - """ - res = {'vserver_group_id': obj.vserver_group_id} - - if hasattr(obj, 'backend_servers'): - res['backend_servers'] = obj.backend_servers - if hasattr(obj, 'vserver_group_name'): - res['vserver_group_name'] = obj.vserver_group_name - return res - - -def convert_to_utf8(obj): - if sys.version_info.major == 3: - return obj - if isinstance(obj, dict): - res = {} - for key, value in obj.iteritems(): - res = dict(res, **{convert_to_utf8(key): convert_to_utf8(value)}) - return res - elif isinstance(obj, list): - res = [] - for i in obj: - res.append(convert_to_utf8(i)) - return res - elif type(obj) not in [int, float, bool, complex, long]: - return obj.encode('utf-8') - return obj +VALID_SERVER_PARAMS = ["server_id", "port", "weight", "type"] + + +def check_backend_servers(module, servers): + for s in servers: + for key in s.keys(): + if key not in VALID_SERVER_PARAMS: + module.fail_json(msg='Invalid backend server key {0}. Valid keys: {1}.'.format(key, VALID_SERVER_PARAMS)) + + +def format_backend_servers(servers): + backend_servers = [] + if servers: + for s in servers: + server = {} + for key, value in s.items(): + split = [] + for k in str(key).split("_"): + split.append(str.upper(k[0]) + k[1:]) + server["".join(split)] = value + backend_servers.append(server) + return backend_servers + + +def filter_backend_servers(existing, inputting): + old = [] + new = [] + removed = [] + existingList = [] + inputtingList = [] + oldList = [] + for s in existing: + existingList.append(s['server_id']) + + for s in inputting: + inputtingList.append(s['server_id']) + + for s in inputting: + if s['server_id'] in existingList: + old.append(s) + oldList.append([s['server_id']]) + continue + new.append(s) + + for s in existing: + key = s['server_id'] + if key in inputtingList: + if key not in oldList: + old.append(s) + continue + removed.append(s) + + return old, new, removed def main(): argument_spec = ecs_argument_spec() argument_spec.update(dict( - state=dict(type='str', default='present', choices=['present', 'absent', 'list']), - load_balancer_id=dict(type='str'), - vserver_group_name=dict(type='str', aliases=['group_name']), + state=dict(type='str', default='present', choices=['present', 'absent']), + load_balancer_id=dict(type='str', required=True, aliases=['lb_id']), + vserver_group_name=dict(type='str', required=True, aliases=['group_name', 'name']), backend_servers=dict(type='list'), vserver_group_id=dict(type='str', aliases=['group_id']), - old_backend_servers=dict(type='list'), - new_backend_servers=dict(type='list') + purge_backend_servers=dict(type='bool', default=False), + multi_ok=dict(type='bool', default=False) )) - module = AnsibleModule(argument_spec=argument_spec) + module = AnsibleModule(argument_spec=argument_spec, + required_if=([ + ('state', 'present', ['backend_servers']) + ]) + ) if HAS_FOOTMARK is False: module.fail_json(msg='footmark required for the module ali_slb_vsg.') slb = slb_connect(module) state = module.params['state'] - load_balancer_id = module.params['load_balancer_id'] - vserver_group_name = module.params['vserver_group_name'] - backend_servers = module.params['backend_servers'] - vserver_group_id = module.params['vserver_group_id'] + lb_id = module.params['load_balancer_id'] + vsg_name = module.params['vserver_group_name'] + changed = False - current_vserver_group = None + matching = None - if vserver_group_id: + if not module.params['multi_ok']: try: - current_vserver_group = slb.describe_vserver_group_attribute(vserver_group_id) + matching_vsgs = [] + for group in slb.describe_vserver_groups(**{'load_balancer_id': lb_id}): + if group.name != vsg_name: + continue + matching_vsgs.append(group) + if len(matching_vsgs) == 1: + matching = matching_vsgs[0] + elif len(matching_vsgs) > 1: + module.fail_json(msg='Currently there are {0} virtual server groups that have the same name {1}. ' + 'If you would like to create anyway ' + 'please pass True to the multi_ok param.'.format(len(matching_vsgs), vsg_name)) except Exception as e: module.fail_json(msg=str("Unable to describe vserver group attribute, error:{0}".format(e))) - if state == "present": - if current_vserver_group: - set_data = [] - add_data = [] - if backend_servers: - server_info = {} - decode_infos = convert_to_utf8(current_vserver_group.backend_servers) - for info in decode_infos['backend_server']: - server_info[info['port']] = info - for server in backend_servers: - if server['port'] in server_info.keys(): - if server_info[server['port']]['weight'] != server['weight']: - set_data.append(server) - else: - add_data.append(server) - if set_data or vserver_group_name: - try: - current_vserver_group.set_attribute(backend_servers=set_data, vserver_group_name=vserver_group_name) - except Exception as e: - module.fail_json(msg=str("Unable to set vserver group attribute, error:{0}".format(e))) - if add_data: - try: - current_vserver_group.add_backend_servers(backend_servers=add_data) - except Exception as e: - module.fail_json(msg=str("Unable to add vserver group backend server, error:{0}".format(e))) + + if state == 'absent': + if matching: try: - current_vserver_group = slb.describe_vserver_group_attribute(vserver_group_id) + changed = matching.delete() except Exception as e: - module.fail_json(msg=str("Unable to describe vserver group attribute, error:{0}".format(e))) - module.exit_json(changed=True, vserver_group=get_info(current_vserver_group)) - else: + module.fail_json(msg=str("Unable to delete vserver group, error: {0}".format(e))) + module.exit_json(changed=changed, vserver_group={}) + + backend_servers = module.params['backend_servers'] + check_backend_servers(module, backend_servers) + + if not matching: + try: + params = module.params + params['backend_servers'] = format_backend_servers(backend_servers[:20]) + matching = slb.create_vserver_group(**params) + changed = True + except Exception as e: + module.fail_json(msg=str("Unable to create vserver group error:{0}".format(e))) + + if backend_servers: + old, new, removed = filter_backend_servers(matching.backend_servers['backend_server'], backend_servers) + if old: try: - current_vserver_group = slb.create_vserver_group(load_balancer_id, vserver_group_name, backend_servers) + if matching.modify(backend_servers=old): + changed = True except Exception as e: - module.fail_json(msg=str("Unable to create vserver group error:{0}".format(e))) - module.exit_json(changed=True, vserver_group=get_info(current_vserver_group)) - if not current_vserver_group: - module.fail_json(msg="The specified vserver group is not exist. Please check your vserver_group_id and try again.") - elif state == "list": - module.exit_json(changed=True, vserver_group=get_info(current_vserver_group)) - elif state == 'absent': - if backend_servers: + module.fail_json(msg='Modify backend servers failed: {0}'.format(e)) + + if new: try: - current_vserver_group = current_vserver_group.remove_backend_servers(backend_servers) + if matching.add(backend_servers=new): + changed = True except Exception as e: - module.fail_json(msg=str("Unable to remove vserver group backend server, error:{0}".format(e))) - module.exit_json(changed=True, vserver_group=get_info(current_vserver_group)) - else: + module.fail_json(msg='Add backend servers failed: {0}'.format(e)) + + if module.params['purge_backend_servers'] and removed: try: - changed = current_vserver_group.delete() + if matching.remove(backend_servers=removed): + changed = True except Exception as e: - module.fail_json(msg=str("Unable to delete vserver group, error:{0}".format(e))) - module.exit_json(changed=changed, vserver_group=get_info(current_vserver_group)) + module.fail_json(msg='Remove backend servers failed: {0}'.format(e)) + module.exit_json(changed=changed, vserver_group=matching.get().read()) if __name__ == '__main__': diff --git a/lib/ansible/modules/cloud/alicloud/ali_slb_vsg_facts.py b/lib/ansible/modules/cloud/alicloud/ali_slb_vsg_facts.py index 0660155c..fed8ab39 100644 --- a/lib/ansible/modules/cloud/alicloud/ali_slb_vsg_facts.py +++ b/lib/ansible/modules/cloud/alicloud/ali_slb_vsg_facts.py @@ -27,122 +27,123 @@ DOCUMENTATION = ''' --- module: ali_slb_vsg_facts -version_added: "1.5.0" -short_description: Gather facts on vserver group of Alibaba Cloud SLB. +version_added: "2.8" +short_description: Gather facts on virtual server group of Alibaba Cloud SLB. description: - - This module fetches data from the Open API in Alicloud. - The module must be called from within the SLB vserver group itself. - + - This module fetches virtual server groups data from the Open API in Alibaba Cloud. options: - load_balancer_id: - description: - - ID of server load balancer. - required: true - aliases: [ "lb_id"] - vserver_group_ids: - description: - - A list of SLB vserver group ids. - required: false - aliases: [ "group_ids" ] + load_balancer_id: + description: + - ID of server load balancer. + required: true + aliases: ["lb_id"] + vserver_group_ids: + description: + - A list of SLB vserver group ids. + required: false + aliases: ["group_ids", "ids"] + name_prefix: + description: + - Use a vritual server group name prefix to filter vserver groups. author: - "He Guimin (@xiaozhu36)" requirements: - "python >= 2.6" - - "footmark" + - "footmark >= 1.9.0" extends_documentation_fragment: - alicloud ''' EXAMPLES = ''' -# Fetch slb server group according to setting different filters -- name: Fetch slb vserver group example - hosts: localhost - vars: - alicloud_access_key: - alicloud_secret_key: - alicloud_region: cn-beijing - load_balancer_id: lb-dj1hv3n9oemvk34evb466 - vserver_group_ids: - - rsp-dj1lrpsgr8d5v - - rsp-dj10xmgq31vl0 - tasks: - - name: Find all vserver gorup in specified slb - ali_slb_vsg_facts: - alicloud_access_key: '{{ alicloud_access_key }}' - alicloud_secret_key: '{{ alicloud_secret_key }}' - alicloud_region: '{{ alicloud_region }}' - load_balancer_id: '{{ load_balancer_id }}' - register: all_vserver_group - - debug: var=all_vserver_group - - - name: Find all vserver group by ids - ali_slb_vsg_facts: - alicloud_access_key: '{{ alicloud_access_key }}' - alicloud_secret_key: '{{ alicloud_secret_key }}' - alicloud_region: '{{ alicloud_region }}' - load_balancer_id: '{{ load_balancer_id }}' - vserver_group_ids: '{{ vserver_group_ids }}' - register: vserver_group_by_ids - - debug: var=vserver_group_by_ids +# Note: These examples do not set authentication details, see the Alibaba Cloud Guide for details. +- name: Retrieving vsgs using slb id + ali_slb_vsg_facts: + lb_id: '{{item}}' + with_items: '{{slbs.ids}}' + +- name: Filter vsg using name_regex + ali_slb_vsg_facts: + name_prefix: 'ansible-foo' + lb_id: 'lb-cn3cn34' ''' RETURN = ''' -vserver_group_ids: - description: List all vserver group's id after operating slb vserver group. +ids: + description: List ids of being fetched virtual server group. returned: when success type: list - sample: [ "rsp-dj1lrpsgr8d5v", "rsp-dj10xmgq31vl0" ] -vserver_groups: - description: Details about the slb vserver group that were created. + sample: ["rsp-2zehblhcv", "rsp-f22c4lhcv"] +names: + description: List name of being fetched virtual server group. returned: when success type: list - sample: [ - { - "backend_servers": { - "backend_server": [ - { - "port": 8282, - "server_id": "i-2ze35dldjc05dcvezgwk", - "weight": 100 - }, - { - "port": 8283, - "server_id": "i-2zehjm3jvtbkp175c2bt", - "weight": 100 - } - ] - }, - "vserver_group_id": "rsp-dj1lrpsgr8d5v", - "vserver_group_name": "group_1" - }, - { - "backend_servers": { - "backend_server": [ - { - "port": 8085, - "server_id": "i-2zehjm3jvtbkp175c2bt", - "weight": 100 - }, - { - "port": 8086, - "server_id": "i-2ze35dldjc05dcvezgwk", - "weight": 100 - } - ] - }, - "vserver_group_id": "rsp-dj10xmgq31vl0", - "vserver_group_name": "group_2" - } - ] -total: - description: The number of all vserver group after operating slb. - returned: when success - type: int - sample: 2 + sample: ["ansible-1", "ansible-2"] +vserver_groups: + description: + - info about the virtual server group that was created or deleted. + returned: on present + type: complex + contains: + address: + description: The IP address of the loal balancer + returned: always + type: string + sample: "47.94.26.126" + + backend_servers: + description: The load balancer's backend servers + returned: always + type: complex + contains: + port: + description: The backend server port + returned: always + type: int + sample: 22 + server_id: + description: The backend server id + returned: always + type: string + sample: "i-vqunci342" + type: + description: The backend server type, ecs or eni + returned: always + type: string + sample: "ecs" + weight: + description: The backend server weight + returned: always + type: int + sample: 100 + id: + description: The ID of the virtual server group was created. Same as vserver_group_id. + returned: always + type: string + sample: "rsp-2zehblhcv" + vserver_group_id: + description: The ID of the virtual server group was created. + returned: always + type: string + sample: "rsp-2zehblhcv" + vserver_group_name: + description: The name of the virtual server group was created. + returned: always + type: string + sample: "ansible-ali_slb_vsg" + name: + description: The name of the virtual server group was created. + returned: always + type: string + sample: "ansible-ali_slb_vsg" + tags: + description: The load balancer tags + returned: always + type: complex + sample: {} ''' from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.alicloud_ecs import get_acs_connection_info, ecs_argument_spec, slb_connect +from ansible.module_utils.alicloud_ecs import ecs_argument_spec, slb_connect HAS_FOOTMARK = False @@ -153,75 +154,42 @@ HAS_FOOTMARK = False -def get_info(obj): - """ - get info from vsg object - :param obj: vsg obj - :return: res: info of vsg - """ - res = {'vserver_group_id': obj.vserver_group_id} - - if hasattr(obj, 'backend_servers'): - res['backend_servers'] = obj.backend_servers - if hasattr(obj, 'vserver_group_name'): - res['vserver_group_name'] = obj.vserver_group_name - return res - - def main(): argument_spec = ecs_argument_spec() argument_spec.update(dict( load_balancer_id=dict(type='str', aliases=['lb_id'], required=True), - vserver_group_ids=dict(type='list', aliases=['group_ids']) + vserver_group_ids=dict(type='list', aliases=['group_ids', 'ids']), + name_prefix=dict(type='str') )) module = AnsibleModule(argument_spec=argument_spec) if HAS_FOOTMARK is False: module.fail_json(msg="Package 'footmark' required for this module.") - load_balancer_id = module.params['load_balancer_id'] - vserver_group_ids = module.params['vserver_group_ids'] - ids = [] - result = [] - all_vserver_group_ids = [] + vsg_ids = module.params['vserver_group_ids'] + name_prefix = module.params['name_prefix'] - if vserver_group_ids and (not isinstance(vserver_group_ids, list) or len(vserver_group_ids)) < 1: - module.fail_json(msg='vserver_group_ids should be a list of vserver group ids, aborting') + ids = [] + vsgs = [] + names = [] try: slb = slb_connect(module) - laod_balancer = slb.describe_load_balancers(load_balancer_id=load_balancer_id) - - if laod_balancer and len(laod_balancer) == 1: - - # list all vserver groups in selected load balancer - for vserver_group_obj in slb.describe_vserver_groups(load_balancer_id=load_balancer_id): - all_vserver_group_ids.append(vserver_group_obj.vserver_group_id) - - # if list of vserver group id provided - if vserver_group_ids: - - for vserver_group_id in vserver_group_ids: - - # check whether provided vserver grooup id is valid or not - if vserver_group_id in all_vserver_group_ids: - vserver_group = slb.describe_vserver_group_attribute(vserver_group_id) - result.append(get_info(vserver_group)) - ids.append(vserver_group_id) - - # list all vserver group in specified slb - else: - for vserver_group_id in all_vserver_group_ids: - vserver_group = slb.describe_vserver_group_attribute(vserver_group_id) - result.append(get_info(vserver_group)) - ids.append(vserver_group.vserver_group_id) - - module.exit_json(changed=False, vserver_group_ids=ids, - vserver_groups=result, total=len(result)) - else: - module.fail_json(msg="Unable to describe slb vserver groups, invalid load balancer id") + groups = slb.describe_vserver_groups(**{'load_balancer_id': module.params['load_balancer_id']}) + + if groups: + for group in groups: + if vsg_ids and group.id not in vsg_ids: + continue + if name_prefix and not str(group.name).startswith(name_prefix): + continue + vsgs.append(group.read()) + ids.append(group.id) + names.append(group.name) + + module.exit_json(changed=False, vserver_groups=vsgs, ids=ids, names=names) except Exception as e: - module.fail_json(msg=str("Unable to describe slb vserver group, error:{0}".format(e))) + module.fail_json(msg=str("Unable to describe slb vserver groups, error:{0}".format(e))) if __name__ == '__main__': diff --git a/tests/ali_slb_vsg_facts_test.yml b/tests/ali_slb_vsg_facts_test.yml new file mode 100644 index 00000000..8fc1c972 --- /dev/null +++ b/tests/ali_slb_vsg_facts_test.yml @@ -0,0 +1,97 @@ +--- +- name: Validate module ali_slb_vsg_facts + hosts: localhost + remote_user: root + vars: + number_of_instances: 2 + name: ansible-testacc-ali_slb_vsg_facts + roles: + - vpc + - vswitch + - security_group + - slb + - instance + + tasks: + - name: Changed. Create a VServer Group in public SLB + ali_slb_vsg: + lb_id: '{{ slbpub.load_balancer.id }}' + name: '{{name}}' + backend_servers: + - server_id: '{{instances.ids.0}}' + port: 20 + weight: 100 + type: ecs + register: vsg + + - name: Changed. Create four VServer Group in private SLB + ali_slb_vsg: + lb_id: '{{ slbpri.load_balancer.id }}' + name: '{{name}}-{{item}}' + backend_servers: + - server_id: '{{instances.ids.1}}' + port: 20 + weight: 100 + type: ecs + with_sequence: count=4 + register: vsgs + + - name: Filter slb using name_regex + ali_slb_lb_facts: + name_prefix: '{{name}}' + register: slbs + + - name: Retrieving vsgs using slb id + ali_slb_vsg_facts: + lb_id: '{{item}}' + with_items: '{{slbs.ids}}' + + - name: Filter vsg using name_regex + ali_slb_vsg_facts: + name_prefix: '{{name}}-' + lb_id: '{{item}}' + with_items: '{{slbs.ids}}' + + - name: Filter vsgs using ids + ali_slb_vsg_facts: + ids: '{{item.vserver_group.id}}' + lb_id: '{{slbpri.load_balancer.id}}' + with_items: '{{vsgs.results}}' + + - name: Retrieving all vsgs based on name_regex + ali_slb_vsg_facts: + name_prefix: '{{name}}' + lb_id: '{{item}}' + with_items: '{{slbs.ids}}' + + - name: Changed. Delete all slbs + ali_slb_lb: + name: '{{ item.name }}' + state: absent + with_items: "{{slbs.load_balancers}}" + + - name: Changed. Delete ecs instances + ali_instance: + instance_ids: '{{ instances.ids }}' + force: True + state: absent + + - name: Changed. Deleting vswitches + ali_vswitch: + vpc_id: '{{ item.vpc_id}}' + cidr_block: '{{ item.cidr_block}}' + state: absent + with_items: '{{vswitches.vswitches}}' + + - name: Changed. Deleting the security groups + ali_security_group: + name: '{{ item.group_name }}' + vpc_id: '{{item.vpc_id}}' + state: absent + with_items: '{{sgs.groups}}' + + - name: Changed. Deleting vpcs + ali_vpc: + name: '{{vpc.vpc.vpc_name}}' + cidr_block: '{{vpc.vpc.cidr_block}}' + state: absent diff --git a/tests/ali_slb_vsg_test.yml b/tests/ali_slb_vsg_test.yml new file mode 100644 index 00000000..6991fa9b --- /dev/null +++ b/tests/ali_slb_vsg_test.yml @@ -0,0 +1,220 @@ +--- +- name: Validate module ali_slb_vsg + hosts: localhost + remote_user: root + vars: + number_of_instances: 4 + name: ansible-testacc-ali_slb_vsg + roles: + - vpc + - vswitch + - security_group + - slb + - instance + + tasks: + - name: Changed. Create a VServer Group in SLB + ali_slb_vsg: + lb_id: '{{ slbpri.load_balancer.id }}' + name: '{{name}}' + backend_servers: + - server_id: '{{instances.ids.0}}' + port: 20 + weight: 100 + type: ecs + register: vsg + + - name: Changed. Add backend servers + ali_slb_vsg: + name: '{{ vsg.vserver_group.vserver_group_name }}' + lb_id: '{{ slbpri.load_balancer.id }}' + backend_servers: + - server_id: '{{instances.ids.1}}' + port: 21 + weight: 100 + type: ecs + - server_id: '{{instances.ids.2}}' + port: 22 + weight: 100 + type: ecs + + - name: No Changed. There is no any new backend servers + ali_slb_vsg: + name: '{{ vsg.vserver_group.vserver_group_name }}' + lb_id: '{{ slbpri.load_balancer.id }}' + backend_servers: + - server_id: '{{instances.ids.0}}' + port: 20 + weight: 100 + type: ecs + - server_id: '{{instances.ids.1}}' + port: 21 + weight: 100 + type: ecs + - server_id: '{{instances.ids.2}}' + port: 22 + weight: 100 + type: ecs + + - name: Changed. Change backend server weight and port + ali_slb_vsg: + name: '{{ vsg.vserver_group.vserver_group_name }}' + lb_id: '{{ slbpri.load_balancer.id }}' + backend_servers: + - server_id: '{{instances.ids.0}}' + port: 22 + weight: 10 + type: ecs + - server_id: '{{instances.ids.1}}' + port: 23 + weight: 20 + type: ecs + - server_id: '{{instances.ids.2}}' + port: 22 + weight: 100 + type: ecs + + - name: No Changed. There is no any backend server need to change + ali_slb_vsg: + name: '{{ vsg.vserver_group.vserver_group_name }}' + lb_id: '{{ slbpri.load_balancer.id }}' + backend_servers: + - server_id: '{{instances.ids.0}}' + port: 22 + weight: 10 + type: ecs + - server_id: '{{instances.ids.1}}' + port: 23 + weight: 20 + type: ecs + - server_id: '{{instances.ids.2}}' + port: 22 + weight: 100 + type: ecs + + - name: Changed. Remove a backend server + ali_slb_vsg: + name: '{{ vsg.vserver_group.vserver_group_name }}' + lb_id: '{{ slbpri.load_balancer.id }}' + backend_servers: + - server_id: '{{instances.ids.1}}' + port: 23 + weight: 20 + type: ecs + - server_id: '{{instances.ids.2}}' + port: 22 + weight: 100 + type: ecs + purge_backend_servers: True + + - name: No Changed. There is no any backend server will be removed + ali_slb_vsg: + name: '{{ vsg.vserver_group.vserver_group_name }}' + lb_id: '{{ slbpri.load_balancer.id }}' + backend_servers: + - server_id: '{{instances.ids.1}}' + port: 23 + weight: 20 + type: ecs + - server_id: '{{instances.ids.2}}' + port: 22 + weight: 100 + type: ecs + purge_backend_servers: True + + - name: Changed. Remove a backend server and add add a new one + ali_slb_vsg: + name: '{{ vsg.vserver_group.vserver_group_name }}' + lb_id: '{{ slbpri.load_balancer.id }}' + backend_servers: + - server_id: '{{instances.ids.2}}' + port: 22 + weight: 100 + type: ecs + - server_id: '{{instances.ids.3}}' + port: 24 + weight: 10 + type: ecs + purge_backend_servers: True + + - name: Changed. Remove a backend server and add add two and modify existing + ali_slb_vsg: + name: '{{ vsg.vserver_group.vserver_group_name }}' + lb_id: '{{ slbpri.load_balancer.id }}' + backend_servers: + - server_id: '{{instances.ids.3}}' + port: 25 + weight: 100 + type: ecs + - server_id: '{{instances.ids.1}}' + port: 22 + weight: 100 + type: ecs + - server_id: '{{instances.ids.0}}' + port: 23 + weight: 100 + type: ecs + purge_backend_servers: True + + - name: No Changed. There is no any changes + ali_slb_vsg: + name: '{{ vsg.vserver_group.vserver_group_name }}' + lb_id: '{{ slbpri.load_balancer.id }}' + backend_servers: + - server_id: '{{instances.ids.3}}' + port: 25 + weight: 100 + type: ecs + - server_id: '{{instances.ids.1}}' + port: 22 + weight: 100 + type: ecs + - server_id: '{{instances.ids.0}}' + port: 23 + weight: 100 + type: ecs + purge_backend_servers: True + + - name: Changed. Delete the vserver group + ali_slb_vsg: + name: '{{ vsg.vserver_group.vserver_group_name }}' + lb_id: '{{ slbpri.load_balancer.id }}' + state: absent + + - name: Filter slb using name_regex + ali_slb_lb_facts: + name_prefix: '{{name}}' + register: slbs + + - name: Changed. Delete slb instances + ali_slb_lb: + name: '{{ item.name }}' + state: absent + with_items: '{{slbs.load_balancers}}' + + - name: Changed. Delete ecs instances + ali_instance: + instance_ids: '{{ instances.ids }}' + force: True + state: absent + + - name: Changed. Deleting vswitches + ali_vswitch: + vpc_id: '{{ item.vpc_id}}' + cidr_block: '{{ item.cidr_block}}' + state: absent + with_items: '{{vswitches.vswitches}}' + + - name: Changed. Deleting the security groups + ali_security_group: + name: '{{ item.group_name }}' + vpc_id: '{{item.vpc_id}}' + state: absent + with_items: '{{sgs.groups}}' + + - name: Changed. Deleting vpcs + ali_vpc: + name: '{{vpc.vpc.vpc_name}}' + cidr_block: '{{vpc.vpc.cidr_block}}' + state: absent + diff --git a/tests/roles/slb/tasks/main.yml b/tests/roles/slb/tasks/main.yml index 8fdbd480..69b3bf01 100644 --- a/tests/roles/slb/tasks/main.yml +++ b/tests/roles/slb/tasks/main.yml @@ -1,9 +1,18 @@ --- -- name: Creating an SLB instance +- name: Creating a public SLB instance ali_slb_lb: - name: '{{ name }}-ali_slb_lb' + name: '{{ name }}-public' internet_charge_type: '{{ internet_charge_type }}' is_internet: True spec: slb.s1.small state: 'present' - register: slb \ No newline at end of file + register: slbpub + +- name: Creating a private SLB instance + ali_slb_lb: + name: '{{ name }}-private' + internet_charge_type: '{{ internet_charge_type }}' + spec: slb.s1.small + vswitch_id: '{{ vswitches.vswitches.0.id }}' + state: 'present' + register: slbpri \ No newline at end of file