-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathec2_launch_template.py
844 lines (808 loc) · 34.6 KB
/
ec2_launch_template.py
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2018 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = r"""
---
module: ec2_launch_template
version_added: 1.0.0
short_description: Manage EC2 launch templates
description:
- Create, modify, and delete EC2 Launch Templates, which can be used to
create individual instances or with Autoscaling Groups.
- The M(amazon.aws.ec2_instance) and M(community.aws.autoscaling_group) modules can, instead of specifying all
parameters on those tasks, be passed a Launch Template which contains
settings like instance size, disk type, subnet, and more.
author:
- Ryan Scott Brown (@ryansb)
options:
template_id:
description:
- The ID for the launch template, can be used for all cases except creating a new Launch Template.
aliases: [id]
type: str
template_name:
description:
- The template name. This must be unique in the region-account combination you are using.
- If no launch template exists with the specified name, a new launch template is created.
- If a launch template with the specified name already exists and the configuration has not changed,
nothing happens.
- If a launch template with the specified name already exists and the configuration has changed,
a new version of the launch template is created.
aliases: [name]
type: str
default_version:
description:
- Which version should be the default when users spin up new instances based on this template? By default, the latest version will be made the default.
type: str
default: latest
version_description:
version_added: 5.5.0
description:
- The description of a launch template version.
default: ""
type: str
state:
description:
- Whether the launch template should exist or not.
- Deleting specific versions of a launch template is not supported at this time.
choices: [present, absent]
default: present
type: str
block_device_mappings:
description:
- The block device mapping. Supplying both a snapshot ID and an encryption
value as arguments for block-device mapping results in an error. This is
because only blank volumes can be encrypted on start, and these are not
created from a snapshot. If a snapshot is the basis for the volume, it
contains data by definition and its encryption status cannot be changed
using this action.
type: list
elements: dict
suboptions:
device_name:
description: The device name (for example, /dev/sdh or xvdh).
type: str
no_device:
description: Suppresses the specified device included in the block device mapping of the AMI.
type: str
virtual_name:
description: >
The virtual device name (ephemeralN). Instance store volumes are
numbered starting from 0. An instance type with 2 available instance
store volumes can specify mappings for ephemeral0 and ephemeral1. The
number of available instance store volumes depends on the instance
type. After you connect to the instance, you must mount the volume.
type: str
ebs:
description: Parameters used to automatically set up EBS volumes when the instance is launched.
type: dict
suboptions:
delete_on_termination:
description: Indicates whether the EBS volume is deleted on instance termination.
type: bool
encrypted:
description: >
Indicates whether the EBS volume is encrypted. Encrypted volumes
can only be attached to instances that support Amazon EBS
encryption. If you are creating a volume from a snapshot, you
can't specify an encryption value.
type: bool
iops:
description:
- The number of I/O operations per second (IOPS) that the volume
supports. For io1, this represents the number of IOPS that are
provisioned for the volume. For gp2, this represents the baseline
performance of the volume and the rate at which the volume
accumulates I/O credits for bursting. For more information about
General Purpose SSD baseline performance, I/O credits, and
bursting, see Amazon EBS Volume Types in the Amazon Elastic
Compute Cloud User Guide.
- >
Condition: This parameter is required for requests to create io1
volumes; it is not used in requests to create gp2, st1, sc1, or
standard volumes.
type: int
kms_key_id:
description: The ARN of the AWS Key Management Service (AWS KMS) CMK used for encryption.
type: str
snapshot_id:
description: The ID of the snapshot to create the volume from.
type: str
volume_size:
description:
- The size of the volume, in GiB.
- "Default: If you're creating the volume from a snapshot and don't specify a volume size, the default is the snapshot size."
type: int
volume_type:
description: The volume type
type: str
cpu_options:
description:
- Choose CPU settings for the EC2 instances that will be created with this template.
- For more information, see U(http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-optimize-cpu.html)
type: dict
suboptions:
core_count:
description: The number of CPU cores for the instance.
type: int
threads_per_core:
description: >
The number of threads per CPU core. To disable Intel Hyper-Threading
Technology for the instance, specify a value of 1. Otherwise, specify
the default value of 2.
type: int
credit_specification:
description: The credit option for CPU usage of the instance. Valid for T2 or T3 instances only.
type: dict
suboptions:
cpu_credits:
description: >
The credit option for CPU usage of a T2 or T3 instance. Valid values
are C(standard) and C(unlimited).
type: str
disable_api_termination:
description: >
This helps protect instances from accidental termination. If set to true,
you can't terminate the instance using the Amazon EC2 console, CLI, or
API. To change this attribute to false after launch, use
I(ModifyInstanceAttribute).
type: bool
ebs_optimized:
description: >
Indicates whether the instance is optimized for Amazon EBS I/O. This
optimization provides dedicated throughput to Amazon EBS and an optimized
configuration stack to provide optimal Amazon EBS I/O performance. This
optimization isn't available with all instance types. Additional usage
charges apply when using an EBS-optimized instance.
type: bool
elastic_gpu_specifications:
type: list
elements: dict
description: Settings for Elastic GPU attachments. See U(https://aws.amazon.com/ec2/elastic-gpus/) for details.
suboptions:
type:
description: The type of Elastic GPU to attach
type: str
iam_instance_profile:
description: >
The name or ARN of an IAM instance profile. Requires permissions to
describe existing instance roles to confirm ARN is properly formed.
type: str
image_id:
description: >
The AMI ID to use for new instances launched with this template. This
value is region-dependent since AMIs are not global resources.
type: str
instance_initiated_shutdown_behavior:
description: >
Indicates whether an instance stops or terminates when you initiate
shutdown from the instance using the operating system shutdown command.
choices: [stop, terminate]
type: str
instance_market_options:
description: Options for alternative instance markets, currently only the spot market is supported.
type: dict
suboptions:
market_type:
description: The market type. This should always be 'spot'.
type: str
spot_options:
description: Spot-market specific settings.
type: dict
suboptions:
block_duration_minutes:
description: >
The required duration for the Spot Instances (also known as Spot
blocks), in minutes. This value must be a multiple of 60 (60,
120, 180, 240, 300, or 360).
type: int
instance_interruption_behavior:
description: The behavior when a Spot Instance is interrupted. The default is C(terminate).
choices: [hibernate, stop, terminate]
type: str
max_price:
description: The highest hourly price you're willing to pay for this Spot Instance.
type: str
spot_instance_type:
description: The request type to send.
choices: [one-time, persistent]
type: str
instance_type:
description: >
The instance type, such as C(c5.2xlarge). For a full list of instance types, see
U(http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html).
type: str
kernel_id:
description: >
The ID of the kernel. We recommend that you use PV-GRUB instead of
kernels and RAM disks. For more information, see
U(http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/UserProvidedkernels.html)
type: str
key_name:
description:
- The name of the key pair. You can create a key pair using M(amazon.aws.ec2_key).
- If you do not specify a key pair, you can't connect to the instance
unless you choose an AMI that is configured to allow users another way to
log in.
type: str
monitoring:
description: Settings for instance monitoring.
type: dict
suboptions:
enabled:
type: bool
description: Whether to turn on detailed monitoring for new instances. This will incur extra charges.
network_interfaces:
description: One or more network interfaces.
type: list
elements: dict
suboptions:
associate_public_ip_address:
description: Associates a public IPv4 address with eth0 for a new network interface.
type: bool
delete_on_termination:
description: Indicates whether the network interface is deleted when the instance is terminated.
type: bool
description:
description: A description for the network interface.
type: str
device_index:
description: The device index for the network interface attachment.
type: int
groups:
description: List of security group IDs to include on this instance.
type: list
elements: str
ipv6_address_count:
description: >
The number of IPv6 addresses to assign to a network interface. Amazon
EC2 automatically selects the IPv6 addresses from the subnet range.
You can't use this option if specifying the I(ipv6_addresses) option.
type: int
ipv6_addresses:
description: >
A list of one or more specific IPv6 addresses from the IPv6 CIDR
block range of your subnet. You can't use this option if you're
specifying the I(ipv6_address_count) option.
type: list
elements: str
network_interface_id:
description: The eni ID of a network interface to attach.
type: str
private_ip_address:
description: The primary private IPv4 address of the network interface.
type: str
subnet_id:
description: The ID of the subnet for the network interface.
type: str
placement:
description: The placement group settings for the instance.
type: dict
suboptions:
affinity:
description: The affinity setting for an instance on a Dedicated Host.
type: str
availability_zone:
description: The Availability Zone for the instance.
type: str
group_name:
description: The name of the placement group for the instance.
type: str
host_id:
description: The ID of the Dedicated Host for the instance.
type: str
tenancy:
description: >
The tenancy of the instance (if the instance is running in a VPC). An
instance with a tenancy of dedicated runs on single-tenant hardware.
type: str
ram_disk_id:
description: >
The ID of the RAM disk to launch the instance with. We recommend that you
use PV-GRUB instead of kernels and RAM disks. For more information, see
U(http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/UserProvidedkernels.html)
type: str
security_group_ids:
description: A list of security group IDs (VPC or EC2-Classic) that the new instances will be added to.
type: list
elements: str
security_groups:
description: >
A list of security group names (Default VPC or EC2-Classic) that the new instances will be added to.
For any VPC other than Default, you must use I(security_group_ids).
type: list
elements: str
source_version:
description: >
The version number of the launch template version on which to base the new version.
The new version inherits the same launch parameters as the source version, except for parameters that you explicity specify.
Snapshots applied to the block device mapping are ignored when creating a new version unless they are explicitly included.
type: str
default: latest
version_added: 4.1.0
tags:
type: dict
description:
- A set of key-value pairs to be applied to resources when this Launch Template is used.
- "Tag key constraints: Tag keys are case-sensitive and accept a maximum of 127 Unicode characters. May not begin with I(aws:)"
- "Tag value constraints: Tag values are case-sensitive and accept a maximum of 255 Unicode characters."
aliases: ['resource_tags']
user_data:
description: >
The Base64-encoded user data to make available to the instance. For more information, see the Linux
U(http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html) and Windows
U(http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-instance-metadata.html#instancedata-add-user-data)
documentation on user-data.
type: str
metadata_options:
description:
- Configure EC2 Metadata options.
- For more information see the IMDS documentation
U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html).
type: dict
version_added: 1.5.0
suboptions:
http_endpoint:
type: str
description: >
This parameter enables or disables the HTTP metadata endpoint on your instances.
choices: [enabled, disabled]
default: 'enabled'
http_put_response_hop_limit:
type: int
description: >
The desired HTTP PUT response hop limit for instance metadata requests.
The larger the number, the further instance metadata requests can travel.
default: 1
http_tokens:
type: str
description: >
The state of token usage for your instance metadata requests.
choices: [optional, required]
default: 'optional'
http_protocol_ipv6:
version_added: 3.1.0
type: str
description: >
- Wether the instance metadata endpoint is available via IPv6 (C(enabled)) or not (C(disabled)).
choices: [enabled, disabled]
default: 'disabled'
instance_metadata_tags:
version_added: 3.1.0
type: str
description:
- Wether the instance tags are availble (C(enabled)) via metadata endpoint or not (C(disabled)).
choices: [enabled, disabled]
default: 'disabled'
extends_documentation_fragment:
- amazon.aws.common.modules
- amazon.aws.region.modules
- amazon.aws.boto3
"""
EXAMPLES = r"""
- name: Create an ec2 launch template
community.aws.ec2_launch_template:
name: "my_template"
image_id: "ami-04b762b4289fba92b"
key_name: my_ssh_key
instance_type: t2.micro
iam_instance_profile: myTestProfile
disable_api_termination: true
- name: >
Create a new version of an existing ec2 launch template with a different instance type,
while leaving an older version as the default version
community.aws.ec2_launch_template:
name: "my_template"
default_version: 1
instance_type: c5.4xlarge
- name: Delete an ec2 launch template
community.aws.ec2_launch_template:
name: "my_template"
state: absent
# This module does not yet allow deletion of specific versions of launch templates
"""
RETURN = r"""
latest_version:
description: Latest available version of the launch template
returned: when state=present
type: int
default_version:
description: The version that will be used if only the template name is specified. Often this is the same as the latest version, but not always.
returned: when state=present
type: int
"""
from uuid import uuid4
try:
from botocore.exceptions import BotoCoreError
from botocore.exceptions import ClientError
from botocore.exceptions import WaiterError
except ImportError:
pass # caught by AnsibleAWSModule
from ansible.module_utils._text import to_text
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict
from ansible_collections.amazon.aws.plugins.module_utils.arn import validate_aws_arn
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
from ansible_collections.amazon.aws.plugins.module_utils.transformation import scrub_none_parameters
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
def determine_iam_role(module, name_or_arn):
if validate_aws_arn(name_or_arn, service="iam", resource_type="instance-profile"):
return {"arn": name_or_arn}
iam = module.client("iam", retry_decorator=AWSRetry.jittered_backoff())
try:
role = iam.get_instance_profile(InstanceProfileName=name_or_arn, aws_retry=True)
return {"arn": role["InstanceProfile"]["Arn"]}
except is_boto3_error_code("NoSuchEntity") as e:
module.fail_json_aws(e, msg=f"Could not find instance_role {name_or_arn}")
except (BotoCoreError, ClientError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(
e,
msg=f"An error occurred while searching for instance_role {name_or_arn}. Please try supplying the full ARN.",
)
def existing_templates(module):
ec2 = module.client("ec2", retry_decorator=AWSRetry.jittered_backoff())
matches = None
try:
if module.params.get("template_id"):
matches = ec2.describe_launch_templates(
LaunchTemplateIds=[module.params.get("template_id")], aws_retry=True
)
elif module.params.get("template_name"):
matches = ec2.describe_launch_templates(
LaunchTemplateNames=[module.params.get("template_name")], aws_retry=True
)
except is_boto3_error_code("InvalidLaunchTemplateName.NotFoundException") as e:
# no named template was found, return nothing/empty versions
return None, []
except is_boto3_error_code("InvalidLaunchTemplateId.Malformed") as e: # pylint: disable=duplicate-except
module.fail_json_aws(
e,
msg=(
f"Launch template with ID {module.params.get('launch_template_id')} is not a valid ID. It should start"
" with `lt-....`"
),
)
except is_boto3_error_code("InvalidLaunchTemplateId.NotFoundException") as e: # pylint: disable=duplicate-except
module.fail_json_aws(
e,
msg=(
f"Launch template with ID {module.params.get('launch_template_id')} could not be found, please supply a"
" name instead so that a new template can be created"
),
)
except (ClientError, BotoCoreError, WaiterError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Could not check existing launch templates. This may be an IAM permission problem.")
else:
template = matches["LaunchTemplates"][0]
template_id, template_version, template_default = (
template["LaunchTemplateId"],
template["LatestVersionNumber"],
template["DefaultVersionNumber"],
)
try:
return (
template,
ec2.describe_launch_template_versions(LaunchTemplateId=template_id, aws_retry=True)[
"LaunchTemplateVersions"
],
)
except (ClientError, BotoCoreError, WaiterError) as e:
module.fail_json_aws(
e,
msg=f"Could not find launch template versions for {template['LaunchTemplateName']} (ID: {template_id}).",
)
def params_to_launch_data(module, template_params):
if template_params.get("tags"):
tag_list = ansible_dict_to_boto3_tag_list(template_params.get("tags"))
template_params["tag_specifications"] = [
{"resource_type": r_type, "tags": tag_list} for r_type in ("instance", "volume")
]
del template_params["tags"]
if module.params.get("iam_instance_profile"):
template_params["iam_instance_profile"] = determine_iam_role(module, module.params["iam_instance_profile"])
params = snake_dict_to_camel_dict(
dict((k, v) for k, v in template_params.items() if v is not None),
capitalize_first=True,
)
return params
def delete_template(module):
ec2 = module.client("ec2", retry_decorator=AWSRetry.jittered_backoff())
template, template_versions = existing_templates(module)
deleted_versions = []
if template or template_versions:
non_default_versions = [to_text(t["VersionNumber"]) for t in template_versions if not t["DefaultVersion"]]
if non_default_versions:
try:
v_resp = ec2.delete_launch_template_versions(
LaunchTemplateId=template["LaunchTemplateId"],
Versions=non_default_versions,
aws_retry=True,
)
if v_resp["UnsuccessfullyDeletedLaunchTemplateVersions"]:
module.warn(
f"Failed to delete template versions {v_resp['UnsuccessfullyDeletedLaunchTemplateVersions']} on"
f" launch template {template['LaunchTemplateId']}"
)
deleted_versions = [
camel_dict_to_snake_dict(v) for v in v_resp["SuccessfullyDeletedLaunchTemplateVersions"]
]
except (ClientError, BotoCoreError) as e:
module.fail_json_aws(
e,
msg=f"Could not delete existing versions of the launch template {template['LaunchTemplateId']}",
)
try:
resp = ec2.delete_launch_template(
LaunchTemplateId=template["LaunchTemplateId"],
aws_retry=True,
)
except (ClientError, BotoCoreError) as e:
module.fail_json_aws(e, msg=f"Could not delete launch template {template['LaunchTemplateId']}")
return {
"deleted_versions": deleted_versions,
"deleted_template": camel_dict_to_snake_dict(resp["LaunchTemplate"]),
"changed": True,
}
else:
return {"changed": False}
def create_or_update(module, template_options):
ec2 = module.client(
"ec2", retry_decorator=AWSRetry.jittered_backoff(catch_extra_error_codes=["InvalidLaunchTemplateId.NotFound"])
)
template, template_versions = existing_templates(module)
out = {}
lt_data = params_to_launch_data(module, dict((k, v) for k, v in module.params.items() if k in template_options))
lt_data = scrub_none_parameters(lt_data, descend_into_lists=True)
if not (template or template_versions):
# create a full new one
try:
resp = ec2.create_launch_template(
LaunchTemplateName=module.params["template_name"],
LaunchTemplateData=lt_data,
ClientToken=uuid4().hex,
aws_retry=True,
)
except (ClientError, BotoCoreError) as e:
module.fail_json_aws(e, msg="Couldn't create launch template")
template, template_versions = existing_templates(module)
out["changed"] = True
elif template and template_versions:
most_recent = sorted(template_versions, key=lambda x: x["VersionNumber"])[-1]
if lt_data == most_recent["LaunchTemplateData"] and module.params["version_description"] == most_recent.get(
"VersionDescription", ""
):
out["changed"] = False
return out
try:
if module.params.get("source_version") in (None, ""):
resp = ec2.create_launch_template_version(
LaunchTemplateId=template["LaunchTemplateId"],
LaunchTemplateData=lt_data,
ClientToken=uuid4().hex,
VersionDescription=str(module.params["version_description"]),
aws_retry=True,
)
elif module.params.get("source_version") == "latest":
resp = ec2.create_launch_template_version(
LaunchTemplateId=template["LaunchTemplateId"],
LaunchTemplateData=lt_data,
ClientToken=uuid4().hex,
SourceVersion=str(most_recent["VersionNumber"]),
VersionDescription=str(module.params["version_description"]),
aws_retry=True,
)
else:
try:
int(module.params.get("source_version"))
except ValueError:
module.fail_json(
msg=f"source_version param was not a valid integer, got \"{module.params.get('source_version')}\""
)
# get source template version
source_version = next(
(v for v in template_versions if v["VersionNumber"] == int(module.params.get("source_version"))),
None,
)
if source_version is None:
module.fail_json(
msg=f"source_version does not exist, got \"{module.params.get('source_version')}\""
)
resp = ec2.create_launch_template_version(
LaunchTemplateId=template["LaunchTemplateId"],
LaunchTemplateData=lt_data,
ClientToken=uuid4().hex,
SourceVersion=str(source_version["VersionNumber"]),
VersionDescription=str(module.params["version_description"]),
aws_retry=True,
)
if module.params.get("default_version") in (None, ""):
# no need to do anything, leave the existing version as default
pass
elif module.params.get("default_version") == "latest":
set_default = ec2.modify_launch_template(
LaunchTemplateId=template["LaunchTemplateId"],
DefaultVersion=to_text(resp["LaunchTemplateVersion"]["VersionNumber"]),
ClientToken=uuid4().hex,
aws_retry=True,
)
else:
try:
int(module.params.get("default_version"))
except ValueError:
module.fail_json(
msg=f"default_version param was not a valid integer, got \"{module.params.get('default_version')}\""
)
set_default = ec2.modify_launch_template(
LaunchTemplateId=template["LaunchTemplateId"],
DefaultVersion=to_text(int(module.params.get("default_version"))),
ClientToken=uuid4().hex,
aws_retry=True,
)
except (ClientError, BotoCoreError) as e:
module.fail_json_aws(e, msg="Couldn't create subsequent launch template version")
template, template_versions = existing_templates(module)
out["changed"] = True
return out
def format_module_output(module):
output = {}
template, template_versions = existing_templates(module)
template = camel_dict_to_snake_dict(template)
template_versions = [camel_dict_to_snake_dict(v) for v in template_versions]
for v in template_versions:
for ts in v["launch_template_data"].get("tag_specifications") or []:
ts["tags"] = boto3_tag_list_to_ansible_dict(ts.pop("tags"))
output.update(dict(template=template, versions=template_versions))
output["default_template"] = [v for v in template_versions if v.get("default_version")][0]
output["latest_template"] = [
v
for v in template_versions
if (v.get("version_number") and int(v["version_number"]) == int(template["latest_version_number"]))
][0]
if "version_number" in output["default_template"]:
output["default_version"] = output["default_template"]["version_number"]
if "version_number" in output["latest_template"]:
output["latest_version"] = output["latest_template"]["version_number"]
return output
def main():
template_options = dict(
block_device_mappings=dict(
type="list",
elements="dict",
options=dict(
device_name=dict(),
ebs=dict(
type="dict",
options=dict(
delete_on_termination=dict(type="bool"),
encrypted=dict(type="bool"),
iops=dict(type="int"),
kms_key_id=dict(),
snapshot_id=dict(),
volume_size=dict(type="int"),
volume_type=dict(),
),
),
no_device=dict(),
virtual_name=dict(),
),
),
cpu_options=dict(
type="dict",
options=dict(
core_count=dict(type="int"),
threads_per_core=dict(type="int"),
),
),
credit_specification=dict(
dict(type="dict"),
options=dict(
cpu_credits=dict(),
),
),
disable_api_termination=dict(type="bool"),
ebs_optimized=dict(type="bool"),
elastic_gpu_specifications=dict(
options=dict(type=dict()),
type="list",
elements="dict",
),
iam_instance_profile=dict(),
image_id=dict(),
instance_initiated_shutdown_behavior=dict(choices=["stop", "terminate"]),
instance_market_options=dict(
type="dict",
options=dict(
market_type=dict(),
spot_options=dict(
type="dict",
options=dict(
block_duration_minutes=dict(type="int"),
instance_interruption_behavior=dict(choices=["hibernate", "stop", "terminate"]),
max_price=dict(),
spot_instance_type=dict(choices=["one-time", "persistent"]),
),
),
),
),
instance_type=dict(),
kernel_id=dict(),
key_name=dict(),
monitoring=dict(
type="dict",
options=dict(enabled=dict(type="bool")),
),
metadata_options=dict(
type="dict",
options=dict(
http_endpoint=dict(choices=["enabled", "disabled"], default="enabled"),
http_put_response_hop_limit=dict(type="int", default=1),
http_tokens=dict(choices=["optional", "required"], default="optional"),
http_protocol_ipv6=dict(choices=["disabled", "enabled"], default="disabled"),
instance_metadata_tags=dict(choices=["disabled", "enabled"], default="disabled"),
),
),
network_interfaces=dict(
type="list",
elements="dict",
options=dict(
associate_public_ip_address=dict(type="bool"),
delete_on_termination=dict(type="bool"),
description=dict(),
device_index=dict(type="int"),
groups=dict(type="list", elements="str"),
ipv6_address_count=dict(type="int"),
ipv6_addresses=dict(type="list", elements="str"),
network_interface_id=dict(),
private_ip_address=dict(),
subnet_id=dict(),
),
),
placement=dict(
options=dict(
affinity=dict(),
availability_zone=dict(),
group_name=dict(),
host_id=dict(),
tenancy=dict(),
),
type="dict",
),
ram_disk_id=dict(),
security_group_ids=dict(type="list", elements="str"),
security_groups=dict(type="list", elements="str"),
tags=dict(type="dict", aliases=["resource_tags"]),
user_data=dict(),
)
arg_spec = dict(
state=dict(choices=["present", "absent"], default="present"),
template_name=dict(aliases=["name"]),
template_id=dict(aliases=["id"]),
default_version=dict(default="latest"),
source_version=dict(default="latest"),
version_description=dict(default=""),
)
arg_spec.update(template_options)
module = AnsibleAWSModule(
argument_spec=arg_spec,
required_one_of=[
("template_name", "template_id"),
],
supports_check_mode=True,
)
for interface in module.params.get("network_interfaces") or []:
if interface.get("ipv6_addresses"):
interface["ipv6_addresses"] = [{"ipv6_address": x} for x in interface["ipv6_addresses"]]
if module.params.get("state") == "present":
out = create_or_update(module, template_options)
out.update(format_module_output(module))
elif module.params.get("state") == "absent":
out = delete_template(module)
else:
module.fail_json(msg=f"Unsupported value \"{module.params.get('state')}\" for `state` parameter")
module.exit_json(**out)
if __name__ == "__main__":
main()