diff --git a/changelogs/fragments/1593-autoscaling_group_info-20-target-groups-per-call.yml b/changelogs/fragments/1593-autoscaling_group_info-20-target-groups-per-call.yml new file mode 100644 index 00000000000..7a763556b80 --- /dev/null +++ b/changelogs/fragments/1593-autoscaling_group_info-20-target-groups-per-call.yml @@ -0,0 +1,4 @@ +--- +bugfixes: + - autoscaling_group_info - fix ValidationError when describing an autoscaling group that has more than 20 target groups attached to it by breaking the request into chunks (https://github.com/ansible-collections/amazon.aws/pull/1593). + - autoscaling_group - fix ValidationError when describing an autoscaling group that has more than 20 target groups attached to it by breaking the request into chunks (https://github.com/ansible-collections/amazon.aws/pull/1593). diff --git a/plugins/modules/autoscaling_group.py b/plugins/modules/autoscaling_group.py index 8ac27e2e062..5ee414c89d4 100644 --- a/plugins/modules/autoscaling_group.py +++ b/plugins/modules/autoscaling_group.py @@ -901,12 +901,18 @@ def get_properties(autoscaling_group): if properties["target_group_arns"]: elbv2_connection = module.client("elbv2") tg_paginator = elbv2_connection.get_paginator("describe_target_groups") - tg_result = tg_paginator.paginate(TargetGroupArns=properties["target_group_arns"]).build_full_result() - target_groups = tg_result["TargetGroups"] + # Limit of 20 similar to https://docs.aws.amazon.com/elasticloadbalancing/latest/APIReference/API_DescribeLoadBalancers.html + tg_chunk_size = 20 + properties["target_group_names"] = [] + tg_chunks = [ + properties["target_group_arns"][i : i + tg_chunk_size] + for i in range(0, len(properties["target_group_arns"]), tg_chunk_size) + ] + for chunk in tg_chunks: + tg_result = tg_paginator.paginate(TargetGroupArns=chunk).build_full_result() + properties["target_group_names"].extend([tg["TargetGroupName"] for tg in tg_result["TargetGroups"]]) else: - target_groups = [] - - properties["target_group_names"] = [tg["TargetGroupName"] for tg in target_groups] + properties["target_group_names"] = [] return properties diff --git a/plugins/modules/autoscaling_group_info.py b/plugins/modules/autoscaling_group_info.py index 4e3fe5c5eab..eb376c001fd 100644 --- a/plugins/modules/autoscaling_group_info.py +++ b/plugins/modules/autoscaling_group_info.py @@ -414,8 +414,18 @@ def find_asgs(conn, module, name=None, tags=None): if elbv2: try: tg_paginator = elbv2.get_paginator("describe_target_groups") - tg_result = tg_paginator.paginate(TargetGroupArns=asg["target_group_arns"]).build_full_result() - asg["target_group_names"] = [tg["TargetGroupName"] for tg in tg_result["TargetGroups"]] + # Limit of 20 similar to https://docs.aws.amazon.com/elasticloadbalancing/latest/APIReference/API_DescribeLoadBalancers.html + tg_chunk_size = 20 + asg["target_group_names"] = [] + tg_chunks = [ + asg["target_group_arns"][i : i + tg_chunk_size] + for i in range(0, len(asg["target_group_arns"]), tg_chunk_size) + ] + for chunk in tg_chunks: + tg_result = tg_paginator.paginate(TargetGroupArns=chunk).build_full_result() + asg["target_group_names"].extend( + [tg["TargetGroupName"] for tg in tg_result["TargetGroups"]] + ) except is_boto3_error_code("TargetGroupNotFound"): asg["target_group_names"] = [] except ( diff --git a/tests/integration/targets/autoscaling_group/roles/ec2_asg/tasks/create_update_delete.yml b/tests/integration/targets/autoscaling_group/roles/ec2_asg/tasks/create_update_delete.yml index bea9e82163f..456973ce417 100644 --- a/tests/integration/targets/autoscaling_group/roles/ec2_asg/tasks/create_update_delete.yml +++ b/tests/integration/targets/autoscaling_group/roles/ec2_asg/tasks/create_update_delete.yml @@ -474,8 +474,8 @@ # Target group names have max length of 32 characters - set_fact: - tg1_name: "{{ (resource_prefix + '-tg1' ) | regex_search('(.{1,32})$') }}" - tg2_name: "{{ (resource_prefix + '-tg2' ) | regex_search('(.{1,32})$') }}" + tg1_name: "ansible-test-{{tiny_prefix}}-asg-t1" + tg2_name: "ansible-test-{{tiny_prefix}}-asg-t2" - name: create target group 1 elb_target_group: name: '{{ tg1_name }}'