| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the 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 |
| 10 |
version_added: 1.0.0 |
| 11 |
|
| 12 |
short_description: Create or delete AWS Autoscaling Launch Configurations |
| 13 |
|
| 14 |
description: |
| 15 |
- Can create or delete AWS Autoscaling Configurations. |
| 16 |
- Works with the M(community.aws.autoscaling_group) module to manage Autoscaling Groups. |
| 17 |
- Prior to release 5.0.0 this module was called C(community.aws.ec2_lc). |
| 18 |
The usage did not change. |
| 19 |
|
| 20 |
notes: |
| 21 |
- Amazon ASG Autoscaling Launch Configurations are immutable once created, so modifying the configuration after it is changed will not modify the |
| 22 |
launch configuration on AWS. You must create a new config and assign it to the ASG instead. |
| 23 |
|
| 24 |
author: |
| 25 |
- "Gareth Rushgrove (@garethr)" |
| 26 |
- "Willem van Ketwich (@wilvk)" |
| 27 |
|
| 28 |
options: |
| 29 |
state: |
| 30 |
description: |
| 31 |
- Register or deregister the instance. |
| 32 |
default: present |
| 33 |
choices: ['present', 'absent'] |
| 34 |
type: str |
| 35 |
name: |
| 36 |
description: |
| 37 |
- Unique name for configuration. |
| 38 |
required: true |
| 39 |
type: str |
| 40 |
instance_type: |
| 41 |
description: |
| 42 |
- Instance type to use for the instance. |
| 43 |
- Required when creating a new Launch Configuration. |
| 44 |
type: str |
| 45 |
image_id: |
| 46 |
description: |
| 47 |
- The AMI unique identifier to be used for the group. |
| 48 |
type: str |
| 49 |
key_name: |
| 50 |
description: |
| 51 |
- The SSH key name to be used for access to managed instances. |
| 52 |
type: str |
| 53 |
security_groups: |
| 54 |
description: |
| 55 |
- A list of security groups to apply to the instances. |
| 56 |
- You can specify either security group names or IDs or a mix. |
| 57 |
type: list |
| 58 |
elements: str |
| 59 |
default: [] |
| 60 |
volumes: |
| 61 |
description: |
| 62 |
- A list dictionaries defining the volumes to create. |
| 63 |
- For any volume, a volume size less than C(1) will be interpreted as a request not to create the volume. |
| 64 |
type: list |
| 65 |
elements: dict |
| 66 |
suboptions: |
| 67 |
device_name: |
| 68 |
type: str |
| 69 |
description: |
| 70 |
- The name for the volume (For example C(/dev/sda)). |
| 71 |
required: true |
| 72 |
no_device: |
| 73 |
type: bool |
| 74 |
description: |
| 75 |
- When I(no_device=true) the device will not be created. |
| 76 |
snapshot: |
| 77 |
type: str |
| 78 |
description: |
| 79 |
- The ID of an EBS snapshot to copy when creating the volume. |
| 80 |
- Mutually exclusive with the I(ephemeral) parameter. |
| 81 |
ephemeral: |
| 82 |
type: str |
| 83 |
description: |
| 84 |
- Whether the volume should be ephemeral. |
| 85 |
- Data on ephemeral volumes is lost when the instance is stopped. |
| 86 |
- Mutually exclusive with the I(snapshot) parameter. |
| 87 |
volume_size: |
| 88 |
type: int |
| 89 |
description: |
| 90 |
- The size of the volume (in GiB). |
| 91 |
- Required unless one of I(ephemeral), I(snapshot) or I(no_device) is set. |
| 92 |
volume_type: |
| 93 |
type: str |
| 94 |
description: |
| 95 |
- The type of volume to create. |
| 96 |
- See |
| 97 |
U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html) for more information on the available volume types. |
| 98 |
delete_on_termination: |
| 99 |
type: bool |
| 100 |
default: false |
| 101 |
description: |
| 102 |
- Whether the volume should be automatically deleted when the instance |
| 103 |
is terminated. |
| 104 |
iops: |
| 105 |
type: int |
| 106 |
description: |
| 107 |
- The number of IOPS per second to provision for the volume. |
| 108 |
- Required when I(volume_type=io1). |
| 109 |
throughput: |
| 110 |
type: int |
| 111 |
description: |
| 112 |
- The throughput to provision for a gp3 volume. |
| 113 |
- Valid Range is a minimum value of 125 and a maximum value of 1000. |
| 114 |
version_added: 3.1.0 |
| 115 |
encrypted: |
| 116 |
type: bool |
| 117 |
default: false |
| 118 |
description: |
| 119 |
- Whether the volume should be encrypted using the 'aws/ebs' KMS CMK. |
| 120 |
user_data: |
| 121 |
description: |
| 122 |
- Opaque blob of data which is made available to the ec2 instance. Mutually exclusive with I(user_data_path). |
| 123 |
type: str |
| 124 |
user_data_path: |
| 125 |
description: |
| 126 |
- Path to the file that contains userdata for the ec2 instances. Mutually exclusive with I(user_data). |
| 127 |
type: path |
| 128 |
kernel_id: |
| 129 |
description: |
| 130 |
- Kernel id for the EC2 instance. |
| 131 |
type: str |
| 132 |
spot_price: |
| 133 |
description: |
| 134 |
- The spot price you are bidding. Only applies for an autoscaling group with spot instances. |
| 135 |
type: float |
| 136 |
instance_monitoring: |
| 137 |
description: |
| 138 |
- Specifies whether instances are launched with detailed monitoring. |
| 139 |
type: bool |
| 140 |
default: false |
| 141 |
assign_public_ip: |
| 142 |
description: |
| 143 |
- Used for Auto Scaling groups that launch instances into an Amazon Virtual Private Cloud. Specifies whether to assign a public IP address |
| 144 |
to each instance launched in a Amazon VPC. |
| 145 |
type: bool |
| 146 |
ramdisk_id: |
| 147 |
description: |
| 148 |
- A RAM disk id for the instances. |
| 149 |
type: str |
| 150 |
instance_profile_name: |
| 151 |
description: |
| 152 |
- The name or the Amazon Resource Name (ARN) of the instance profile associated with the IAM role for the instances. |
| 153 |
type: str |
| 154 |
ebs_optimized: |
| 155 |
description: |
| 156 |
- Specifies whether the instance is optimized for EBS I/O (true) or not (false). |
| 157 |
default: false |
| 158 |
type: bool |
| 159 |
classic_link_vpc_id: |
| 160 |
description: |
| 161 |
- Id of ClassicLink enabled VPC |
| 162 |
type: str |
| 163 |
classic_link_vpc_security_groups: |
| 164 |
description: |
| 165 |
- A list of security group IDs with which to associate the ClassicLink VPC instances. |
| 166 |
type: list |
| 167 |
elements: str |
| 168 |
vpc_id: |
| 169 |
description: |
| 170 |
- VPC ID, used when resolving security group names to IDs. |
| 171 |
type: str |
| 172 |
instance_id: |
| 173 |
description: |
| 174 |
- The Id of a running instance to use as a basis for a launch configuration. Can be used in place of I(image_id) and I(instance_type). |
| 175 |
type: str |
| 176 |
placement_tenancy: |
| 177 |
description: |
| 178 |
- Determines whether the instance runs on single-tenant hardware or not. |
| 179 |
- When not set AWS will default to C(default). |
| 180 |
type: str |
| 181 |
choices: ['default', 'dedicated'] |
| 182 |
extends_documentation_fragment: |
| 183 |
- amazon.aws.common.modules |
| 184 |
- amazon.aws.region.modules |
| 185 |
- amazon.aws.boto3 |
| 186 |
""" |
| 187 |
|
| 188 |
EXAMPLES = r""" |
| 189 |
|
| 190 |
- name: create a launch configuration with an encrypted volume |
| 191 |
community.aws.autoscaling_launch_config: |
| 192 |
name: special |
| 193 |
image_id: ami-XXX |
| 194 |
key_name: default |
| 195 |
security_groups: |
| 196 |
- 'group' |
| 197 |
- 'group2' |
| 198 |
instance_type: t1.micro |
| 199 |
volumes: |
| 200 |
- device_name: /dev/sda1 |
| 201 |
volume_size: 100 |
| 202 |
volume_type: io1 |
| 203 |
iops: 3000 |
| 204 |
delete_on_termination: true |
| 205 |
encrypted: true |
| 206 |
- device_name: /dev/sdb |
| 207 |
ephemeral: ephemeral0 |
| 208 |
|
| 209 |
- name: create a launch configuration using a running instance id as a basis |
| 210 |
community.aws.autoscaling_launch_config: |
| 211 |
name: special |
| 212 |
instance_id: i-00a48b207ec59e948 |
| 213 |
key_name: default |
| 214 |
security_groups: |
| 215 |
- 'launch-wizard-2' |
| 216 |
volumes: |
| 217 |
- device_name: /dev/sda1 |
| 218 |
volume_size: 120 |
| 219 |
volume_type: io1 |
| 220 |
iops: 3000 |
| 221 |
delete_on_termination: true |
| 222 |
|
| 223 |
- name: create a launch configuration to omit the /dev/sdf EBS device that is included in the AMI image |
| 224 |
community.aws.autoscaling_launch_config: |
| 225 |
name: special |
| 226 |
image_id: ami-XXX |
| 227 |
key_name: default |
| 228 |
security_groups: |
| 229 |
- 'group' |
| 230 |
- 'group2' |
| 231 |
instance_type: t1.micro |
| 232 |
volumes: |
| 233 |
- device_name: /dev/sdf |
| 234 |
no_device: true |
| 235 |
|
| 236 |
- name: Use EBS snapshot ID for volume |
| 237 |
block: |
| 238 |
- name: Set Volume Facts |
| 239 |
ansible.builtin.set_fact: |
| 240 |
volumes: |
| 241 |
- device_name: /dev/sda1 |
| 242 |
volume_size: 20 |
| 243 |
ebs: |
| 244 |
snapshot: snap-XXXX |
| 245 |
volume_type: gp2 |
| 246 |
delete_on_termination: true |
| 247 |
encrypted: false |
| 248 |
|
| 249 |
- name: Create launch configuration |
| 250 |
community.aws.autoscaling_launch_config: |
| 251 |
name: lc1 |
| 252 |
image_id: ami-xxxx |
| 253 |
assign_public_ip: true |
| 254 |
instance_type: t2.medium |
| 255 |
key_name: my-key |
| 256 |
security_groups: |
| 257 |
- 'sg-xxxx' |
| 258 |
volumes: "{{ volumes }}" |
| 259 |
register: lc_info |
| 260 |
""" |
| 261 |
|
| 262 |
RETURN = r""" |
| 263 |
arn: |
| 264 |
description: The Amazon Resource Name of the launch configuration. |
| 265 |
returned: when I(state=present) |
| 266 |
type: str |
| 267 |
sample: arn:aws:autoscaling:us-east-1:123456789012:launchConfiguration:888d9b58-d93a-40c4-90cf-759197a2621a:launchConfigurationName/launch_config_name |
| 268 |
changed: |
| 269 |
description: Whether the state of the launch configuration has changed. |
| 270 |
returned: always |
| 271 |
type: bool |
| 272 |
sample: false |
| 273 |
created_time: |
| 274 |
description: The creation date and time for the launch configuration. |
| 275 |
returned: when I(state=present) |
| 276 |
type: str |
| 277 |
sample: '2017-11-03 23:46:44.841000' |
| 278 |
image_id: |
| 279 |
description: The ID of the Amazon Machine Image used by the launch configuration. |
| 280 |
returned: when I(state=present) |
| 281 |
type: str |
| 282 |
sample: ami-9be6f38c |
| 283 |
instance_type: |
| 284 |
description: The instance type for the instances. |
| 285 |
returned: when I(state=present) |
| 286 |
type: str |
| 287 |
sample: t1.micro |
| 288 |
name: |
| 289 |
description: The name of the launch configuration. |
| 290 |
returned: when I(state=present) |
| 291 |
type: str |
| 292 |
sample: launch_config_name |
| 293 |
result: |
| 294 |
description: The specification details for the launch configuration. |
| 295 |
returned: when I(state=present) |
| 296 |
type: complex |
| 297 |
contains: |
| 298 |
PlacementTenancy: |
| 299 |
description: The tenancy of the instances, either default or dedicated. |
| 300 |
returned: when I(state=present) |
| 301 |
type: str |
| 302 |
sample: default |
| 303 |
associate_public_ip_address: |
| 304 |
description: (EC2-VPC) Indicates whether to assign a public IP address to each instance. |
| 305 |
returned: when I(state=present) |
| 306 |
type: bool |
| 307 |
sample: false |
| 308 |
block_device_mappings: |
| 309 |
description: A block device mapping, which specifies the block devices. |
| 310 |
returned: when I(state=present) |
| 311 |
type: complex |
| 312 |
contains: |
| 313 |
device_name: |
| 314 |
description: The device name exposed to the EC2 instance (for example, /dev/sdh or xvdh). |
| 315 |
returned: when I(state=present) |
| 316 |
type: str |
| 317 |
sample: /dev/sda1 |
| 318 |
ebs: |
| 319 |
description: The information about the Amazon EBS volume. |
| 320 |
returned: when I(state=present) |
| 321 |
type: complex |
| 322 |
contains: |
| 323 |
snapshot_id: |
| 324 |
description: The ID of the snapshot. |
| 325 |
returned: when I(state=present) |
| 326 |
type: str |
| 327 |
volume_size: |
| 328 |
description: The volume size, in GiB. |
| 329 |
returned: when I(state=present) |
| 330 |
type: str |
| 331 |
sample: '100' |
| 332 |
virtual_name: |
| 333 |
description: The name of the virtual device (for example, ephemeral0). |
| 334 |
returned: when I(state=present) |
| 335 |
type: str |
| 336 |
sample: ephemeral0 |
| 337 |
classic_link_vpc_id: |
| 338 |
description: The ID of a ClassicLink-enabled VPC to link your EC2-Classic instances to. |
| 339 |
returned: when I(state=present) |
| 340 |
type: str |
| 341 |
classic_link_vpc_security_groups: |
| 342 |
description: The IDs of one or more security groups for the VPC specified in ClassicLinkVPCId. |
| 343 |
returned: when I(state=present) |
| 344 |
type: list |
| 345 |
sample: [] |
| 346 |
created_time: |
| 347 |
description: The creation date and time for the launch configuration. |
| 348 |
returned: when I(state=present) |
| 349 |
type: str |
| 350 |
sample: '2017-11-03 23:46:44.841000' |
| 351 |
delete_on_termination: |
| 352 |
description: Indicates whether the volume is deleted on instance termination. |
| 353 |
returned: when I(state=present) |
| 354 |
type: bool |
| 355 |
sample: true |
| 356 |
ebs_optimized: |
| 357 |
description: Indicates whether the instance is optimized for EBS I/O C(true) or not C(false). |
| 358 |
returned: when I(state=present) |
| 359 |
type: bool |
| 360 |
sample: false |
| 361 |
image_id: |
| 362 |
description: The ID of the Amazon Machine Image used by the launch configuration. |
| 363 |
returned: when I(state=present) |
| 364 |
type: str |
| 365 |
sample: ami-9be6f38c |
| 366 |
instance_monitoring: |
| 367 |
description: Indicates whether instances in this group are launched with detailed C(true) or basic C(false) monitoring. |
| 368 |
returned: when I(state=present) |
| 369 |
type: bool |
| 370 |
sample: true |
| 371 |
instance_profile_name: |
| 372 |
description: The name or Amazon Resource Name (ARN) of the instance profile associated with the IAM role for the instance. |
| 373 |
returned: when I(state=present) |
| 374 |
type: str |
| 375 |
sample: null |
| 376 |
instance_type: |
| 377 |
description: The instance type for the instances. |
| 378 |
returned: when I(state=present) |
| 379 |
type: str |
| 380 |
sample: t1.micro |
| 381 |
iops: |
| 382 |
description: The number of I/O operations per second (IOPS) to provision for the volume. |
| 383 |
returned: when I(state=present) |
| 384 |
type: int |
| 385 |
kernel_id: |
| 386 |
description: The ID of the kernel associated with the AMI. |
| 387 |
returned: when I(state=present) |
| 388 |
type: str |
| 389 |
sample: '' |
| 390 |
key_name: |
| 391 |
description: The name of the key pair. |
| 392 |
returned: when I(state=present) |
| 393 |
type: str |
| 394 |
sample: testkey |
| 395 |
launch_configuration_arn: |
| 396 |
description: The Amazon Resource Name (ARN) of the launch configuration. |
| 397 |
returned: when I(state=present) |
| 398 |
type: str |
| 399 |
sample: arn:aws:autoscaling:us-east-1:123456789012:launchConfiguration:888d9b58-d93a-40c4-90cf-759197a2621a:launchConfigurationName/launch_config_name |
| 400 |
member: |
| 401 |
description: "" |
| 402 |
returned: when I(state=present) |
| 403 |
type: str |
| 404 |
sample: "\n " |
| 405 |
name: |
| 406 |
description: The name of the launch configuration. |
| 407 |
returned: when I(state=present) |
| 408 |
type: str |
| 409 |
sample: launch_config_name |
| 410 |
ramdisk_id: |
| 411 |
description: The ID of the RAM disk associated with the AMI. |
| 412 |
returned: when I(state=present) |
| 413 |
type: str |
| 414 |
sample: '' |
| 415 |
security_groups: |
| 416 |
description: The security groups to associate with the instances. |
| 417 |
returned: when I(state=present) |
| 418 |
type: list |
| 419 |
sample: |
| 420 |
- sg-5e27db2f |
| 421 |
spot_price: |
| 422 |
description: The price to bid when launching Spot Instances. |
| 423 |
returned: when I(state=present) |
| 424 |
type: float |
| 425 |
use_block_device_types: |
| 426 |
description: Indicates whether to suppress a device mapping. |
| 427 |
returned: when I(state=present) |
| 428 |
type: bool |
| 429 |
sample: false |
| 430 |
user_data: |
| 431 |
description: The user data available to the instances. |
| 432 |
returned: when I(state=present) |
| 433 |
type: str |
| 434 |
sample: '' |
| 435 |
volume_type: |
| 436 |
description: The volume type (one of standard, io1, gp2). |
| 437 |
returned: when I(state=present) |
| 438 |
type: str |
| 439 |
sample: io1 |
| 440 |
security_groups: |
| 441 |
description: The security groups to associate with the instances. |
| 442 |
returned: when I(state=present) |
| 443 |
type: list |
| 444 |
sample: |
| 445 |
- sg-5e27db2f |
| 446 |
""" |
| 447 |
|
| 448 |
|
| 449 |
import traceback |
| 450 |
|
| 451 |
try: |
| 452 |
import botocore |
| 453 |
except ImportError: |
| 454 |
pass # Handled by AnsibleAWSModule |
| 455 |
|
| 456 |
from ansible.module_utils._text import to_text |
| 457 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 458 |
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict |
| 459 |
|
| 460 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import get_ec2_security_group_ids_from_names |
| 461 |
|
| 462 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 463 |
|
| 464 |
|
| 465 |
def create_block_device_meta(module, volume): |
| 466 |
if "snapshot" not in volume and "ephemeral" not in volume and "no_device" not in volume: |
| 467 |
if "volume_size" not in volume: |
| 468 |
module.fail_json(msg="Size must be specified when creating a new volume or modifying the root volume") |
| 469 |
if "snapshot" in volume: |
| 470 |
if volume.get("volume_type") == "io1" and "iops" not in volume: |
| 471 |
module.fail_json(msg="io1 volumes must have an iops value set") |
| 472 |
if "ephemeral" in volume: |
| 473 |
if "snapshot" in volume: |
| 474 |
module.fail_json(msg="Cannot set both ephemeral and snapshot") |
| 475 |
|
| 476 |
return_object = {} |
| 477 |
|
| 478 |
if "ephemeral" in volume: |
| 479 |
return_object["VirtualName"] = volume.get("ephemeral") |
| 480 |
|
| 481 |
if "device_name" in volume: |
| 482 |
return_object["DeviceName"] = volume.get("device_name") |
| 483 |
|
| 484 |
if "no_device" in volume: |
| 485 |
return_object["NoDevice"] = volume.get("no_device") |
| 486 |
|
| 487 |
if any( |
| 488 |
key in volume |
| 489 |
for key in [ |
| 490 |
"snapshot", |
| 491 |
"volume_size", |
| 492 |
"volume_type", |
| 493 |
"delete_on_termination", |
| 494 |
"iops", |
| 495 |
"throughput", |
| 496 |
"encrypted", |
| 497 |
] |
| 498 |
): |
| 499 |
return_object["Ebs"] = {} |
| 500 |
|
| 501 |
if "snapshot" in volume: |
| 502 |
return_object["Ebs"]["SnapshotId"] = volume.get("snapshot") |
| 503 |
|
| 504 |
if "volume_size" in volume: |
| 505 |
return_object["Ebs"]["VolumeSize"] = int(volume.get("volume_size", 0)) |
| 506 |
|
| 507 |
if "volume_type" in volume: |
| 508 |
return_object["Ebs"]["VolumeType"] = volume.get("volume_type") |
| 509 |
|
| 510 |
if "delete_on_termination" in volume: |
| 511 |
return_object["Ebs"]["DeleteOnTermination"] = volume.get("delete_on_termination", False) |
| 512 |
|
| 513 |
if "iops" in volume: |
| 514 |
return_object["Ebs"]["Iops"] = volume.get("iops") |
| 515 |
|
| 516 |
if "throughput" in volume: |
| 517 |
if volume.get("volume_type") != "gp3": |
| 518 |
module.fail_json(msg="The throughput parameter is supported only for GP3 volumes.") |
| 519 |
return_object["Ebs"]["Throughput"] = volume.get("throughput") |
| 520 |
|
| 521 |
if "encrypted" in volume: |
| 522 |
return_object["Ebs"]["Encrypted"] = volume.get("encrypted") |
| 523 |
|
| 524 |
return return_object |
| 525 |
|
| 526 |
|
| 527 |
def create_launch_config(connection, module): |
| 528 |
name = module.params.get("name") |
| 529 |
vpc_id = module.params.get("vpc_id") |
| 530 |
try: |
| 531 |
ec2_connection = module.client("ec2") |
| 532 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 533 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 534 |
try: |
| 535 |
security_groups = get_ec2_security_group_ids_from_names( |
| 536 |
module.params.get("security_groups"), ec2_connection, vpc_id=vpc_id |
| 537 |
) |
| 538 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 539 |
module.fail_json_aws(e, msg="Failed to get Security Group IDs") |
| 540 |
except ValueError: |
| 541 |
module.fail_json(msg="Failed to get Security Group IDs", exception=traceback.format_exc()) |
| 542 |
user_data = module.params.get("user_data") |
| 543 |
user_data_path = module.params.get("user_data_path") |
| 544 |
volumes = module.params["volumes"] |
| 545 |
instance_monitoring = module.params.get("instance_monitoring") |
| 546 |
assign_public_ip = module.params.get("assign_public_ip") |
| 547 |
instance_profile_name = module.params.get("instance_profile_name") |
| 548 |
ebs_optimized = module.params.get("ebs_optimized") |
| 549 |
classic_link_vpc_id = module.params.get("classic_link_vpc_id") |
| 550 |
classic_link_vpc_security_groups = module.params.get("classic_link_vpc_security_groups") |
| 551 |
|
| 552 |
block_device_mapping = [] |
| 553 |
|
| 554 |
convert_list = [ |
| 555 |
"image_id", |
| 556 |
"instance_type", |
| 557 |
"instance_type", |
| 558 |
"instance_id", |
| 559 |
"placement_tenancy", |
| 560 |
"key_name", |
| 561 |
"kernel_id", |
| 562 |
"ramdisk_id", |
| 563 |
"spot_price", |
| 564 |
] |
| 565 |
|
| 566 |
launch_config = snake_dict_to_camel_dict( |
| 567 |
dict((k.capitalize(), str(v)) for k, v in module.params.items() if v is not None and k in convert_list) |
| 568 |
) |
| 569 |
|
| 570 |
if user_data_path: |
| 571 |
try: |
| 572 |
with open(user_data_path, "r") as user_data_file: |
| 573 |
user_data = user_data_file.read() |
| 574 |
except IOError: |
| 575 |
module.fail_json(msg="Failed to open file for reading", exception=traceback.format_exc()) |
| 576 |
|
| 577 |
if volumes: |
| 578 |
for volume in volumes: |
| 579 |
if "device_name" not in volume: |
| 580 |
module.fail_json(msg="Device name must be set for volume") |
| 581 |
# Minimum volume size is 1GiB. We'll use volume size explicitly set to 0 to be a signal not to create this volume |
| 582 |
if "volume_size" not in volume or int(volume["volume_size"]) > 0: |
| 583 |
block_device_mapping.append(create_block_device_meta(module, volume)) |
| 584 |
|
| 585 |
try: |
| 586 |
launch_configs = connection.describe_launch_configurations(LaunchConfigurationNames=[name]).get( |
| 587 |
"LaunchConfigurations" |
| 588 |
) |
| 589 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 590 |
module.fail_json_aws(e, msg="Failed to describe launch configuration by name") |
| 591 |
|
| 592 |
changed = False |
| 593 |
result = {} |
| 594 |
|
| 595 |
launch_config["LaunchConfigurationName"] = name |
| 596 |
|
| 597 |
if security_groups is not None: |
| 598 |
launch_config["SecurityGroups"] = security_groups |
| 599 |
|
| 600 |
if classic_link_vpc_id is not None: |
| 601 |
launch_config["ClassicLinkVPCId"] = classic_link_vpc_id |
| 602 |
|
| 603 |
if instance_monitoring is not None: |
| 604 |
launch_config["InstanceMonitoring"] = {"Enabled": instance_monitoring} |
| 605 |
|
| 606 |
if classic_link_vpc_security_groups is not None: |
| 607 |
launch_config["ClassicLinkVPCSecurityGroups"] = classic_link_vpc_security_groups |
| 608 |
|
| 609 |
if block_device_mapping: |
| 610 |
launch_config["BlockDeviceMappings"] = block_device_mapping |
| 611 |
|
| 612 |
if instance_profile_name is not None: |
| 613 |
launch_config["IamInstanceProfile"] = instance_profile_name |
| 614 |
|
| 615 |
if assign_public_ip is not None: |
| 616 |
launch_config["AssociatePublicIpAddress"] = assign_public_ip |
| 617 |
|
| 618 |
if user_data is not None: |
| 619 |
launch_config["UserData"] = user_data |
| 620 |
|
| 621 |
if ebs_optimized is not None: |
| 622 |
launch_config["EbsOptimized"] = ebs_optimized |
| 623 |
|
| 624 |
if len(launch_configs) == 0: |
| 625 |
try: |
| 626 |
connection.create_launch_configuration(**launch_config) |
| 627 |
launch_configs = connection.describe_launch_configurations(LaunchConfigurationNames=[name]).get( |
| 628 |
"LaunchConfigurations" |
| 629 |
) |
| 630 |
changed = True |
| 631 |
if launch_configs: |
| 632 |
launch_config = launch_configs[0] |
| 633 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 634 |
module.fail_json_aws(e, msg="Failed to create launch configuration") |
| 635 |
|
| 636 |
result = dict( |
| 637 |
(k, v) |
| 638 |
for k, v in launch_config.items() |
| 639 |
if k not in ["Connection", "CreatedTime", "InstanceMonitoring", "BlockDeviceMappings"] |
| 640 |
) |
| 641 |
|
| 642 |
result["CreatedTime"] = to_text(launch_config.get("CreatedTime")) |
| 643 |
|
| 644 |
try: |
| 645 |
result["InstanceMonitoring"] = module.boolean(launch_config.get("InstanceMonitoring").get("Enabled")) |
| 646 |
except AttributeError: |
| 647 |
result["InstanceMonitoring"] = False |
| 648 |
|
| 649 |
result["BlockDeviceMappings"] = [] |
| 650 |
|
| 651 |
for block_device_mapping in launch_config.get("BlockDeviceMappings", []): |
| 652 |
result["BlockDeviceMappings"].append( |
| 653 |
dict( |
| 654 |
device_name=block_device_mapping.get("DeviceName"), virtual_name=block_device_mapping.get("VirtualName") |
| 655 |
) |
| 656 |
) |
| 657 |
if block_device_mapping.get("Ebs") is not None: |
| 658 |
result["BlockDeviceMappings"][-1]["ebs"] = dict( |
| 659 |
snapshot_id=block_device_mapping.get("Ebs").get("SnapshotId"), |
| 660 |
volume_size=block_device_mapping.get("Ebs").get("VolumeSize"), |
| 661 |
) |
| 662 |
|
| 663 |
if user_data_path: |
| 664 |
result["UserData"] = "hidden" # Otherwise, we dump binary to the user's terminal |
| 665 |
|
| 666 |
return_object = { |
| 667 |
"Name": result.get("LaunchConfigurationName"), |
| 668 |
"CreatedTime": result.get("CreatedTime"), |
| 669 |
"ImageId": result.get("ImageId"), |
| 670 |
"Arn": result.get("LaunchConfigurationARN"), |
| 671 |
"SecurityGroups": result.get("SecurityGroups"), |
| 672 |
"InstanceType": result.get("InstanceType"), |
| 673 |
"Result": result, |
| 674 |
} |
| 675 |
|
| 676 |
module.exit_json(changed=changed, **camel_dict_to_snake_dict(return_object)) |
| 677 |
|
| 678 |
|
| 679 |
def delete_launch_config(connection, module): |
| 680 |
try: |
| 681 |
name = module.params.get("name") |
| 682 |
launch_configs = connection.describe_launch_configurations(LaunchConfigurationNames=[name]).get( |
| 683 |
"LaunchConfigurations" |
| 684 |
) |
| 685 |
if launch_configs: |
| 686 |
connection.delete_launch_configuration( |
| 687 |
LaunchConfigurationName=launch_configs[0].get("LaunchConfigurationName") |
| 688 |
) |
| 689 |
module.exit_json(changed=True) |
| 690 |
else: |
| 691 |
module.exit_json(changed=False) |
| 692 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 693 |
module.fail_json_aws(e, msg="Failed to delete launch configuration") |
| 694 |
|
| 695 |
|
| 696 |
def main(): |
| 697 |
argument_spec = dict( |
| 698 |
name=dict(required=True), |
| 699 |
image_id=dict(), |
| 700 |
instance_id=dict(), |
| 701 |
key_name=dict(), |
| 702 |
security_groups=dict(default=[], type="list", elements="str"), |
| 703 |
user_data=dict(), |
| 704 |
user_data_path=dict(type="path"), |
| 705 |
kernel_id=dict(), |
| 706 |
volumes=dict(type="list", elements="dict"), |
| 707 |
instance_type=dict(), |
| 708 |
state=dict(default="present", choices=["present", "absent"]), |
| 709 |
spot_price=dict(type="float"), |
| 710 |
ramdisk_id=dict(), |
| 711 |
instance_profile_name=dict(), |
| 712 |
ebs_optimized=dict(default=False, type="bool"), |
| 713 |
instance_monitoring=dict(default=False, type="bool"), |
| 714 |
assign_public_ip=dict(type="bool"), |
| 715 |
classic_link_vpc_security_groups=dict(type="list", elements="str"), |
| 716 |
classic_link_vpc_id=dict(), |
| 717 |
vpc_id=dict(), |
| 718 |
placement_tenancy=dict(choices=["default", "dedicated"]), |
| 719 |
) |
| 720 |
|
| 721 |
module = AnsibleAWSModule( |
| 722 |
argument_spec=argument_spec, |
| 723 |
mutually_exclusive=[["user_data", "user_data_path"]], |
| 724 |
) |
| 725 |
|
| 726 |
try: |
| 727 |
connection = module.client("autoscaling") |
| 728 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 729 |
module.fail_json_aws(e, msg="unable to establish connection") |
| 730 |
|
| 731 |
state = module.params.get("state") |
| 732 |
|
| 733 |
if state == "present": |
| 734 |
create_launch_config(connection, module) |
| 735 |
elif state == "absent": |
| 736 |
delete_launch_config(connection, module) |
| 737 |
|
| 738 |
|
| 739 |
if __name__ == "__main__": |
| 740 |
main() |