diff --git a/lib/ansible/modules/cloud/alicloud/ali_eip.py b/lib/ansible/modules/cloud/alicloud/ali_eip.py index 4337cca8..c9030f6b 100644 --- a/lib/ansible/modules/cloud/alicloud/ali_eip.py +++ b/lib/ansible/modules/cloud/alicloud/ali_eip.py @@ -82,6 +82,15 @@ ECS or SLB Instance to be re-associated with the specified instance. type: bool default: False + tags: + description: + - A hash/dictionaries of eip tags. C({"key":"value"}) + purge_tags: + description: + - Delete existing tags on the eip that are not specified in the task. + If True, it means you have to specify all the desired tags on each task affecting a eip. + default: False + type: bool notes: - A ip address or a instance id which has been associated with EIP can ensure idempotence. requirements: @@ -254,7 +263,9 @@ def main(): release_on_disassociation=dict(type='bool', default=False), allow_reassociation=dict(type='bool', default=False), name=dict(type='str'), - description=dict(type='str') + description=dict(type='str'), + tags=dict(type='dict'), + purge_tags=dict(type='bool', default=False) ) ) @@ -332,6 +343,23 @@ def main(): except VPCResponseError as e: module.fail_json(msg='Modify EIP attribute with an error {0}.'.format(e)) + tags = module.params['tags'] + if module.params['purge_tags']: + if not tags: + tags = eip.tags + try: + if eip.remove_tags(tags): + changed = True + except Exception as e: + module.fail_json(msg="{0}".format(e)) + + if tags: + try: + if eip.add_tags(tags): + changed = True + except Exception as e: + module.fail_json(msg="{0}".format(e)) + # Associate instance if instance_id: if eip.instance_id and eip.instance_id != instance_id: diff --git a/lib/ansible/modules/cloud/alicloud/ali_eip_info.py b/lib/ansible/modules/cloud/alicloud/ali_eip_info.py index ba0e012e..40e0e6c5 100644 --- a/lib/ansible/modules/cloud/alicloud/ali_eip_info.py +++ b/lib/ansible/modules/cloud/alicloud/ali_eip_info.py @@ -49,6 +49,9 @@ all of request parameters. See U(https://www.alibabacloud.com/help/doc-detail/36018.htm) for parameter details. Filter keys can be same as request parameter name or be lower case and use underscore ("_") or dashes ("-") to connect different words in one parameter. 'AllocationId' will be appended to I(eip_ids) automatically. + tags: + description: + - A hash/dictionaries of eip tags. C({"key":"value"}) author: - "He Guimin (@xiaozhu36)" requirements: @@ -185,6 +188,7 @@ def main(): name_prefix=dict(), ip_address_prefix=dict(type='str', aliases=['ip_prefix']), filters=dict(type='dict'), + tags=dict(type='dict') ) ) @@ -210,6 +214,7 @@ def main(): name_prefix = module.params["name_prefix"] address_prefix = module.params["ip_address_prefix"] + tags = module.params["tags"] try: for eip in vpc_connect(module).describe_eip_addresses(**new_filters): @@ -219,6 +224,13 @@ def main(): continue if eip_ids and eip.allocation_id not in eip_ids: continue + if tags: + flag = False + for key, value in list(tags.items()): + if key in list(eip.tags.keys()) and value == eip.tags[key]: + flag = True + if not flag: + continue eips.append(eip.read()) ids.append(eip.id) diff --git a/lib/ansible/modules/cloud/alicloud/ali_vpc.py b/lib/ansible/modules/cloud/alicloud/ali_vpc.py index 6408b67c..22d0d8b9 100644 --- a/lib/ansible/modules/cloud/alicloud/ali_vpc.py +++ b/lib/ansible/modules/cloud/alicloud/ali_vpc.py @@ -73,6 +73,15 @@ There will be conflict when I(multi_ok=True) and I(recent=True). default: False type: bool + tags: + description: + - A hash/dictionaries of vpc tags. C({"key":"value"}) + purge_tags: + description: + - Delete existing tags on the vpc that are not specified in the task. + If True, it means you have to specify all the desired tags on each task affecting a vpc. + default: False + type: bool notes: - There will be launch a virtual router along with creating a vpc successfully. - There is only one virtual router in one vpc and one route table in one virtual router. @@ -224,6 +233,8 @@ def main(): multi_ok=dict(type='bool', default=False), description=dict(), recent=dict(type='bool', default=False), + tags=dict(type='dict'), + purge_tags=dict(type='bool', default=False) )) module = AnsibleModule(argument_spec=argument_spec) @@ -273,10 +284,27 @@ def main(): try: if vpc.modify(vpc_name, description): changed = True - module.exit_json(changed=changed, vpc=vpc.get().read()) except VPCResponseError as e: module.fail_json(msg='Unable to modify vpc {0}, error: {1}'.format(vpc.id, e)) + tags = module.params['tags'] + if module.params['purge_tags']: + if not tags: + tags = vpc.tags + try: + if vpc.remove_tags(tags): + changed = True + except Exception as e: + module.fail_json(msg="{0}".format(e)) + + if tags: + try: + if vpc.add_tags(tags): + changed = True + except Exception as e: + module.fail_json(msg="{0}".format(e)) + module.exit_json(changed=changed, vpc=vpc.get().read()) + if __name__ == '__main__': main() diff --git a/lib/ansible/modules/cloud/alicloud/ali_vpc_info.py b/lib/ansible/modules/cloud/alicloud/ali_vpc_info.py index c1bb7c7b..4661832d 100644 --- a/lib/ansible/modules/cloud/alicloud/ali_vpc_info.py +++ b/lib/ansible/modules/cloud/alicloud/ali_vpc_info.py @@ -54,6 +54,9 @@ all of request parameters. See U(https://www.alibabacloud.com/help/doc-detail/35739.htm) for parameter details. Filter keys can be same as request parameter name or be lower case and use underscore ("_") or dash ("-") to connect different words in one parameter. 'VpcId' will be appended to I(vpc_ids) automatically. + tags: + description: + - A hash/dictionaries of vpc tags. C({"key":"value"}) author: - "He Guimin (@xiaozhu36)" requirements: @@ -178,7 +181,8 @@ def main(): vpc_name=dict(aliases=['name']), name_prefix=dict(), cidr_prefix=dict(), - filters=dict(type='dict') + filters=dict(type='dict'), + tags=dict(type='dict') ) ) module = AnsibleModule(argument_spec=argument_spec) @@ -200,6 +204,7 @@ def main(): name = module.params['vpc_name'] name_prefix = module.params['name_prefix'] cidr_prefix = module.params['cidr_prefix'] + tags = module.params['tags'] try: vpcs = [] @@ -215,6 +220,13 @@ def main(): continue if cidr_prefix and not str(vpc.cidr_block).startswith(cidr_prefix): continue + if tags: + flag = False + for key, value in list(tags.items()): + if key in list(vpc.tags.keys()) and value == vpc.tags[key]: + flag = True + if not flag: + continue vpcs.append(vpc.read()) ids.append(vpc.id) if not vpc_ids: diff --git a/lib/ansible/modules/cloud/alicloud/ali_vswitch.py b/lib/ansible/modules/cloud/alicloud/ali_vswitch.py index f5a46e95..201cc58b 100644 --- a/lib/ansible/modules/cloud/alicloud/ali_vswitch.py +++ b/lib/ansible/modules/cloud/alicloud/ali_vswitch.py @@ -66,6 +66,15 @@ description: - (Deprecated) VSwitch ID. aliases: ['subnet_id', 'id'] + tags: + description: + - A hash/dictionaries of vswitch tags. C({"key":"value"}) + purge_tags: + description: + - Delete existing tags on the vswitch that are not specified in the task. + If True, it means you have to specify all the desired tags on each task affecting a vswitch. + default: False + type: bool requirements: - "python >= 2.6" - "footmark >= 1.7.0" @@ -199,6 +208,8 @@ def main(): vpc_id=dict(required=True), name=dict(aliases=['vswitch_name', 'subnet_name']), vswitch_id=dict(aliases=['subnet_id', 'id']), + tags=dict(type='dict'), + purge_tags=dict(type='bool', default=False) )) module = AnsibleModule(argument_spec=argument_spec) @@ -251,6 +262,22 @@ def main(): except VPCResponseError as e: module.fail_json(msg='Unable to modify vswitch attribute, error: {0}'.format(e)) + tags = module.params['tags'] + if module.params['purge_tags']: + if not tags: + tags = vswitch.tags + try: + if vswitch.remove_tags(tags): + changed = True + except Exception as e: + module.fail_json(msg="{0}".format(e)) + + if tags: + try: + if vswitch.add_tags(tags): + changed = True + except Exception as e: + module.fail_json(msg="{0}".format(e)) module.exit_json(changed=changed, vswitch=vswitch.get().read()) diff --git a/lib/ansible/modules/cloud/alicloud/ali_vswitch_info.py b/lib/ansible/modules/cloud/alicloud/ali_vswitch_info.py index 6581e8e6..db70c0b1 100644 --- a/lib/ansible/modules/cloud/alicloud/ali_vswitch_info.py +++ b/lib/ansible/modules/cloud/alicloud/ali_vswitch_info.py @@ -57,6 +57,9 @@ all of request parameters. See U(https://www.alibabacloud.com/help/doc-detail/35748.htm) for parameter details. Filter keys can be same as request parameter name or be lower case and use underscores ("_") or dashes ("-") to connect different words in one parameter. 'VSwitchId' will be appended to I(vswitch_ids) automatically. + tags: + description: + - A hash/dictionaries of vswitch tags. C({"key":"value"}) author: - "He Guimin (@xiaozhu36)" requirements: @@ -182,7 +185,8 @@ def main(): name_prefix=dict(), cidr_prefix=dict(), vswitch_ids=dict(type='list', aliases=['ids', 'subnet_ids']), - filters=dict(type='dict') + filters=dict(type='dict'), + tags=dict(type='dict') ) ) @@ -206,6 +210,7 @@ def main(): cidr_block = module.params['cidr_block'] name_prefix = module.params['name_prefix'] cidr_prefix = module.params['cidr_prefix'] + tags = module.params['tags'] try: vswitches = [] @@ -223,6 +228,13 @@ def main(): continue if cidr_prefix and not str(vsw.cidr_block).startswith(cidr_prefix): continue + if tags: + flag = False + for key, value in list(tags.items()): + if key in list(vsw.tags.keys()) and value == vsw.tags[key]: + flag = True + if not flag: + continue vswitches.append(vsw.read()) ids.append(vsw.id) if not vswitch_ids: diff --git a/tests/ali_eip_test.yml b/tests/ali_eip_test.yml index f55f9098..5baf584a 100644 --- a/tests/ali_eip_test.yml +++ b/tests/ali_eip_test.yml @@ -98,6 +98,39 @@ description: "{{ description }}-ali-eip" bandwidth: 2 + - name: Changed. Add Tags. + ali_eip: + ip_address: "{{ eip.eip.ip_address }}" + tags: + Test1: "add1" + Test2: "add2" + + - name: Filter eip using tags + ali_eip_info: + tags: + Test1: "add1" + + - name: Changed. Modify Tags. + ali_eip: + ip_address: "{{ eip.eip.ip_address }}" + tags: + Test1: "add1" + Test2: "add3" + + - name: No Changed. No tags need to be added + ali_eip: + ip_address: "{{ eip.eip.ip_address }}" + tags: + Test1: "add1" + Test2: "add3" + + - name: Changed. Removing tags. + ali_eip: + ip_address: "{{ eip.eip.ip_address }}" + purge_tags: True + tags: + Test1: "add1" + - name: Changed. Release ips. ali_eip: ip_address: "{{ eip.eip.ip_address }}" diff --git a/tests/ali_vpc_test.yml b/tests/ali_vpc_test.yml index b5595370..2bb0e285 100644 --- a/tests/ali_vpc_test.yml +++ b/tests/ali_vpc_test.yml @@ -32,6 +32,47 @@ # new_name: '{{ item.vpc_name }}-modified' # with_items: '{{vpcs.vpcs}}' + - name: Changed. Add Tags. + ali_vpc: + cidr_block: '{{ vpc_cidr }}' + name: '{{ item.vpc_name }}' + tags: + Test1: "add1" + Test2: "add2" + with_items: '{{vpcs.vpcs}}' + + - name: Filter vpc using tags + ali_vpc_info: + tags: + Test1: "add1" + + - name: Changed. Modify Tags. + ali_vpc: + cidr_block: '{{ vpc_cidr }}' + name: '{{ item.vpc_name }}' + tags: + Test1: "add1" + Test2: "add3" + with_items: '{{vpcs.vpcs}}' + + - name: No Changed. No tags need to be added + ali_vpc: + cidr_block: '{{ vpc_cidr }}' + name: '{{ item.vpc_name }}' + tags: + Test1: "add1" + Test2: "add3" + with_items: '{{vpcs.vpcs}}' + + - name: Changed. Removing tags. + ali_vpc: + cidr_block: '{{ vpc_cidr }}' + name: '{{ item.vpc_name }}' + purge_tags: True + tags: + Test1: "add1" + with_items: '{{vpcs.vpcs}}' + - name: Changed. Modify description. ali_vpc: cidr_block: '{{ vpc_cidr }}' diff --git a/tests/ali_vswitch_test.yml b/tests/ali_vswitch_test.yml index 5e3b572b..21b35ebb 100644 --- a/tests/ali_vswitch_test.yml +++ b/tests/ali_vswitch_test.yml @@ -18,6 +18,47 @@ name: '{{ name }}-modified' with_items: '{{vswitches.vswitches}}' + - name: Changed. Add Tags. + ali_vswitch: + vpc_id: '{{ item.vpc_id}}' + cidr_block: '{{ item.cidr_block}}' + tags: + Test1: "add1" + Test2: "add2" + with_items: '{{vswitches.vswitches}}' + + - name: Filter vswitches using tags + ali_vswitch_info: + tags: + Test1: "add1" + + - name: Changed. Modify Tags. + ali_vswitch: + vpc_id: '{{ item.vpc_id}}' + cidr_block: '{{ item.cidr_block}}' + tags: + Test1: "add1" + Test2: "add3" + with_items: '{{vswitches.vswitches}}' + + - name: No Changed. No tags need to be added + ali_vswitch: + vpc_id: '{{ item.vpc_id}}' + cidr_block: '{{ item.cidr_block}}' + tags: + Test1: "add1" + Test2: "add3" + with_items: '{{vswitches.vswitches}}' + + - name: Changed. Removing tags. + ali_vswitch: + vpc_id: '{{ item.vpc_id}}' + cidr_block: '{{ item.cidr_block}}' + purge_tags: True + tags: + Test1: "add1" + with_items: '{{vswitches.vswitches}}' + - name: Changed. Modify description. ali_vswitch: vpc_id: '{{ item.vpc_id}}'