ec2_asg and ec2_asg_facts module improvements (#25166)

* ec2_asg and ec2_asg_facts module improvements

Return target group information for both ec2_asg and ec2_asg_facts
modules

Provide RETURN documentation for ec2_asg module

PEP8 fixes for ec2_asg_facts

* ec2_asg: use pagination when describing target groups

In case an ASG has 100s of target groups, ensure that
we get the full result using build_full_result
This commit is contained in:
Will Thames 2017-06-12 21:15:04 +10:00 committed by Sloane Hertel
parent c296fcb0e0
commit 16b877e2b3
3 changed files with 215 additions and 27 deletions

View file

@ -139,6 +139,13 @@ instances:
"protected_from_scale_in": "false"
}
]
launch_config_name:
description: >
Name of launch configuration associated with the ASG. Same as launch_configuration_name,
provided for compatibility with ec2_asg module.
returned: success
type: str
sample: "public-webapp-production-1"
launch_configuration_name:
description: Name of launch configuration associated with the ASG.
returned: success
@ -194,6 +201,22 @@ tags:
"propagate_at_launch": "true"
}
]
target_group_arns:
description: List of ARNs of the target groups that the ASG populates
returned: success
type: list
sample: [
"arn:aws:elasticloadbalancing:ap-southeast-2:123456789012:targetgroup/target-group-host-hello/1a2b3c4d5e6f1a2b",
"arn:aws:elasticloadbalancing:ap-southeast-2:123456789012:targetgroup/target-group-path-world/abcd1234abcd1234"
]
target_group_names:
description: List of names of the target groups that the ASG populates
returned: success
type: list
sample: [
"target-group-host-hello",
"target-group-path-world"
]
termination_policies:
description: A list of termination policies for the group.
returned: success
@ -201,12 +224,17 @@ termination_policies:
sample: ["Default"]
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ec2 import get_aws_connection_info, boto3_conn, ec2_argument_spec
from ansible.module_utils.ec2 import camel_dict_to_snake_dict, HAS_BOTO3
import re
try:
import boto3
from botocore.exceptions import ClientError
HAS_BOTO3 = True
except ImportError:
HAS_BOTO3 = False
pass # caught by imported HAS_BOTO3
def match_asg_tags(tags_to_match, asg):
for key, value in tags_to_match.items():
@ -217,6 +245,7 @@ def match_asg_tags(tags_to_match, asg):
return False
return True
def find_asgs(conn, module, name=None, tags=None):
"""
Args:
@ -265,6 +294,7 @@ def find_asgs(conn, module, name=None, tags=None):
"protected_from_scale_in": false
}
],
"launch_config_name": "public-webapp-production-1",
"launch_configuration_name": "public-webapp-production-1",
"load_balancer_names": ["public-webapp-production-lb"],
"max_size": 4,
@ -290,6 +320,8 @@ def find_asgs(conn, module, name=None, tags=None):
"value": "production"
}
],
"target_group_names": [],
"target_group_arns": [],
"termination_policies":
[
"Default"
@ -310,6 +342,14 @@ def find_asgs(conn, module, name=None, tags=None):
except ClientError as e:
module.fail_json(msg=e.message, **camel_dict_to_snake_dict(e.response))
if not asgs:
return asgs
try:
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
elbv2 = boto3_conn(module, conn_type='client', resource='elbv2', region=region, endpoint=ec2_url, **aws_connect_kwargs)
except ClientError as e:
# This is nice to have, not essential
elbv2 = None
matched_asgs = []
if name is not None:
@ -328,7 +368,18 @@ def find_asgs(conn, module, name=None, tags=None):
matched_tags = True
if matched_name and matched_tags:
matched_asgs.append(camel_dict_to_snake_dict(asg))
asg = camel_dict_to_snake_dict(asg)
# compatibility with ec2_asg module
asg['launch_config_name'] = asg['launch_configuration_name']
# workaround for https://github.com/ansible/ansible/pull/25015
if 'target_group_ar_ns' in asg:
asg['target_group_arns'] = asg['target_group_ar_ns']
del(asg['target_group_ar_ns'])
if elbv2 and asg.get('target_group_arns'):
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']]
matched_asgs.append(asg)
return matched_asgs
@ -359,9 +410,6 @@ def main():
results = find_asgs(autoscaling, module, name=asg_name, tags=asg_tags)
module.exit_json(results=results)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import *
if __name__ == '__main__':
main()