1#!/usr/bin/python 2# encoding: utf-8 3 4# (c) 2015, Jose Armesto <jose@armesto.net> 5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 6 7from __future__ import absolute_import, division, print_function 8__metaclass__ = type 9 10 11ANSIBLE_METADATA = {'metadata_version': '1.1', 12 'status': ['preview'], 13 'supported_by': 'community'} 14 15 16DOCUMENTATION = """ 17--- 18module: ec2_lc_find 19short_description: Find AWS Autoscaling Launch Configurations 20description: 21 - Returns list of matching Launch Configurations for a given name, along with other useful information 22 - Results can be sorted and sliced 23 - It depends on boto 24 - Based on the work by Tom Bamford (https://github.com/tombamford) 25 26version_added: "2.2" 27author: "Jose Armesto (@fiunchinho)" 28options: 29 region: 30 description: 31 - The AWS region to use. 32 required: true 33 aliases: ['aws_region', 'ec2_region'] 34 name_regex: 35 description: 36 - A Launch Configuration to match 37 - It'll be compiled as regex 38 required: True 39 sort_order: 40 description: 41 - Order in which to sort results. 42 choices: ['ascending', 'descending'] 43 default: 'ascending' 44 limit: 45 description: 46 - How many results to show. 47 - Corresponds to Python slice notation like list[:limit]. 48requirements: 49 - "python >= 2.6" 50 - boto3 51extends_documentation_fragment: 52 - aws 53""" 54 55EXAMPLES = ''' 56# Note: These examples do not set authentication details, see the AWS Guide for details. 57 58# Search for the Launch Configurations that start with "app" 59- ec2_lc_find: 60 name_regex: app.* 61 sort_order: descending 62 limit: 2 63''' 64 65RETURN = ''' 66image_id: 67 description: AMI id 68 returned: when Launch Configuration was found 69 type: str 70 sample: "ami-0d75df7e" 71user_data: 72 description: User data used to start instance 73 returned: when Launch Configuration was found 74 type: str 75 sample: "ZXhwb3J0IENMT1VE" 76name: 77 description: Name of the Launch Configuration 78 returned: when Launch Configuration was found 79 type: str 80 sample: "myapp-v123" 81arn: 82 description: Name of the AMI 83 returned: when Launch Configuration was found 84 type: str 85 sample: "arn:aws:autoscaling:eu-west-1:12345:launchConfiguration:d82f050e-e315:launchConfigurationName/yourproject" 86instance_type: 87 description: Type of ec2 instance 88 returned: when Launch Configuration was found 89 type: str 90 sample: "t2.small" 91created_time: 92 description: When it was created 93 returned: when Launch Configuration was found 94 type: str 95 sample: "2016-06-29T14:59:22.222000+00:00" 96ebs_optimized: 97 description: Launch Configuration EBS optimized property 98 returned: when Launch Configuration was found 99 type: bool 100 sample: False 101instance_monitoring: 102 description: Launch Configuration instance monitoring property 103 returned: when Launch Configuration was found 104 type: str 105 sample: {"Enabled": false} 106classic_link_vpc_security_groups: 107 description: Launch Configuration classic link vpc security groups property 108 returned: when Launch Configuration was found 109 type: list 110 sample: [] 111block_device_mappings: 112 description: Launch Configuration block device mappings property 113 returned: when Launch Configuration was found 114 type: list 115 sample: [] 116keyname: 117 description: Launch Configuration ssh key 118 returned: when Launch Configuration was found 119 type: str 120 sample: mykey 121security_groups: 122 description: Launch Configuration security groups 123 returned: when Launch Configuration was found 124 type: list 125 sample: [] 126kernel_id: 127 description: Launch Configuration kernel to use 128 returned: when Launch Configuration was found 129 type: str 130 sample: '' 131ram_disk_id: 132 description: Launch Configuration ram disk property 133 returned: when Launch Configuration was found 134 type: str 135 sample: '' 136associate_public_address: 137 description: Assign public address or not 138 returned: when Launch Configuration was found 139 type: bool 140 sample: True 141... 142''' 143import re 144 145from ansible.module_utils.basic import AnsibleModule 146from ansible.module_utils.ec2 import boto3_conn, ec2_argument_spec, get_aws_connection_info 147 148 149def find_launch_configs(client, module): 150 name_regex = module.params.get('name_regex') 151 sort_order = module.params.get('sort_order') 152 limit = module.params.get('limit') 153 154 paginator = client.get_paginator('describe_launch_configurations') 155 156 response_iterator = paginator.paginate( 157 PaginationConfig={ 158 'MaxItems': 1000, 159 'PageSize': 100 160 } 161 ) 162 163 results = [] 164 165 for response in response_iterator: 166 response['LaunchConfigurations'] = filter(lambda lc: re.compile(name_regex).match(lc['LaunchConfigurationName']), 167 response['LaunchConfigurations']) 168 169 for lc in response['LaunchConfigurations']: 170 data = { 171 'name': lc['LaunchConfigurationName'], 172 'arn': lc['LaunchConfigurationARN'], 173 'created_time': lc['CreatedTime'], 174 'user_data': lc['UserData'], 175 'instance_type': lc['InstanceType'], 176 'image_id': lc['ImageId'], 177 'ebs_optimized': lc['EbsOptimized'], 178 'instance_monitoring': lc['InstanceMonitoring'], 179 'classic_link_vpc_security_groups': lc['ClassicLinkVPCSecurityGroups'], 180 'block_device_mappings': lc['BlockDeviceMappings'], 181 'keyname': lc['KeyName'], 182 'security_groups': lc['SecurityGroups'], 183 'kernel_id': lc['KernelId'], 184 'ram_disk_id': lc['RamdiskId'], 185 'associate_public_address': lc.get('AssociatePublicIpAddress', False), 186 } 187 188 results.append(data) 189 190 results.sort(key=lambda e: e['name'], reverse=(sort_order == 'descending')) 191 192 if limit: 193 results = results[:int(limit)] 194 195 module.exit_json(changed=False, results=results) 196 197 198def main(): 199 argument_spec = ec2_argument_spec() 200 argument_spec.update(dict( 201 region=dict(required=True, aliases=['aws_region', 'ec2_region']), 202 name_regex=dict(required=True), 203 sort_order=dict(required=False, default='ascending', choices=['ascending', 'descending']), 204 limit=dict(required=False, type='int'), 205 ) 206 ) 207 208 module = AnsibleModule( 209 argument_spec=argument_spec, 210 ) 211 212 region, ec2_url, aws_connect_params = get_aws_connection_info(module, True) 213 214 client = boto3_conn(module=module, conn_type='client', resource='autoscaling', region=region, **aws_connect_params) 215 find_launch_configs(client, module) 216 217 218if __name__ == '__main__': 219 main() 220