| 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_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Gather information about EC2 AMIs |
| 12 |
description: |
| 13 |
- Gather information about EC2 AMIs. |
| 14 |
author: |
| 15 |
- Prasad Katti (@prasadkatti) |
| 16 |
options: |
| 17 |
image_ids: |
| 18 |
description: One or more image IDs. |
| 19 |
aliases: [image_id] |
| 20 |
type: list |
| 21 |
elements: str |
| 22 |
default: [] |
| 23 |
filters: |
| 24 |
description: |
| 25 |
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. |
| 26 |
- See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeImages.html) for possible filters. |
| 27 |
- Filter names and values are case sensitive. |
| 28 |
type: dict |
| 29 |
default: {} |
| 30 |
owners: |
| 31 |
description: |
| 32 |
- Filter the images by the owner. Valid options are an AWS account ID, self, |
| 33 |
or an AWS owner alias ( amazon | aws-marketplace | microsoft ). |
| 34 |
aliases: [owner] |
| 35 |
type: list |
| 36 |
elements: str |
| 37 |
default: [] |
| 38 |
executable_users: |
| 39 |
description: |
| 40 |
- Filter images by users with explicit launch permissions. Valid options are an AWS account ID, self, or all (public AMIs). |
| 41 |
aliases: [executable_user] |
| 42 |
type: list |
| 43 |
elements: str |
| 44 |
default: [] |
| 45 |
describe_image_attributes: |
| 46 |
description: |
| 47 |
- Describe attributes (like launchPermission) of the images found. |
| 48 |
default: false |
| 49 |
type: bool |
| 50 |
|
| 51 |
extends_documentation_fragment: |
| 52 |
- amazon.aws.common.modules |
| 53 |
- amazon.aws.region.modules |
| 54 |
- amazon.aws.boto3 |
| 55 |
""" |
| 56 |
|
| 57 |
EXAMPLES = r""" |
| 58 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 59 |
|
| 60 |
- name: gather information about an AMI using ami-id |
| 61 |
amazon.aws.ec2_ami_info: |
| 62 |
image_ids: ami-5b488823 |
| 63 |
|
| 64 |
- name: gather information about all AMIs with tag key Name and value webapp |
| 65 |
amazon.aws.ec2_ami_info: |
| 66 |
filters: |
| 67 |
"tag:Name": webapp |
| 68 |
|
| 69 |
- name: gather information about an AMI with 'AMI Name' equal to foobar |
| 70 |
amazon.aws.ec2_ami_info: |
| 71 |
filters: |
| 72 |
name: foobar |
| 73 |
|
| 74 |
- name: gather information about Ubuntu 17.04 AMIs published by Canonical (099720109477) |
| 75 |
amazon.aws.ec2_ami_info: |
| 76 |
owners: 099720109477 |
| 77 |
filters: |
| 78 |
name: "ubuntu/images/ubuntu-zesty-17.04-*" |
| 79 |
""" |
| 80 |
|
| 81 |
RETURN = r""" |
| 82 |
images: |
| 83 |
description: A list of images. |
| 84 |
returned: always |
| 85 |
type: list |
| 86 |
elements: dict |
| 87 |
contains: |
| 88 |
architecture: |
| 89 |
description: The architecture of the image. |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: x86_64 |
| 93 |
block_device_mappings: |
| 94 |
description: Any block device mapping entries. |
| 95 |
returned: always |
| 96 |
type: list |
| 97 |
elements: dict |
| 98 |
contains: |
| 99 |
device_name: |
| 100 |
description: The device name exposed to the instance. |
| 101 |
returned: always |
| 102 |
type: str |
| 103 |
sample: /dev/sda1 |
| 104 |
ebs: |
| 105 |
description: EBS volumes |
| 106 |
returned: always |
| 107 |
type: complex |
| 108 |
creation_date: |
| 109 |
description: The date and time the image was created. |
| 110 |
returned: always |
| 111 |
type: str |
| 112 |
sample: '2017-10-16T19:22:13.000Z' |
| 113 |
description: |
| 114 |
description: The description of the AMI. |
| 115 |
type: str |
| 116 |
sample: '' |
| 117 |
ena_support: |
| 118 |
description: Whether enhanced networking with ENA is enabled. |
| 119 |
returned: always |
| 120 |
type: bool |
| 121 |
sample: true |
| 122 |
hypervisor: |
| 123 |
description: The hypervisor type of the image. |
| 124 |
returned: always |
| 125 |
type: str |
| 126 |
sample: xen |
| 127 |
image_id: |
| 128 |
description: The ID of the AMI. |
| 129 |
returned: always |
| 130 |
type: str |
| 131 |
sample: ami-5b466623 |
| 132 |
image_location: |
| 133 |
description: The location of the AMI. |
| 134 |
returned: always |
| 135 |
type: str |
| 136 |
sample: 123456789012/Webapp |
| 137 |
image_type: |
| 138 |
description: The type of image. |
| 139 |
returned: always |
| 140 |
type: str |
| 141 |
sample: machine |
| 142 |
launch_permissions: |
| 143 |
description: A List of AWS accounts may launch the AMI. |
| 144 |
returned: When image is owned by calling account and O(describe_image_attributes=true). |
| 145 |
type: list |
| 146 |
elements: dict |
| 147 |
contains: |
| 148 |
group: |
| 149 |
description: A value of 'all' means the AMI is public. |
| 150 |
type: str |
| 151 |
user_id: |
| 152 |
description: An AWS account ID with permissions to launch the AMI. |
| 153 |
type: str |
| 154 |
sample: [{"group": "all"}, {"user_id": "123456789012"}] |
| 155 |
name: |
| 156 |
description: The name of the AMI that was provided during image creation. |
| 157 |
returned: always |
| 158 |
type: str |
| 159 |
sample: Webapp |
| 160 |
owner_id: |
| 161 |
description: The AWS account ID of the image owner. |
| 162 |
returned: always |
| 163 |
type: str |
| 164 |
sample: '123456789012' |
| 165 |
platform_details: |
| 166 |
description: Platform of image. |
| 167 |
returned: always |
| 168 |
type: str |
| 169 |
sample: "Windows" |
| 170 |
public: |
| 171 |
description: Whether the image has public launch permissions. |
| 172 |
returned: always |
| 173 |
type: bool |
| 174 |
sample: true |
| 175 |
root_device_name: |
| 176 |
description: The device name of the root device. |
| 177 |
returned: always |
| 178 |
type: str |
| 179 |
sample: /dev/sda1 |
| 180 |
root_device_type: |
| 181 |
description: The type of root device used by the AMI. |
| 182 |
returned: always |
| 183 |
type: str |
| 184 |
sample: ebs |
| 185 |
sriov_net_support: |
| 186 |
description: Whether enhanced networking is enabled. |
| 187 |
type: str |
| 188 |
sample: simple |
| 189 |
state: |
| 190 |
description: The current state of the AMI. |
| 191 |
returned: always |
| 192 |
type: str |
| 193 |
sample: available |
| 194 |
tags: |
| 195 |
description: Any tags assigned to the image. |
| 196 |
returned: always |
| 197 |
type: dict |
| 198 |
usage_operation: |
| 199 |
description: The operation of the Amazon EC2 instance and the billing code that is associated with the AMI. |
| 200 |
returned: always |
| 201 |
type: str |
| 202 |
sample: "RunInstances" |
| 203 |
virtualization_type: |
| 204 |
description: The type of virtualization of the AMI. |
| 205 |
returned: always |
| 206 |
type: str |
| 207 |
sample: hvm |
| 208 |
""" |
| 209 |
|
| 210 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 211 |
|
| 212 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 213 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_image_attribute |
| 214 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_images |
| 215 |
from ansible_collections.amazon.aws.plugins.module_utils.exceptions import AnsibleAWSError |
| 216 |
from ansible_collections.amazon.aws.plugins.module_utils.exceptions import is_ansible_aws_error_code |
| 217 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 218 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 219 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 220 |
|
| 221 |
|
| 222 |
class AmiInfoFailure(Exception): |
| 223 |
def __init__(self, original_e, user_message): |
| 224 |
self.original_e = original_e |
| 225 |
self.user_message = user_message |
| 226 |
super().__init__(self) |
| 227 |
|
| 228 |
|
| 229 |
def build_request_args(executable_users, filters, image_ids, owners): |
| 230 |
request_args = { |
| 231 |
"ExecutableUsers": [str(user) for user in executable_users], |
| 232 |
"ImageIds": [str(image_id) for image_id in image_ids], |
| 233 |
} |
| 234 |
|
| 235 |
# describe_images is *very* slow if you pass the `Owners` |
| 236 |
# param (unless it's self), for some reason. |
| 237 |
# Converting the owners to filters and removing from the |
| 238 |
# owners param greatly speeds things up. |
| 239 |
# Implementation based on aioue's suggestion in #24886 |
| 240 |
for owner in owners: |
| 241 |
if owner.isdigit(): |
| 242 |
if "owner-id" not in filters: |
| 243 |
filters["owner-id"] = list() |
| 244 |
filters["owner-id"].append(owner) |
| 245 |
elif owner == "self": |
| 246 |
# self not a valid owner-alias filter (https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeImages.html) |
| 247 |
request_args["Owners"] = [str(owner)] |
| 248 |
else: |
| 249 |
if "owner-alias" not in filters: |
| 250 |
filters["owner-alias"] = list() |
| 251 |
filters["owner-alias"].append(owner) |
| 252 |
|
| 253 |
request_args["Filters"] = ansible_dict_to_boto3_filter_list(filters) |
| 254 |
|
| 255 |
request_args = {k: v for k, v in request_args.items() if v} |
| 256 |
|
| 257 |
return request_args |
| 258 |
|
| 259 |
|
| 260 |
def get_images(ec2_client, request_args): |
| 261 |
try: |
| 262 |
images = describe_images(ec2_client, **request_args) |
| 263 |
except AnsibleEC2Error as e: |
| 264 |
raise AmiInfoFailure(e, "error describing images") |
| 265 |
return images |
| 266 |
|
| 267 |
|
| 268 |
def list_ec2_images(ec2_client, module, request_args): |
| 269 |
images = get_images(ec2_client, request_args) |
| 270 |
images = [camel_dict_to_snake_dict(image) for image in images] |
| 271 |
|
| 272 |
for image in images: |
| 273 |
try: |
| 274 |
image["tags"] = boto3_tag_list_to_ansible_dict(image.get("tags", [])) |
| 275 |
if module.params.get("describe_image_attributes"): |
| 276 |
launch_permissions = describe_image_attribute( |
| 277 |
ec2_client, attribute="launchPermission", image_id=image["image_id"] |
| 278 |
).get("LaunchPermissions", []) |
| 279 |
image["launch_permissions"] = [camel_dict_to_snake_dict(perm) for perm in launch_permissions] |
| 280 |
except is_ansible_aws_error_code("AuthFailure"): |
| 281 |
# describing launch permissions of images owned by others is not permitted, but shouldn't cause failures |
| 282 |
pass |
| 283 |
except AnsibleAWSError as e: # pylint: disable=duplicate-except |
| 284 |
raise AmiInfoFailure(e, "Failed to describe AMI") |
| 285 |
|
| 286 |
images.sort(key=lambda e: e.get("creation_date", "")) # it may be possible that creation_date does not always exist |
| 287 |
|
| 288 |
return images |
| 289 |
|
| 290 |
|
| 291 |
def main(): |
| 292 |
argument_spec = dict( |
| 293 |
describe_image_attributes=dict(default=False, type="bool"), |
| 294 |
executable_users=dict(default=[], type="list", elements="str", aliases=["executable_user"]), |
| 295 |
filters=dict(default={}, type="dict"), |
| 296 |
image_ids=dict(default=[], type="list", elements="str", aliases=["image_id"]), |
| 297 |
owners=dict(default=[], type="list", elements="str", aliases=["owner"]), |
| 298 |
) |
| 299 |
|
| 300 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 301 |
|
| 302 |
ec2_client = module.client("ec2") |
| 303 |
|
| 304 |
request_args = build_request_args( |
| 305 |
executable_users=module.params["executable_users"], |
| 306 |
filters=module.params["filters"], |
| 307 |
image_ids=module.params["image_ids"], |
| 308 |
owners=module.params["owners"], |
| 309 |
) |
| 310 |
|
| 311 |
images = list_ec2_images(ec2_client, module, request_args) |
| 312 |
|
| 313 |
module.exit_json(images=images) |
| 314 |
|
| 315 |
|
| 316 |
if __name__ == "__main__": |
| 317 |
main() |