| 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: ec2_dedicated_host_info |
| 10 |
version_added: 9.3.0 |
| 11 |
short_description: Gather information about EC2 Dedicated Hosts in AWS |
| 12 |
description: |
| 13 |
- Gather information about EC2 Dedicated Hosts in AWS. |
| 14 |
- The module can also gather information about dedicated Mac hosts. |
| 15 |
author: |
| 16 |
- "Aubin Bikouo (@abikouo)" |
| 17 |
options: |
| 18 |
host_ids: |
| 19 |
description: |
| 20 |
- The IDs of the Dedicated Hosts. |
| 21 |
type: list |
| 22 |
elements: str |
| 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_DescribeHosts.html) for possible filters. |
| 27 |
- See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeMacHosts.html) for possible filters for |
| 28 |
EC2 Mac dedicated hosts. |
| 29 |
type: dict |
| 30 |
mac_only: |
| 31 |
description: |
| 32 |
- When set to V(true) retrieve EC2 Mac Dedicated Hosts only. |
| 33 |
type: bool |
| 34 |
default: False |
| 35 |
extends_documentation_fragment: |
| 36 |
- amazon.aws.common.modules |
| 37 |
- amazon.aws.region.modules |
| 38 |
- amazon.aws.boto3 |
| 39 |
""" |
| 40 |
|
| 41 |
EXAMPLES = r""" |
| 42 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 43 |
|
| 44 |
# Gather information about all dedicated hosts |
| 45 |
- name: Gather information about all dedicated hosts |
| 46 |
amazon.aws.ec2_dedicated_host_info: |
| 47 |
|
| 48 |
# Gather information about all EC2 Mac dedicated hosts only |
| 49 |
- name: Gather information about all EC2 Mac dedicated hosts |
| 50 |
amazon.aws.ec2_dedicated_host_info: |
| 51 |
mac_only: true |
| 52 |
|
| 53 |
# Gather information about dedicated hosts with state=released |
| 54 |
- name: Gather information about released dedicated hosts |
| 55 |
amazon.aws.ec2_dedicated_host_info: |
| 56 |
filters: |
| 57 |
state: released |
| 58 |
""" |
| 59 |
|
| 60 |
RETURN = r""" |
| 61 |
hosts: |
| 62 |
description: List of dedicated hosts. |
| 63 |
returned: When O(mac_only=false) |
| 64 |
type: complex |
| 65 |
contains: |
| 66 |
allocation_time: |
| 67 |
description: The time that the Dedicated Host was allocated. |
| 68 |
returned: always |
| 69 |
type: str |
| 70 |
sample: "2025-02-12T12:09:22+00:00" |
| 71 |
allows_multiple_instance_types: |
| 72 |
description: Indicates whether the Dedicated Host supports multiple instance types of the same instance family. |
| 73 |
returned: always |
| 74 |
type: str |
| 75 |
sample: "off" |
| 76 |
auto_placement: |
| 77 |
description: Whether auto-placement is on or off. |
| 78 |
returned: always |
| 79 |
type: str |
| 80 |
sample: "off" |
| 81 |
availability_zone: |
| 82 |
description: The Availability Zone of the Dedicated Host. |
| 83 |
returned: always |
| 84 |
type: str |
| 85 |
sample: "us-east-1b" |
| 86 |
availability_zone_id: |
| 87 |
description: The ID of the Availability Zone in which the Dedicated Host is allocated. |
| 88 |
returned: always |
| 89 |
type: str |
| 90 |
sample: "use1-az6" |
| 91 |
available_capacity: |
| 92 |
description: Information about the instances running on the Dedicated Host. |
| 93 |
returned: always |
| 94 |
type: dict |
| 95 |
contains: |
| 96 |
available_v_cpus: |
| 97 |
description: The number of vCPUs available for launching instances onto the Dedicated Host. |
| 98 |
returned: always |
| 99 |
type: int |
| 100 |
sample: 8 |
| 101 |
available_instance_capacity: |
| 102 |
description: The number of instances that can be launched onto the Dedicated Host. |
| 103 |
returned: always |
| 104 |
type: complex |
| 105 |
contains: |
| 106 |
available_capacity: |
| 107 |
description: The number of instances that can be launched onto the Dedicated Host. |
| 108 |
returned: always |
| 109 |
type: int |
| 110 |
sample: 1 |
| 111 |
instance_type: |
| 112 |
description: The instance type supported by the Dedicated Host. |
| 113 |
returned: always |
| 114 |
type: str |
| 115 |
sample: "mac2.metal" |
| 116 |
total_capacity: |
| 117 |
description: The total number of instances that can be launched onto the Dedicated Host if there are no instances running on it. |
| 118 |
returned: always |
| 119 |
type: dict |
| 120 |
sample: 1 |
| 121 |
host_id: |
| 122 |
description: The ID of the Dedicated Host. |
| 123 |
returned: always |
| 124 |
type: str |
| 125 |
sample: "h-03f51341e6e39f848" |
| 126 |
client_token: |
| 127 |
description: Unique, case-sensitive identifier that you provide to ensure the idempotency of the request. |
| 128 |
returned: always |
| 129 |
type: str |
| 130 |
sample: "token-0123456789a" |
| 131 |
host_properties: |
| 132 |
description: The hardware specifications of the Dedicated Host. |
| 133 |
returned: always |
| 134 |
type: dict |
| 135 |
contains: |
| 136 |
cores: |
| 137 |
description: The number of cores on the Dedicated Host. |
| 138 |
returned: always |
| 139 |
type: int |
| 140 |
sample: 8 |
| 141 |
instance_type: |
| 142 |
description: The instance type supported by the Dedicated Host. |
| 143 |
returned: always |
| 144 |
type: str |
| 145 |
sample: "mac2.metal" |
| 146 |
instance_family: |
| 147 |
description: The instance family supported by the Dedicated Host. |
| 148 |
returned: if defined |
| 149 |
type: str |
| 150 |
sample: "mac2" |
| 151 |
sockets: |
| 152 |
description: The number of sockets on the Dedicated Host. |
| 153 |
returned: always |
| 154 |
type: int |
| 155 |
sample: 1 |
| 156 |
total_v_cpus: |
| 157 |
description: The total number of vCPUs on the Dedicated Host. |
| 158 |
returned: always |
| 159 |
type: int |
| 160 |
sample: 8 |
| 161 |
host_reservation_id: |
| 162 |
description: The reservation ID of the Dedicated Host. |
| 163 |
returned: always |
| 164 |
type: str |
| 165 |
instances: |
| 166 |
description: The IDs and instance type that are currently running on the Dedicated Host. |
| 167 |
returned: always |
| 168 |
type: complex |
| 169 |
contains: |
| 170 |
instance_id: |
| 171 |
description: The ID of instance that is running on the Dedicated Host. |
| 172 |
returned: always |
| 173 |
type: str |
| 174 |
sample: "i-0123456789abcd" |
| 175 |
instance_type: |
| 176 |
description: The instance type of the running instance. |
| 177 |
returned: always |
| 178 |
type: str |
| 179 |
sample: "ec2.micro" |
| 180 |
owner_id: |
| 181 |
description: The ID of the Amazon Web Services account that owns the instance. |
| 182 |
returned: always |
| 183 |
type: str |
| 184 |
sample: "0123456789" |
| 185 |
state: |
| 186 |
description: The state of the Dedicated Host. |
| 187 |
returned: always |
| 188 |
type: str |
| 189 |
sample: "available" |
| 190 |
release_time: |
| 191 |
description: The time that the Dedicated Host was released. |
| 192 |
returned: always |
| 193 |
type: str |
| 194 |
sample: "2025-02-12T12:09:22+00:00" |
| 195 |
host_recovery: |
| 196 |
description: Indicates whether host recovery is enabled or disabled for the Dedicated Host. |
| 197 |
returned: always |
| 198 |
type: str |
| 199 |
sample: "off" |
| 200 |
owner_id: |
| 201 |
description: The ID of the Amazon Web Services account that owns the Dedicated Host. |
| 202 |
returned: always |
| 203 |
type: str |
| 204 |
sample: "0123456789" |
| 205 |
member_of_service_linked_resource_group: |
| 206 |
description: Indicates whether the Dedicated Host is in a host resource group. |
| 207 |
returned: always |
| 208 |
type: bool |
| 209 |
sample: false |
| 210 |
outpost_arn: |
| 211 |
description: The Amazon Resource Name (ARN) of the Amazon Web Services Outpost on which the Dedicated Host is allocated. |
| 212 |
returned: always |
| 213 |
type: str |
| 214 |
sample: "arn:aws:outposts:us-east-1:0123012301230123:outpost/op-0123456789abcdef0" |
| 215 |
host_maintenance: |
| 216 |
description: Indicates whether host maintenance is enabled or disabled for the Dedicated Host. |
| 217 |
returned: always |
| 218 |
type: str |
| 219 |
sample: "off" |
| 220 |
asset_id: |
| 221 |
description: The ID of the Outpost hardware asset on which the Dedicated Host is allocated. |
| 222 |
returned: always |
| 223 |
type: str |
| 224 |
sample: "abcdefgh" |
| 225 |
tags: |
| 226 |
description: Dictionary of tags added to the dedicated host. |
| 227 |
returned: always |
| 228 |
type: dict |
| 229 |
sample: {} |
| 230 |
mac_hosts: |
| 231 |
description: List of EC2 Mac dedicated hosts. |
| 232 |
returned: When O(mac_only=true) |
| 233 |
type: complex |
| 234 |
contains: |
| 235 |
host_id: |
| 236 |
description: The EC2 Mac Dedicated Host ID. |
| 237 |
returned: always |
| 238 |
type: str |
| 239 |
sample: "h-04b38258aba8c1875" |
| 240 |
mac_os_latest_supported_versions: |
| 241 |
description: The latest macOS versions that the EC2 Mac Dedicated Host can launch without being upgraded. |
| 242 |
returned: always |
| 243 |
type: list |
| 244 |
elements: str |
| 245 |
sample: ["15.3", "14.7.3", "13.7.3"] |
| 246 |
""" |
| 247 |
|
| 248 |
from typing import Any |
| 249 |
from typing import Dict |
| 250 |
from typing import List |
| 251 |
|
| 252 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 253 |
|
| 254 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_ec2_dedicated_hosts |
| 255 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_ec2_mac_dedicated_hosts |
| 256 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 257 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 258 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 259 |
|
| 260 |
|
| 261 |
def format_results(hosts: List[Dict[str, Any]]) -> List[Dict[str, Any]]: |
| 262 |
results = [] |
| 263 |
for h in hosts: |
| 264 |
h["tags"] = boto3_tag_list_to_ansible_dict(h.pop("Tags", {})) |
| 265 |
results.append(camel_dict_to_snake_dict(h, ignore_list=["tags"])) |
| 266 |
return results |
| 267 |
|
| 268 |
|
| 269 |
def build_args(module: AnsibleAWSModule) -> Dict[str, Any]: |
| 270 |
filters = module.params.get("filters") |
| 271 |
host_ids = module.params.get("host_ids") |
| 272 |
params = {} |
| 273 |
if host_ids: |
| 274 |
params["HostIds"] = host_ids |
| 275 |
if filters: |
| 276 |
params["Filters"] = ansible_dict_to_boto3_filter_list(filters) |
| 277 |
return params |
| 278 |
|
| 279 |
|
| 280 |
def list_dedicated_hosts(client: Any, module: AnsibleAWSModule) -> Dict[str, Any]: |
| 281 |
params = build_args(module) |
| 282 |
results = describe_ec2_dedicated_hosts(client, **params) |
| 283 |
return dict(hosts=format_results(results)) |
| 284 |
|
| 285 |
|
| 286 |
def list_mac_dedicated_hosts(client: Any, module: AnsibleAWSModule) -> Dict[str, Any]: |
| 287 |
params = build_args(module) |
| 288 |
results = describe_ec2_mac_dedicated_hosts(client, **params) |
| 289 |
return dict(mac_hosts=format_results(results)) |
| 290 |
|
| 291 |
|
| 292 |
def main(): |
| 293 |
module = AnsibleAWSModule( |
| 294 |
argument_spec=dict( |
| 295 |
host_ids=dict(type="list", elements="str"), |
| 296 |
filters=dict(type="dict"), |
| 297 |
mac_only=dict(type="bool", default=False), |
| 298 |
), |
| 299 |
supports_check_mode=True, |
| 300 |
) |
| 301 |
|
| 302 |
client = module.client("ec2") |
| 303 |
mac_only = module.params.get("mac_only") |
| 304 |
|
| 305 |
if mac_only: |
| 306 |
result = list_mac_dedicated_hosts(client, module) |
| 307 |
else: |
| 308 |
result = list_dedicated_hosts(client, module) |
| 309 |
module.exit_json(**result) |
| 310 |
|
| 311 |
|
| 312 |
if __name__ == "__main__": |
| 313 |
main() |