| 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: ec2_ami |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Create or destroy an image (AMI) in EC2 |
| 12 |
description: |
| 13 |
- Registers or deregisters EC2 images. |
| 14 |
options: |
| 15 |
instance_id: |
| 16 |
description: |
| 17 |
- Instance ID to create the AMI from. |
| 18 |
type: str |
| 19 |
name: |
| 20 |
description: |
| 21 |
- The name of the new AMI. |
| 22 |
type: str |
| 23 |
architecture: |
| 24 |
description: |
| 25 |
- The target architecture of the image to register. |
| 26 |
default: "x86_64" |
| 27 |
type: str |
| 28 |
kernel_id: |
| 29 |
description: |
| 30 |
- The target kernel id of the image to register. |
| 31 |
type: str |
| 32 |
virtualization_type: |
| 33 |
description: |
| 34 |
- The virtualization type of the image to register. |
| 35 |
default: "hvm" |
| 36 |
type: str |
| 37 |
root_device_name: |
| 38 |
description: |
| 39 |
- The root device name of the image to register. |
| 40 |
type: str |
| 41 |
wait: |
| 42 |
description: |
| 43 |
- Wait for the AMI to be in state 'available' before returning. |
| 44 |
default: false |
| 45 |
type: bool |
| 46 |
wait_timeout: |
| 47 |
description: |
| 48 |
- How long before wait gives up, in seconds. |
| 49 |
default: 1200 |
| 50 |
type: int |
| 51 |
state: |
| 52 |
description: |
| 53 |
- Register or deregister an AMI. |
| 54 |
default: 'present' |
| 55 |
choices: [ "absent", "present" ] |
| 56 |
type: str |
| 57 |
description: |
| 58 |
description: |
| 59 |
- Human-readable string describing the contents and purpose of the AMI. |
| 60 |
type: str |
| 61 |
default: '' |
| 62 |
no_reboot: |
| 63 |
description: |
| 64 |
- Flag indicating that the bundling process should not attempt to shutdown the instance before bundling. If this flag is V(true), the |
| 65 |
responsibility of maintaining file system integrity is left to the owner of the instance. |
| 66 |
default: false |
| 67 |
type: bool |
| 68 |
image_id: |
| 69 |
description: |
| 70 |
- Image ID to be deregistered. |
| 71 |
type: str |
| 72 |
device_mapping: |
| 73 |
description: |
| 74 |
- List of device hashes/dictionaries with custom configurations (same block-device-mapping parameters). |
| 75 |
type: list |
| 76 |
elements: dict |
| 77 |
suboptions: |
| 78 |
device_name: |
| 79 |
type: str |
| 80 |
description: |
| 81 |
- The device name. For example V(/dev/sda). |
| 82 |
required: true |
| 83 |
virtual_name: |
| 84 |
type: str |
| 85 |
description: |
| 86 |
- The virtual name for the device. |
| 87 |
- See the AWS documentation for more detail U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_BlockDeviceMapping.html). |
| 88 |
no_device: |
| 89 |
type: bool |
| 90 |
description: |
| 91 |
- Suppresses the specified device included in the block device mapping of the AMI. |
| 92 |
volume_type: |
| 93 |
type: str |
| 94 |
description: The volume type. Defaults to V(gp2) when not set. |
| 95 |
delete_on_termination: |
| 96 |
type: bool |
| 97 |
description: Whether the device should be automatically deleted when the Instance is terminated. |
| 98 |
snapshot_id: |
| 99 |
type: str |
| 100 |
description: The ID of the Snapshot. |
| 101 |
iops: |
| 102 |
type: int |
| 103 |
description: When using O(device_mapping.volume_type=io1), this sets the number of IOPS provisioned for the volume. |
| 104 |
encrypted: |
| 105 |
type: bool |
| 106 |
description: Whether the volume should be encrypted. |
| 107 |
volume_size: |
| 108 |
aliases: ['size'] |
| 109 |
type: int |
| 110 |
description: The size of the volume (in GiB). |
| 111 |
delete_snapshot: |
| 112 |
description: |
| 113 |
- Delete snapshots when deregistering the AMI. |
| 114 |
default: false |
| 115 |
type: bool |
| 116 |
launch_permissions: |
| 117 |
description: |
| 118 |
- Launch permissions for the AMI. |
| 119 |
- You must pass all desired launch permissions if you wish to modify existing launch permissions (passing just groups will remove all users). |
| 120 |
required: false |
| 121 |
type: dict |
| 122 |
suboptions: |
| 123 |
user_ids: |
| 124 |
description: List of account IDs. |
| 125 |
type: list |
| 126 |
elements: str |
| 127 |
required: false |
| 128 |
group_names: |
| 129 |
description: List of group names. |
| 130 |
type: list |
| 131 |
elements: str |
| 132 |
required: false |
| 133 |
org_arns: |
| 134 |
description: List of The Amazon Resource Name(s) (ARN) of organization(s). |
| 135 |
type: list |
| 136 |
elements: str |
| 137 |
required: false |
| 138 |
version_added: 6.5.0 |
| 139 |
org_unit_arns: |
| 140 |
description: List of The Amazon Resource Name(s) (ARN) of an organizational unit(s) (OU). |
| 141 |
type: list |
| 142 |
elements: str |
| 143 |
required: false |
| 144 |
version_added: 6.5.0 |
| 145 |
image_location: |
| 146 |
description: |
| 147 |
- The S3 location of an image to use for the AMI. |
| 148 |
type: str |
| 149 |
enhanced_networking: |
| 150 |
description: |
| 151 |
- A boolean representing whether enhanced networking with ENA is enabled or not. |
| 152 |
type: bool |
| 153 |
billing_products: |
| 154 |
description: |
| 155 |
- A list of valid billing codes. To be used with valid accounts by AWS Marketplace vendors. |
| 156 |
type: list |
| 157 |
elements: str |
| 158 |
ramdisk_id: |
| 159 |
description: |
| 160 |
- The ID of the RAM disk. |
| 161 |
type: str |
| 162 |
sriov_net_support: |
| 163 |
description: |
| 164 |
- Set to simple to enable enhanced networking with the Intel 82599 Virtual Function interface for the AMI and any instances that you launch from the AMI. |
| 165 |
type: str |
| 166 |
boot_mode: |
| 167 |
description: |
| 168 |
- The boot mode of the AMI. |
| 169 |
- See the AWS documentation for more detail U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ami-boot.html). |
| 170 |
- Support for V(uefi-preferred) was added in release 8.2.0. |
| 171 |
type: str |
| 172 |
choices: ['legacy-bios', 'uefi', 'uefi-preferred'] |
| 173 |
version_added: 5.5.0 |
| 174 |
tpm_support: |
| 175 |
description: |
| 176 |
- Set to V(v2.0) to enable Trusted Platform Module (TPM) support. |
| 177 |
- If the image is configured for NitroTPM support, the value is V(v2.0). |
| 178 |
- Requires O(boot_mode) to be set to V(uefi). |
| 179 |
- Requires an instance type that is compatible with Nitro. |
| 180 |
- See the AWS documentation for more detail U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nitrotpm.html). |
| 181 |
type: str |
| 182 |
version_added: 5.5.0 |
| 183 |
uefi_data: |
| 184 |
description: |
| 185 |
- Base64 representation of the non-volatile UEFI variable store. |
| 186 |
- See the AWS documentation for more detail U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/uefi-secure-boot.html). |
| 187 |
type: str |
| 188 |
version_added: 5.5.0 |
| 189 |
author: |
| 190 |
- "Evan Duffield (@scicoin-project) <[email protected]>" |
| 191 |
- "Constantin Bugneac (@Constantin07) <[email protected]>" |
| 192 |
- "Ross Williams (@gunzy83) <[email protected]>" |
| 193 |
- "Willem van Ketwich (@wilvk) <[email protected]>" |
| 194 |
extends_documentation_fragment: |
| 195 |
- amazon.aws.common.modules |
| 196 |
- amazon.aws.region.modules |
| 197 |
- amazon.aws.tags |
| 198 |
- amazon.aws.boto3 |
| 199 |
""" |
| 200 |
|
| 201 |
# Thank you to iAcquire for sponsoring development of this module. |
| 202 |
|
| 203 |
EXAMPLES = r""" |
| 204 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 205 |
|
| 206 |
- name: Basic AMI Creation |
| 207 |
amazon.aws.ec2_ami: |
| 208 |
instance_id: i-xxxxxx |
| 209 |
wait: true |
| 210 |
name: newtest |
| 211 |
tags: |
| 212 |
Name: newtest |
| 213 |
Service: TestService |
| 214 |
|
| 215 |
- name: Basic AMI Creation, without waiting |
| 216 |
amazon.aws.ec2_ami: |
| 217 |
instance_id: i-xxxxxx |
| 218 |
wait: false |
| 219 |
name: newtest |
| 220 |
|
| 221 |
- name: AMI Registration from EBS Snapshot |
| 222 |
amazon.aws.ec2_ami: |
| 223 |
name: newtest |
| 224 |
state: present |
| 225 |
architecture: x86_64 |
| 226 |
virtualization_type: hvm |
| 227 |
root_device_name: /dev/xvda |
| 228 |
device_mapping: |
| 229 |
- device_name: /dev/xvda |
| 230 |
volume_size: 8 |
| 231 |
snapshot_id: snap-xxxxxxxx |
| 232 |
delete_on_termination: true |
| 233 |
volume_type: gp2 |
| 234 |
|
| 235 |
- name: AMI Creation, with a custom root-device size and another EBS attached |
| 236 |
amazon.aws.ec2_ami: |
| 237 |
instance_id: i-xxxxxx |
| 238 |
name: newtest |
| 239 |
device_mapping: |
| 240 |
- device_name: /dev/sda1 |
| 241 |
size: XXX |
| 242 |
delete_on_termination: true |
| 243 |
volume_type: gp2 |
| 244 |
- device_name: /dev/sdb |
| 245 |
size: YYY |
| 246 |
delete_on_termination: false |
| 247 |
volume_type: gp2 |
| 248 |
|
| 249 |
- name: AMI Creation, excluding a volume attached at /dev/sdb |
| 250 |
amazon.aws.ec2_ami: |
| 251 |
instance_id: i-xxxxxx |
| 252 |
name: newtest |
| 253 |
device_mapping: |
| 254 |
- device_name: /dev/sda1 |
| 255 |
size: XXX |
| 256 |
delete_on_termination: true |
| 257 |
volume_type: gp2 |
| 258 |
- device_name: /dev/sdb |
| 259 |
no_device: true |
| 260 |
|
| 261 |
- name: AMI Creation with boot_mode and tpm_support |
| 262 |
amazon.aws.ec2_ami: |
| 263 |
name: newtest |
| 264 |
state: present |
| 265 |
architecture: x86_64 |
| 266 |
virtualization_type: hvm |
| 267 |
root_device_name: /dev/sda1 |
| 268 |
device_mapping: |
| 269 |
- device_name: /dev/sda1 |
| 270 |
snapshot_id: "{{ snapshot_id }}" |
| 271 |
wait: true |
| 272 |
region: us-east-1 |
| 273 |
boot_mode: uefi |
| 274 |
uefi_data: data_file.bin |
| 275 |
tpm_support: v2.0 |
| 276 |
|
| 277 |
- name: Deregister/Delete AMI (keep associated snapshots) |
| 278 |
amazon.aws.ec2_ami: |
| 279 |
image_id: "{{ instance.image_id }}" |
| 280 |
delete_snapshot: false |
| 281 |
state: absent |
| 282 |
|
| 283 |
- name: Deregister AMI (delete associated snapshots too) |
| 284 |
amazon.aws.ec2_ami: |
| 285 |
image_id: "{{ instance.image_id }}" |
| 286 |
delete_snapshot: true |
| 287 |
state: absent |
| 288 |
|
| 289 |
- name: Update AMI Launch Permissions, making it public |
| 290 |
amazon.aws.ec2_ami: |
| 291 |
image_id: "{{ instance.image_id }}" |
| 292 |
state: present |
| 293 |
launch_permissions: |
| 294 |
group_names: ['all'] |
| 295 |
|
| 296 |
- name: Allow AMI to be launched by another account |
| 297 |
amazon.aws.ec2_ami: |
| 298 |
image_id: "{{ instance.image_id }}" |
| 299 |
state: present |
| 300 |
launch_permissions: |
| 301 |
user_ids: ['123456789012'] |
| 302 |
|
| 303 |
- name: Update AMI Launch Permissions, share AMI across an Organization and Organizational Units |
| 304 |
amazon.aws.ec2_ami: |
| 305 |
image_id: "{{ instance.image_id }}" |
| 306 |
state: present |
| 307 |
launch_permissions: |
| 308 |
org_arns: ['arn:aws:organizations::123456789012:organization/o-123ab4cdef'] |
| 309 |
org_unit_arns: ['arn:aws:organizations::123456789012:ou/o-123example/ou-1234-5example'] |
| 310 |
""" |
| 311 |
|
| 312 |
RETURN = r""" |
| 313 |
architecture: |
| 314 |
description: Architecture of image. |
| 315 |
returned: when AMI is created or already exists |
| 316 |
type: str |
| 317 |
sample: "x86_64" |
| 318 |
block_device_mapping: |
| 319 |
description: Block device mapping associated with image. |
| 320 |
returned: when AMI is created or already exists |
| 321 |
type: dict |
| 322 |
sample: { |
| 323 |
"/dev/sda1": { |
| 324 |
"delete_on_termination": true, |
| 325 |
"encrypted": false, |
| 326 |
"size": 10, |
| 327 |
"snapshot_id": "snap-1a03b80e7", |
| 328 |
"volume_type": "standard" |
| 329 |
} |
| 330 |
} |
| 331 |
creationDate: |
| 332 |
description: Creation date of image. |
| 333 |
returned: when AMI is created or already exists |
| 334 |
type: str |
| 335 |
sample: "2015-10-15T22:43:44.000Z" |
| 336 |
description: |
| 337 |
description: Description of image. |
| 338 |
returned: when AMI is created or already exists |
| 339 |
type: str |
| 340 |
sample: "nat-server" |
| 341 |
enhanced_networking: |
| 342 |
description: Specifies whether enhanced networking with ENA is enabled. |
| 343 |
returned: when AMI is created or already exists |
| 344 |
type: bool |
| 345 |
sample: true |
| 346 |
hypervisor: |
| 347 |
description: Type of hypervisor. |
| 348 |
returned: when AMI is created or already exists |
| 349 |
type: str |
| 350 |
sample: "xen" |
| 351 |
image_id: |
| 352 |
description: ID of the image. |
| 353 |
returned: when AMI is created or already exists |
| 354 |
type: str |
| 355 |
sample: "ami-1234abcd" |
| 356 |
image_owner_alias: |
| 357 |
description: The owner alias ( amazon | aws-marketplace). |
| 358 |
returned: when AMI is created or already exists |
| 359 |
type: str |
| 360 |
sample: "amazon" |
| 361 |
image_type: |
| 362 |
description: Type of image. |
| 363 |
returned: when AMI is created or already exists |
| 364 |
type: str |
| 365 |
sample: "machine" |
| 366 |
is_public: |
| 367 |
description: Whether image is public. |
| 368 |
returned: when AMI is created or already exists |
| 369 |
type: bool |
| 370 |
sample: false |
| 371 |
kernel_id: |
| 372 |
description: The kernel associated with the image, if any. Only applicable for machine images. |
| 373 |
returned: when AMI is created or already exists |
| 374 |
type: str |
| 375 |
sample: "aki-88aa75e1" |
| 376 |
launch_permission: |
| 377 |
description: Permissions allowing other accounts to access the AMI. |
| 378 |
returned: when AMI is created or already exists |
| 379 |
type: list |
| 380 |
sample: |
| 381 |
- group: "all" |
| 382 |
location: |
| 383 |
description: Location of image. |
| 384 |
returned: when AMI is created or already exists |
| 385 |
type: str |
| 386 |
sample: "123456789012/nat-server" |
| 387 |
name: |
| 388 |
description: AMI name of image. |
| 389 |
returned: when AMI is created or already exists |
| 390 |
type: str |
| 391 |
sample: "nat-server" |
| 392 |
ownerId: |
| 393 |
description: Owner of image. |
| 394 |
returned: when AMI is created or already exists |
| 395 |
type: str |
| 396 |
sample: "123456789012" |
| 397 |
platform: |
| 398 |
description: Platform of image. |
| 399 |
returned: when AMI is created or already exists |
| 400 |
type: str |
| 401 |
sample: "Windows" |
| 402 |
product_codes: |
| 403 |
description: Any product codes associated with the AMI. |
| 404 |
returned: when AMI is created or already exists |
| 405 |
type: list |
| 406 |
sample: [] |
| 407 |
ramdisk_id: |
| 408 |
description: The RAM disk associated with the image, if any. Only applicable for machine images. |
| 409 |
returned: when AMI is created or already exists |
| 410 |
type: str |
| 411 |
sample: null |
| 412 |
root_device_name: |
| 413 |
description: Root device name of image. |
| 414 |
returned: when AMI is created or already exists |
| 415 |
type: str |
| 416 |
sample: "/dev/sda1" |
| 417 |
root_device_type: |
| 418 |
description: Root device type of image. |
| 419 |
returned: when AMI is created or already exists |
| 420 |
type: str |
| 421 |
sample: "ebs" |
| 422 |
sriov_net_support: |
| 423 |
description: Specifies whether enhanced networking with the Intel 82599 Virtual Function interface is enabled. |
| 424 |
returned: when AMI is created or already exists |
| 425 |
type: str |
| 426 |
sample: "simple" |
| 427 |
state: |
| 428 |
description: State of image. |
| 429 |
returned: when AMI is created or already exists |
| 430 |
type: str |
| 431 |
sample: "available" |
| 432 |
state_reason: |
| 433 |
description: The reason for the state change. |
| 434 |
returned: when AMI is created or already exists |
| 435 |
type: dict |
| 436 |
sample: { |
| 437 |
'Code': 'string', |
| 438 |
'Message': 'string' |
| 439 |
} |
| 440 |
tags: |
| 441 |
description: A dictionary of tags assigned to image. |
| 442 |
returned: when AMI is created or already exists |
| 443 |
type: dict |
| 444 |
sample: { |
| 445 |
"Env": "devel", |
| 446 |
"Name": "nat-server" |
| 447 |
} |
| 448 |
virtualization_type: |
| 449 |
description: Image virtualization type. |
| 450 |
returned: when AMI is created or already exists |
| 451 |
type: str |
| 452 |
sample: "hvm" |
| 453 |
snapshots_deleted: |
| 454 |
description: A list of snapshot ids deleted after deregistering image. |
| 455 |
returned: after AMI is deregistered, if I(delete_snapshot=true) |
| 456 |
type: list |
| 457 |
sample: [ |
| 458 |
"snap-fbcccb8f", |
| 459 |
"snap-cfe7cdb4" |
| 460 |
] |
| 461 |
""" |
| 462 |
|
| 463 |
import time |
| 464 |
|
| 465 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 466 |
|
| 467 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 468 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import add_ec2_tags |
| 469 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import create_image |
| 470 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import delete_snapshot |
| 471 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import deregister_image |
| 472 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_image_attribute |
| 473 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_images |
| 474 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags |
| 475 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import modify_image_attribute |
| 476 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import register_image |
| 477 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 478 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 479 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_specifications |
| 480 |
from ansible_collections.amazon.aws.plugins.module_utils.waiters import wait_for_resource_state |
| 481 |
|
| 482 |
|
| 483 |
class Ec2AmiFailure(Exception): |
| 484 |
def __init__(self, message=None, original_e=None): |
| 485 |
super().__init__(message) |
| 486 |
self.original_e = original_e |
| 487 |
self.message = message |
| 488 |
|
| 489 |
|
| 490 |
def get_block_device_mapping(image): |
| 491 |
bdm_dict = {} |
| 492 |
if image is not None and image.get("block_device_mappings") is not None: |
| 493 |
bdm = image.get("block_device_mappings") |
| 494 |
for device in bdm: |
| 495 |
device_name = device.get("device_name") |
| 496 |
if "ebs" in device: |
| 497 |
ebs = device.get("ebs") |
| 498 |
bdm_dict_item = { |
| 499 |
"size": ebs.get("volume_size"), |
| 500 |
"snapshot_id": ebs.get("snapshot_id"), |
| 501 |
"volume_type": ebs.get("volume_type"), |
| 502 |
"encrypted": ebs.get("encrypted"), |
| 503 |
"delete_on_termination": ebs.get("delete_on_termination"), |
| 504 |
} |
| 505 |
elif "virtual_name" in device: |
| 506 |
bdm_dict_item = dict(virtual_name=device["virtual_name"]) |
| 507 |
bdm_dict[device_name] = bdm_dict_item |
| 508 |
return bdm_dict |
| 509 |
|
| 510 |
|
| 511 |
def get_ami_info(camel_image): |
| 512 |
image = camel_dict_to_snake_dict(camel_image) |
| 513 |
return dict( |
| 514 |
image_id=image.get("image_id"), |
| 515 |
state=image.get("state"), |
| 516 |
architecture=image.get("architecture"), |
| 517 |
block_device_mapping=get_block_device_mapping(image), |
| 518 |
creationDate=image.get("creation_date"), |
| 519 |
description=image.get("description"), |
| 520 |
hypervisor=image.get("hypervisor"), |
| 521 |
is_public=image.get("public"), |
| 522 |
location=image.get("image_location"), |
| 523 |
ownerId=image.get("owner_id"), |
| 524 |
root_device_name=image.get("root_device_name"), |
| 525 |
root_device_type=image.get("root_device_type"), |
| 526 |
virtualization_type=image.get("virtualization_type"), |
| 527 |
name=image.get("name"), |
| 528 |
tags=boto3_tag_list_to_ansible_dict(image.get("tags")), |
| 529 |
platform=image.get("platform"), |
| 530 |
enhanced_networking=image.get("ena_support"), |
| 531 |
image_owner_alias=image.get("image_owner_alias"), |
| 532 |
image_type=image.get("image_type"), |
| 533 |
kernel_id=image.get("kernel_id"), |
| 534 |
product_codes=image.get("product_codes"), |
| 535 |
ramdisk_id=image.get("ramdisk_id"), |
| 536 |
sriov_net_support=image.get("sriov_net_support"), |
| 537 |
state_reason=image.get("state_reason"), |
| 538 |
launch_permissions=image.get("launch_permissions"), |
| 539 |
) |
| 540 |
|
| 541 |
|
| 542 |
def get_image_by_id(connection, image_id): |
| 543 |
try: |
| 544 |
images = describe_images(connection, ImageIds=[image_id]) |
| 545 |
except AnsibleEC2Error as e: |
| 546 |
raise Ec2AmiFailure("Error retrieving image by image_id", e) |
| 547 |
|
| 548 |
if len(images) == 0: |
| 549 |
return None |
| 550 |
|
| 551 |
if len(images) > 1: |
| 552 |
raise Ec2AmiFailure(f"Invalid number of instances ({str(len(images))}) found for image_id: {image_id}.") |
| 553 |
|
| 554 |
result = images[0] |
| 555 |
try: |
| 556 |
image_attribue = describe_image_attribute(connection, attribute="launchPermission", image_id=image_id) |
| 557 |
if image_attribue: |
| 558 |
result["LaunchPermissions"] = image_attribue["LaunchPermissions"] |
| 559 |
image_attribue = describe_image_attribute(connection, attribute="productCodes", image_id=image_id) |
| 560 |
if image_attribue: |
| 561 |
result["ProductCodes"] = image_attribue["ProductCodes"] |
| 562 |
except AnsibleEC2Error as e: |
| 563 |
raise Ec2AmiFailure(f"Error retrieving image attributes for image {image_id}", e) |
| 564 |
return result |
| 565 |
|
| 566 |
|
| 567 |
def rename_item_if_exists(dict_object, attribute, new_attribute, child_node=None, attribute_type=None): |
| 568 |
new_item = dict_object.get(attribute) |
| 569 |
if new_item is not None: |
| 570 |
if attribute_type is not None: |
| 571 |
new_item = attribute_type(new_item) |
| 572 |
if child_node is None: |
| 573 |
dict_object[new_attribute] = new_item |
| 574 |
else: |
| 575 |
dict_object[child_node][new_attribute] = new_item |
| 576 |
dict_object.pop(attribute) |
| 577 |
return dict_object |
| 578 |
|
| 579 |
|
| 580 |
def validate_params( |
| 581 |
module, |
| 582 |
image_id=None, |
| 583 |
instance_id=None, |
| 584 |
name=None, |
| 585 |
state=None, |
| 586 |
tpm_support=None, |
| 587 |
uefi_data=None, |
| 588 |
boot_mode=None, |
| 589 |
device_mapping=None, |
| 590 |
**_, |
| 591 |
): |
| 592 |
# Using a required_one_of=[['name', 'image_id']] overrides the message that should be provided by |
| 593 |
# the required_if for state=absent, so check manually instead |
| 594 |
if not (image_id or name): |
| 595 |
module.fail_json("one of the following is required: name, image_id") |
| 596 |
|
| 597 |
if tpm_support and boot_mode != "uefi": |
| 598 |
module.fail_json("To specify 'tpm_support', 'boot_mode' must be 'uefi'.") |
| 599 |
|
| 600 |
if state == "present" and not image_id and not (instance_id or device_mapping): |
| 601 |
module.fail_json( |
| 602 |
"The parameters instance_id or device_mapping (register from EBS snapshot) are required for a new image." |
| 603 |
) |
| 604 |
|
| 605 |
|
| 606 |
class DeregisterImage: |
| 607 |
@staticmethod |
| 608 |
def do_check_mode(module, connection, image_id): |
| 609 |
image = get_image_by_id(connection, image_id) |
| 610 |
|
| 611 |
if image is None: |
| 612 |
module.exit_json(changed=False) |
| 613 |
|
| 614 |
if "ImageId" in image: |
| 615 |
module.exit_json(changed=True, msg="Would have deregistered AMI if not in check mode.") |
| 616 |
else: |
| 617 |
module.exit_json(msg=f"Image {image_id} has already been deregistered.", changed=False) |
| 618 |
|
| 619 |
@staticmethod |
| 620 |
def defer_purge_snapshots(image): |
| 621 |
def purge_snapshots(connection): |
| 622 |
try: |
| 623 |
for mapping in image.get("BlockDeviceMappings") or []: |
| 624 |
snapshot_id = mapping.get("Ebs", {}).get("SnapshotId") |
| 625 |
if snapshot_id is None: |
| 626 |
continue |
| 627 |
delete_snapshot(connection, snapshot_id) |
| 628 |
yield snapshot_id |
| 629 |
except AnsibleEC2Error as e: |
| 630 |
raise Ec2AmiFailure("Failed to delete snapshot.", e) |
| 631 |
|
| 632 |
return purge_snapshots |
| 633 |
|
| 634 |
@staticmethod |
| 635 |
def timeout(connection, image_id, wait_timeout): |
| 636 |
image = get_image_by_id(connection, image_id) |
| 637 |
wait_till = time.time() + wait_timeout |
| 638 |
|
| 639 |
while wait_till > time.time() and image is not None: |
| 640 |
image = get_image_by_id(connection, image_id) |
| 641 |
time.sleep(3) |
| 642 |
|
| 643 |
if wait_till <= time.time(): |
| 644 |
raise Ec2AmiFailure("Timed out waiting for image to be deregistered.") |
| 645 |
|
| 646 |
@classmethod |
| 647 |
def do(cls, module, connection, image_id): |
| 648 |
"""Entry point to deregister an image""" |
| 649 |
wait = module.params.get("wait") |
| 650 |
wait_timeout = module.params.get("wait_timeout") |
| 651 |
image = get_image_by_id(connection, image_id) |
| 652 |
|
| 653 |
if image is None: |
| 654 |
module.exit_json(changed=False) |
| 655 |
|
| 656 |
# Get all associated snapshot ids before deregistering image otherwise this information becomes unavailable. |
| 657 |
purge_snapshots = cls.defer_purge_snapshots(image) |
| 658 |
|
| 659 |
# When trying to re-deregister an already deregistered image it doesn't raise an exception, it just returns an object without image attributes. |
| 660 |
if "ImageId" in image: |
| 661 |
try: |
| 662 |
deregister_image(connection, image_id) |
| 663 |
except AnsibleEC2Error as e: |
| 664 |
raise Ec2AmiFailure("Error deregistering image", e) |
| 665 |
else: |
| 666 |
module.exit_json(msg=f"Image {image_id} has already been deregistered.", changed=False) |
| 667 |
|
| 668 |
if wait: |
| 669 |
cls.timeout(connection, image_id, wait_timeout) |
| 670 |
|
| 671 |
exit_params = {"msg": "AMI deregister operation complete.", "changed": True} |
| 672 |
|
| 673 |
if module.params.get("delete_snapshot"): |
| 674 |
exit_params["snapshots_deleted"] = list(purge_snapshots(connection)) |
| 675 |
|
| 676 |
module.exit_json(**exit_params) |
| 677 |
|
| 678 |
|
| 679 |
class UpdateImage: |
| 680 |
@staticmethod |
| 681 |
def set_launch_permission(connection, image, launch_permissions, check_mode): |
| 682 |
if launch_permissions is None: |
| 683 |
return False |
| 684 |
|
| 685 |
current_permissions = image["LaunchPermissions"] |
| 686 |
|
| 687 |
current_users = set(permission["UserId"] for permission in current_permissions if "UserId" in permission) |
| 688 |
desired_users = set(str(user_id) for user_id in launch_permissions.get("user_ids", [])) |
| 689 |
current_groups = set(permission["Group"] for permission in current_permissions if "Group" in permission) |
| 690 |
desired_groups = set(launch_permissions.get("group_names", [])) |
| 691 |
current_org_arns = set( |
| 692 |
permission["OrganizationArn"] for permission in current_permissions if "OrganizationArn" in permission |
| 693 |
) |
| 694 |
desired_org_arns = set(str(org_arn) for org_arn in launch_permissions.get("org_arns", [])) |
| 695 |
current_org_unit_arns = set( |
| 696 |
permission["OrganizationalUnitArn"] |
| 697 |
for permission in current_permissions |
| 698 |
if "OrganizationalUnitArn" in permission |
| 699 |
) |
| 700 |
desired_org_unit_arns = set(str(org_unit_arn) for org_unit_arn in launch_permissions.get("org_unit_arns", [])) |
| 701 |
|
| 702 |
to_add_users = desired_users - current_users |
| 703 |
to_remove_users = current_users - desired_users |
| 704 |
to_add_groups = desired_groups - current_groups |
| 705 |
to_remove_groups = current_groups - desired_groups |
| 706 |
to_add_org_arns = desired_org_arns - current_org_arns |
| 707 |
to_remove_org_arns = current_org_arns - desired_org_arns |
| 708 |
to_add_org_unit_arns = desired_org_unit_arns - current_org_unit_arns |
| 709 |
to_remove_org_unit_arns = current_org_unit_arns - desired_org_unit_arns |
| 710 |
|
| 711 |
to_add = ( |
| 712 |
[dict(Group=group) for group in sorted(to_add_groups)] |
| 713 |
+ [dict(UserId=user_id) for user_id in sorted(to_add_users)] |
| 714 |
+ [dict(OrganizationArn=org_arn) for org_arn in sorted(to_add_org_arns)] |
| 715 |
+ [dict(OrganizationalUnitArn=org_unit_arn) for org_unit_arn in sorted(to_add_org_unit_arns)] |
| 716 |
) |
| 717 |
|
| 718 |
to_remove = ( |
| 719 |
[dict(Group=group) for group in sorted(to_remove_groups)] |
| 720 |
+ [dict(UserId=user_id) for user_id in sorted(to_remove_users)] |
| 721 |
+ [dict(OrganizationArn=org_arn) for org_arn in sorted(to_remove_org_arns)] |
| 722 |
+ [dict(OrganizationalUnitArn=org_unit_arn) for org_unit_arn in sorted(to_remove_org_unit_arns)] |
| 723 |
) |
| 724 |
|
| 725 |
if not (to_add or to_remove): |
| 726 |
return False |
| 727 |
|
| 728 |
try: |
| 729 |
if not check_mode: |
| 730 |
changed = modify_image_attribute( |
| 731 |
connection, |
| 732 |
image_id=image["ImageId"], |
| 733 |
Attribute="launchPermission", |
| 734 |
LaunchPermission=dict(Add=to_add, Remove=to_remove), |
| 735 |
) |
| 736 |
changed = True |
| 737 |
except AnsibleEC2Error as e: |
| 738 |
raise Ec2AmiFailure(f"Error updating launch permissions of image {image['ImageId']}", e) |
| 739 |
return changed |
| 740 |
|
| 741 |
@staticmethod |
| 742 |
def set_tags(connection, module, image_id, tags, purge_tags): |
| 743 |
if not tags: |
| 744 |
return False |
| 745 |
|
| 746 |
return ensure_ec2_tags(connection, module, image_id, tags=tags, purge_tags=purge_tags) |
| 747 |
|
| 748 |
@staticmethod |
| 749 |
def set_description(connection, module, image, description): |
| 750 |
if not description: |
| 751 |
return False |
| 752 |
|
| 753 |
if description == image["Description"]: |
| 754 |
return False |
| 755 |
|
| 756 |
try: |
| 757 |
if not module.check_mode: |
| 758 |
modify_image_attribute( |
| 759 |
connection, |
| 760 |
image_id=image["ImageId"], |
| 761 |
Attribute="Description", |
| 762 |
Description={"Value": description}, |
| 763 |
) |
| 764 |
return True |
| 765 |
except AnsibleEC2Error as e: |
| 766 |
raise Ec2AmiFailure(f"Error setting description for image {image['ImageId']}", e) |
| 767 |
|
| 768 |
@classmethod |
| 769 |
def do(cls, module, connection, image_id): |
| 770 |
"""Entry point to update an image""" |
| 771 |
launch_permissions = module.params.get("launch_permissions") |
| 772 |
# remove any keys with value=None |
| 773 |
if launch_permissions: |
| 774 |
launch_permissions = {k: v for k, v in launch_permissions.items() if v is not None} |
| 775 |
|
| 776 |
image = get_image_by_id(connection, image_id) |
| 777 |
if image is None: |
| 778 |
raise Ec2AmiFailure(f"Image {image_id} does not exist") |
| 779 |
|
| 780 |
changed = False |
| 781 |
changed |= cls.set_launch_permission(connection, image, launch_permissions, module.check_mode) |
| 782 |
changed |= cls.set_tags(connection, module, image_id, module.params["tags"], module.params["purge_tags"]) |
| 783 |
changed |= cls.set_description(connection, module, image, module.params["description"]) |
| 784 |
|
| 785 |
if changed and module.check_mode: |
| 786 |
module.exit_json(changed=True, msg="Would have updated AMI if not in check mode.") |
| 787 |
elif changed: |
| 788 |
module.exit_json(msg="AMI updated.", changed=True, **get_ami_info(get_image_by_id(connection, image_id))) |
| 789 |
else: |
| 790 |
module.exit_json(msg="AMI not updated.", changed=False, **get_ami_info(image)) |
| 791 |
|
| 792 |
|
| 793 |
class CreateImage: |
| 794 |
@staticmethod |
| 795 |
def do_check_mode(module, connection, _image_id): |
| 796 |
images = describe_images(connection, Filters=[{"Name": "name", "Values": [str(module.params["name"])]}]) |
| 797 |
if not images: |
| 798 |
module.exit_json(changed=True, msg="Would have created a AMI if not in check mode.") |
| 799 |
else: |
| 800 |
module.exit_json(changed=False, msg="Error registering image: AMI name is already in use by another AMI") |
| 801 |
|
| 802 |
@staticmethod |
| 803 |
def wait(connection, module, image_id): |
| 804 |
if module.params.get("wait") and module.params.get("wait_timeout"): |
| 805 |
delay = 15 |
| 806 |
max_attempts = module.params.get("wait_timeout") // delay |
| 807 |
wait_for_resource_state( |
| 808 |
connection, module, "image_available", delay=delay, max_attempts=max_attempts, ImageIds=[image_id] |
| 809 |
) |
| 810 |
|
| 811 |
@staticmethod |
| 812 |
def set_tags(connection, module, tags, image_id): |
| 813 |
if not tags: |
| 814 |
return |
| 815 |
|
| 816 |
image_info = get_image_by_id(connection, image_id) |
| 817 |
add_ec2_tags(connection, module, image_id, module.params["tags"]) |
| 818 |
if image_info and image_info.get("BlockDeviceMappings"): |
| 819 |
for mapping in image_info.get("BlockDeviceMappings"): |
| 820 |
# We can only tag Ebs volumes |
| 821 |
if "Ebs" not in mapping: |
| 822 |
continue |
| 823 |
add_ec2_tags(connection, module, mapping.get("Ebs").get("SnapshotId"), tags) |
| 824 |
|
| 825 |
@staticmethod |
| 826 |
def set_launch_permissions(connection, launch_permissions, image_id): |
| 827 |
if not launch_permissions: |
| 828 |
return |
| 829 |
# remove any keys with value=None |
| 830 |
launch_permissions = {k: v for k, v in launch_permissions.items() if v is not None} |
| 831 |
try: |
| 832 |
params = {"Attribute": "LaunchPermission", "LaunchPermission": {"Add": []}} |
| 833 |
for group_name in launch_permissions.get("group_names", []): |
| 834 |
params["LaunchPermission"]["Add"].append(dict(Group=group_name)) |
| 835 |
for user_id in launch_permissions.get("user_ids", []): |
| 836 |
params["LaunchPermission"]["Add"].append(dict(UserId=str(user_id))) |
| 837 |
for org_arn in launch_permissions.get("org_arns", []): |
| 838 |
params["LaunchPermission"]["Add"].append(dict(OrganizationArn=org_arn)) |
| 839 |
for org_unit_arn in launch_permissions.get("org_unit_arns", []): |
| 840 |
params["LaunchPermission"]["Add"].append(dict(OrganizationalUnitArn=org_unit_arn)) |
| 841 |
if params["LaunchPermission"]["Add"]: |
| 842 |
modify_image_attribute(connection, image_id=image_id, **params) |
| 843 |
except AnsibleEC2Error as e: |
| 844 |
raise Ec2AmiFailure(f"Error setting launch permissions for image {image_id}", e) |
| 845 |
|
| 846 |
@staticmethod |
| 847 |
def create_or_register(create_image_parameters): |
| 848 |
create_from_instance = "InstanceId" in create_image_parameters |
| 849 |
func = create_image if create_from_instance else register_image |
| 850 |
return func |
| 851 |
|
| 852 |
@staticmethod |
| 853 |
def build_block_device_mapping(device_mapping): |
| 854 |
# Remove empty values injected by using options |
| 855 |
block_device_mapping = [] |
| 856 |
for device in device_mapping: |
| 857 |
device = {k: v for k, v in device.items() if v is not None} |
| 858 |
device["Ebs"] = {} |
| 859 |
rename_item_if_exists(device, "delete_on_termination", "DeleteOnTermination", "Ebs") |
| 860 |
rename_item_if_exists(device, "device_name", "DeviceName") |
| 861 |
rename_item_if_exists(device, "encrypted", "Encrypted", "Ebs") |
| 862 |
rename_item_if_exists(device, "iops", "Iops", "Ebs") |
| 863 |
rename_item_if_exists(device, "no_device", "NoDevice") |
| 864 |
rename_item_if_exists(device, "size", "VolumeSize", "Ebs", attribute_type=int) |
| 865 |
rename_item_if_exists(device, "snapshot_id", "SnapshotId", "Ebs") |
| 866 |
rename_item_if_exists(device, "virtual_name", "VirtualName") |
| 867 |
rename_item_if_exists(device, "volume_size", "VolumeSize", "Ebs", attribute_type=int) |
| 868 |
rename_item_if_exists(device, "volume_type", "VolumeType", "Ebs") |
| 869 |
|
| 870 |
# The NoDevice parameter in Boto3 is a string. Empty string omits the device from block device mapping |
| 871 |
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.create_image |
| 872 |
if "NoDevice" in device: |
| 873 |
if device["NoDevice"] is True: |
| 874 |
device["NoDevice"] = "" |
| 875 |
else: |
| 876 |
del device["NoDevice"] |
| 877 |
block_device_mapping.append(device) |
| 878 |
return block_device_mapping |
| 879 |
|
| 880 |
@staticmethod |
| 881 |
def build_create_image_parameters(**kwargs): |
| 882 |
architecture = kwargs.get("architecture") |
| 883 |
billing_products = kwargs.get("billing_products") |
| 884 |
boot_mode = kwargs.get("boot_mode") |
| 885 |
description = kwargs.get("description") |
| 886 |
device_mapping = kwargs.get("device_mapping") or [] |
| 887 |
enhanced_networking = kwargs.get("enhanced_networking") |
| 888 |
image_location = kwargs.get("image_location") |
| 889 |
instance_id = kwargs.get("instance_id") |
| 890 |
kernel_id = kwargs.get("kernel_id") |
| 891 |
name = kwargs.get("name") |
| 892 |
no_reboot = kwargs.get("no_reboot") |
| 893 |
ramdisk_id = kwargs.get("ramdisk_id") |
| 894 |
root_device_name = kwargs.get("root_device_name") |
| 895 |
sriov_net_support = kwargs.get("sriov_net_support") |
| 896 |
tags = kwargs.get("tags") |
| 897 |
tpm_support = kwargs.get("tpm_support") |
| 898 |
uefi_data = kwargs.get("uefi_data") |
| 899 |
virtualization_type = kwargs.get("virtualization_type") |
| 900 |
|
| 901 |
params = { |
| 902 |
"Name": name, |
| 903 |
"Description": description, |
| 904 |
"BlockDeviceMappings": CreateImage.build_block_device_mapping(device_mapping), |
| 905 |
} |
| 906 |
|
| 907 |
# Remove empty values injected by using options |
| 908 |
if instance_id: |
| 909 |
params.update( |
| 910 |
{ |
| 911 |
"InstanceId": instance_id, |
| 912 |
"NoReboot": no_reboot, |
| 913 |
"TagSpecifications": boto3_tag_specifications(tags, types=["image", "snapshot"]), |
| 914 |
} |
| 915 |
) |
| 916 |
else: |
| 917 |
params.update( |
| 918 |
{ |
| 919 |
"Architecture": architecture, |
| 920 |
"BillingProducts": billing_products, |
| 921 |
"BootMode": boot_mode, |
| 922 |
"EnaSupport": enhanced_networking, |
| 923 |
"ImageLocation": image_location, |
| 924 |
"KernelId": kernel_id, |
| 925 |
"RamdiskId": ramdisk_id, |
| 926 |
"RootDeviceName": root_device_name, |
| 927 |
"SriovNetSupport": sriov_net_support, |
| 928 |
"TpmSupport": tpm_support, |
| 929 |
"UefiData": uefi_data, |
| 930 |
"VirtualizationType": virtualization_type, |
| 931 |
} |
| 932 |
) |
| 933 |
|
| 934 |
return {k: v for k, v in params.items() if v} |
| 935 |
|
| 936 |
@classmethod |
| 937 |
def do(cls, module, connection, _image_id): |
| 938 |
"""Entry point to create image""" |
| 939 |
create_image_parameters = cls.build_create_image_parameters(**module.params) |
| 940 |
|
| 941 |
func = cls.create_or_register(create_image_parameters) |
| 942 |
try: |
| 943 |
image = func(connection, **create_image_parameters) |
| 944 |
image_id = image.get("ImageId") |
| 945 |
except AnsibleEC2Error as e: |
| 946 |
raise Ec2AmiFailure("Error registering/creating image", e) |
| 947 |
|
| 948 |
cls.wait(connection, module, image_id) |
| 949 |
|
| 950 |
if "TagSpecifications" not in create_image_parameters: |
| 951 |
CreateImage.set_tags(connection, module, module.params.get("tags"), image_id) |
| 952 |
|
| 953 |
cls.set_launch_permissions(connection, module.params.get("launch_permissions"), image_id) |
| 954 |
|
| 955 |
module.exit_json( |
| 956 |
msg="AMI creation operation complete.", changed=True, **get_ami_info(get_image_by_id(connection, image_id)) |
| 957 |
) |
| 958 |
|
| 959 |
|
| 960 |
def main(): |
| 961 |
mapping_options = { |
| 962 |
"delete_on_termination": {"type": "bool"}, |
| 963 |
"device_name": {"type": "str", "required": True}, |
| 964 |
"encrypted": {"type": "bool"}, |
| 965 |
"iops": {"type": "int"}, |
| 966 |
"no_device": {"type": "bool"}, |
| 967 |
"snapshot_id": {"type": "str"}, |
| 968 |
"virtual_name": {"type": "str"}, |
| 969 |
"volume_size": {"type": "int", "aliases": ["size"]}, |
| 970 |
"volume_type": {"type": "str"}, |
| 971 |
} |
| 972 |
argument_spec = dict( |
| 973 |
architecture={"default": "x86_64"}, |
| 974 |
billing_products={"type": "list", "elements": "str"}, |
| 975 |
boot_mode={"type": "str", "choices": ["legacy-bios", "uefi", "uefi-preferred"]}, |
| 976 |
delete_snapshot={"default": False, "type": "bool"}, |
| 977 |
description={"default": ""}, |
| 978 |
device_mapping={"type": "list", "elements": "dict", "options": mapping_options}, |
| 979 |
enhanced_networking={"type": "bool"}, |
| 980 |
image_id={}, |
| 981 |
image_location={}, |
| 982 |
instance_id={}, |
| 983 |
kernel_id={}, |
| 984 |
launch_permissions=dict( |
| 985 |
type="dict", |
| 986 |
options=dict( |
| 987 |
user_ids=dict(type="list", elements="str"), |
| 988 |
group_names=dict(type="list", elements="str"), |
| 989 |
org_arns=dict(type="list", elements="str"), |
| 990 |
org_unit_arns=dict(type="list", elements="str"), |
| 991 |
), |
| 992 |
), |
| 993 |
name={}, |
| 994 |
no_reboot={"default": False, "type": "bool"}, |
| 995 |
purge_tags={"type": "bool", "default": True}, |
| 996 |
ramdisk_id={}, |
| 997 |
root_device_name={}, |
| 998 |
sriov_net_support={}, |
| 999 |
state={"default": "present", "choices": ["present", "absent"]}, |
| 1000 |
tags={"type": "dict", "aliases": ["resource_tags"]}, |
| 1001 |
tpm_support={"type": "str"}, |
| 1002 |
uefi_data={"type": "str"}, |
| 1003 |
virtualization_type={"default": "hvm"}, |
| 1004 |
wait={"type": "bool", "default": False}, |
| 1005 |
wait_timeout={"default": 1200, "type": "int"}, |
| 1006 |
) |
| 1007 |
|
| 1008 |
module = AnsibleAWSModule( |
| 1009 |
argument_spec=argument_spec, |
| 1010 |
required_if=[ |
| 1011 |
["state", "absent", ["image_id"]], |
| 1012 |
], |
| 1013 |
supports_check_mode=True, |
| 1014 |
) |
| 1015 |
|
| 1016 |
validate_params(module, **module.params) |
| 1017 |
|
| 1018 |
connection = module.client("ec2") |
| 1019 |
|
| 1020 |
CHECK_MODE_TRUE = True |
| 1021 |
CHECK_MODE_FALSE = False |
| 1022 |
HAS_IMAGE_ID_TRUE = True |
| 1023 |
HAS_IMAGE_ID_FALSE = False |
| 1024 |
|
| 1025 |
func_mapping = { |
| 1026 |
CHECK_MODE_TRUE: { |
| 1027 |
HAS_IMAGE_ID_TRUE: {"absent": DeregisterImage.do_check_mode, "present": UpdateImage.do}, |
| 1028 |
HAS_IMAGE_ID_FALSE: {"present": CreateImage.do_check_mode}, |
| 1029 |
}, |
| 1030 |
CHECK_MODE_FALSE: { |
| 1031 |
HAS_IMAGE_ID_TRUE: {"absent": DeregisterImage.do, "present": UpdateImage.do}, |
| 1032 |
HAS_IMAGE_ID_FALSE: {"present": CreateImage.do}, |
| 1033 |
}, |
| 1034 |
} |
| 1035 |
func = func_mapping[module.check_mode][bool(module.params.get("image_id"))][module.params["state"]] |
| 1036 |
try: |
| 1037 |
func(module, connection, module.params.get("image_id")) |
| 1038 |
except Ec2AmiFailure as e: |
| 1039 |
if e.original_e: |
| 1040 |
module.fail_json_aws(e.original_e, e.message) |
| 1041 |
else: |
| 1042 |
module.fail_json(e.message) |
| 1043 |
|
| 1044 |
|
| 1045 |
if __name__ == "__main__": |
| 1046 |
main() |