Debian Codegraph @

ansible_collections/community/aws/plugins/modules/autoscaling_launch_config_find.py

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