Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: Ansible Project
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_info
10 version_added: 1.0.0
11 short_description: Gather information about AWS Autoscaling Launch Configurations
12 description:
13 - Gather information about AWS Autoscaling Launch Configurations.
14 - Prior to release 5.0.0 this module was called C(community.aws.ec2_lc_info).
15 The usage did not change.
16 author:
17 - "Loïc Latreille (@psykotox)"
18 options:
19 name:
20 description:
21 - A name or a list of name to match.
22 default: []
23 type: list
24 elements: str
25 sort:
26 description:
27 - Optional attribute which with to sort the results.
28 choices: ['launch_configuration_name', 'image_id', 'created_time', 'instance_type', 'kernel_id', 'ramdisk_id', 'key_name']
29 type: str
30 sort_order:
31 description:
32 - Order in which to sort results.
33 - Only used when the 'sort' parameter is specified.
34 choices: ['ascending', 'descending']
35 default: 'ascending'
36 type: str
37 sort_start:
38 description:
39 - Which result to start with (when sorting).
40 - Corresponds to Python slice notation.
41 type: int
42 sort_end:
43 description:
44 - Which result to end with (when sorting).
45 - Corresponds to Python slice notation.
46 type: int
47 extends_documentation_fragment:
48 - amazon.aws.common.modules
49 - amazon.aws.region.modules
50 - amazon.aws.boto3
51 """
52
53 EXAMPLES = r"""
54 # Note: These examples do not set authentication details, see the AWS Guide for details.
55
56 - name: Gather information about all launch configurations
57 community.aws.autoscaling_launch_config_info:
58
59 - name: Gather information about launch configuration with name "example"
60 community.aws.autoscaling_launch_config_info:
61 name: example
62
63 - name: Gather information sorted by created_time from most recent to least recent
64 community.aws.autoscaling_launch_config_info:
65 sort: created_time
66 sort_order: descending
67 """
68
69 RETURN = r"""
70 block_device_mapping:
71 description: Block device mapping for the instances of launch configuration.
72 type: list
73 returned: always
74 sample: "[{
75 'device_name': '/dev/xvda':,
76 'ebs': {
77 'delete_on_termination': true,
78 'volume_size': 8,
79 'volume_type': 'gp2'
80 }]"
81 classic_link_vpc_security_groups:
82 description: IDs of one or more security groups for the VPC specified in classic_link_vpc_id.
83 type: str
84 returned: always
85 sample:
86 created_time:
87 description: The creation date and time for the launch configuration.
88 type: str
89 returned: always
90 sample: "2016-05-27T13:47:44.216000+00:00"
91 ebs_optimized:
92 description: EBS I/O optimized C(true) or not C(false).
93 type: bool
94 returned: always
95 sample: true,
96 image_id:
97 description: ID of the Amazon Machine Image (AMI).
98 type: str
99 returned: always
100 sample: "ami-12345678"
101 instance_monitoring:
102 description: Launched with detailed monitoring or not.
103 type: dict
104 returned: always
105 sample: "{
106 'enabled': true
107 }"
108 instance_type:
109 description: Instance type.
110 type: str
111 returned: always
112 sample: "t2.micro"
113 kernel_id:
114 description: ID of the kernel associated with the AMI.
115 type: str
116 returned: always
117 sample:
118 key_name:
119 description: Name of the key pair.
120 type: str
121 returned: always
122 sample: "user_app"
123 launch_configuration_arn:
124 description: Amazon Resource Name (ARN) of the launch configuration.
125 type: str
126 returned: always
127 sample: "arn:aws:autoscaling:us-east-1:123456798012:launchConfiguration:ba785e3a-dd42-6f02-4585-ea1a2b458b3d:launchConfigurationName/lc-app"
128 launch_configuration_name:
129 description: Name of the launch configuration.
130 type: str
131 returned: always
132 sample: "lc-app"
133 ramdisk_id:
134 description: ID of the RAM disk associated with the AMI.
135 type: str
136 returned: always
137 sample:
138 security_groups:
139 description: Security groups to associated.
140 type: list
141 returned: always
142 sample: "[
143 'web'
144 ]"
145 user_data:
146 description: User data available.
147 type: str
148 returned: always
149 """
150
151 try:
152 import botocore
153 except ImportError:
154 pass # Handled by AnsibleAWSModule
155
156 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
157
158 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
159
160
161 def list_launch_configs(connection, module):
162 launch_config_name = module.params.get("name")
163 sort = module.params.get("sort")
164 sort_order = module.params.get("sort_order")
165 sort_start = module.params.get("sort_start")
166 sort_end = module.params.get("sort_end")
167
168 try:
169 pg = connection.get_paginator("describe_launch_configurations")
170 launch_configs = pg.paginate(LaunchConfigurationNames=launch_config_name).build_full_result()
171 except botocore.exceptions.ClientError as e:
172 module.fail_json_aws(e, msg="Failed to list launch configs")
173
174 snaked_launch_configs = []
175 for launch_config in launch_configs["LaunchConfigurations"]:
176 snaked_launch_configs.append(camel_dict_to_snake_dict(launch_config))
177
178 for launch_config in snaked_launch_configs:
179 if "CreatedTime" in launch_config:
180 launch_config["CreatedTime"] = str(launch_config["CreatedTime"])
181
182 if sort:
183 snaked_launch_configs.sort(key=lambda e: e[sort], reverse=(sort_order == "descending"))
184
185 if sort and sort_start and sort_end:
186 snaked_launch_configs = snaked_launch_configs[sort_start:sort_end]
187 elif sort and sort_start:
188 snaked_launch_configs = snaked_launch_configs[sort_start:]
189 elif sort and sort_end:
190 snaked_launch_configs = snaked_launch_configs[:sort_end]
191
192 module.exit_json(launch_configurations=snaked_launch_configs)
193
194
195 def main():
196 argument_spec = dict(
197 name=dict(required=False, default=[], type="list", elements="str"),
198 sort=dict(
199 required=False,
200 default=None,
201 choices=[
202 "launch_configuration_name",
203 "image_id",
204 "created_time",
205 "instance_type",
206 "kernel_id",
207 "ramdisk_id",
208 "key_name",
209 ],
210 ),
211 sort_order=dict(required=False, default="ascending", choices=["ascending", "descending"]),
212 sort_start=dict(required=False, type="int"),
213 sort_end=dict(required=False, type="int"),
214 )
215
216 module = AnsibleAWSModule(
217 argument_spec=argument_spec,
218 supports_check_mode=True,
219 )
220
221 try:
222 connection = module.client("autoscaling")
223 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
224 module.fail_json_aws(e, msg="Failed to connect to AWS")
225
226 list_launch_configs(connection, module)
227
228
229 if __name__ == "__main__":
230 main()