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