| 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 |
| 10 |
version_added: 9.3.0 |
| 11 |
short_description: Create, update or delete (release) EC2 dedicated host |
| 12 |
description: |
| 13 |
- Create, update or delete (release) EC2 dedicated host. |
| 14 |
author: |
| 15 |
- "Aubin Bikouo (@abikouo)" |
| 16 |
options: |
| 17 |
state: |
| 18 |
description: |
| 19 |
- Create or delete dedicated host. |
| 20 |
required: false |
| 21 |
choices: [ present, absent ] |
| 22 |
default: 'present' |
| 23 |
type: str |
| 24 |
host_id: |
| 25 |
description: |
| 26 |
- The IDs of the Dedicated Hosts to release or modify |
| 27 |
type: str |
| 28 |
required: false |
| 29 |
lookup: |
| 30 |
description: |
| 31 |
- Look up EC2 dedicated host by either O(tags) or by O(host_id). |
| 32 |
- If O(lookup=tag) and O(tags) is not specified then no lookup for an |
| 33 |
existing dedicated host is performed and a new dedicated host will be created. |
| 34 |
- When using O(lookup=tag), multiple matches being found will result in |
| 35 |
a failure and no changes will be made. |
| 36 |
- To change the tags of a dedicated host, use O(lookup=host_id). |
| 37 |
default: tag |
| 38 |
choices: [ 'tag', 'host_id' ] |
| 39 |
type: str |
| 40 |
instance_family: |
| 41 |
description: |
| 42 |
- Specifies the instance family to be supported by the Dedicated Hosts. |
| 43 |
- If you specify an instance family, the Dedicated Hosts support multiple instance types within that instance family. |
| 44 |
- At least one of O(instance_family) or O(instance_type) must be specified when allocating dedicated host. |
| 45 |
- Mutually exclusive with paramter O(instance_type). |
| 46 |
type: str |
| 47 |
instance_type: |
| 48 |
description: |
| 49 |
- Specifies the instance type to be supported by the Dedicated Hosts. |
| 50 |
- If you specify an instance type, the Dedicated Hosts support instances of the specified instance type only. |
| 51 |
- At least one of O(instance_family) or O(instance_type) must be specified when allocating dedicated host. |
| 52 |
- Mutually exclusive with paramter O(instance_family). |
| 53 |
type: str |
| 54 |
host_recovery: |
| 55 |
description: |
| 56 |
- Indicates whether to enable or disable host recovery for the Dedicated Host. |
| 57 |
- Host recovery is disabled by default. |
| 58 |
type: str |
| 59 |
default: 'off' |
| 60 |
choices: ['on', 'off'] |
| 61 |
outpost_arn: |
| 62 |
description: |
| 63 |
- The Amazon Resource Name (ARN) of the Amazon Web Services Outpost on which to allocate the Dedicated Host. |
| 64 |
type: str |
| 65 |
host_maintenance: |
| 66 |
description: |
| 67 |
- Indicates whether to enable or disable host maintenance for the Dedicated Host. |
| 68 |
type: str |
| 69 |
default: 'off' |
| 70 |
choices: ['on', 'off'] |
| 71 |
asset_ids: |
| 72 |
description: |
| 73 |
- The IDs of the Outpost hardware assets on which to allocate the Dedicated Hosts. |
| 74 |
- Targeting specific hardware assets on an Outpost can help to minimize latency between your workloads. |
| 75 |
- This parameter is supported only if you specify O(outpost_arn). |
| 76 |
- If you specify this parameter, you can omit O(quantity). |
| 77 |
- If you specify both O(asset_ids) and O(quantity), then the value for O(quantity) must be equal to the number of O(asset_ids) specified. |
| 78 |
type: list |
| 79 |
elements: str |
| 80 |
auto_placement: |
| 81 |
description: |
| 82 |
- Indicates whether the host accepts any untargeted instance launches that match its instance type configuration, |
| 83 |
or if it only accepts Host tenancy instance launches that specify its unique host ID. |
| 84 |
type: str |
| 85 |
default: 'off' |
| 86 |
choices: ['on', 'off'] |
| 87 |
client_token: |
| 88 |
description: |
| 89 |
- case-sensitive identifier that you provide to ensure the idempotency of the request. |
| 90 |
type: str |
| 91 |
quantity: |
| 92 |
description: |
| 93 |
- The number of Dedicated Hosts to allocate to your account with these parameters. |
| 94 |
- Required when O(state=present). |
| 95 |
- If you are allocating the Dedicated Hosts on an Outpost, and you specify O(asset_ids), you can omit this parameter. |
| 96 |
- If you specify both O(asset_ids) and O(quantity), then the value that you specify for O(quantity) must be equal to the number of O(asset_ids) specified. |
| 97 |
type: int |
| 98 |
availability_zone: |
| 99 |
description: |
| 100 |
- The Availability Zone in which to allocate the Dedicated Host. |
| 101 |
- Required if O(state=present). |
| 102 |
type: str |
| 103 |
extends_documentation_fragment: |
| 104 |
- amazon.aws.common.modules |
| 105 |
- amazon.aws.region.modules |
| 106 |
- amazon.aws.boto3 |
| 107 |
- amazon.aws.tags |
| 108 |
""" |
| 109 |
|
| 110 |
EXAMPLES = r""" |
| 111 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 112 |
|
| 113 |
# Release dedicated hosts |
| 114 |
- name: Release dedicated host |
| 115 |
amazon.aws.ec2_dedicated_host: |
| 116 |
host_id: "h-0123456789abcdef0" |
| 117 |
|
| 118 |
# Allocate dedicated host using instance type |
| 119 |
- name: Allocate dedicated host using instance_type |
| 120 |
amazon.aws.ec2_dedicated_host: |
| 121 |
availability_zone: us-east-1a |
| 122 |
quantity: 1 |
| 123 |
instance_type: mac2.metal |
| 124 |
tags: |
| 125 |
Scope: Mac |
| 126 |
""" |
| 127 |
|
| 128 |
RETURN = r""" |
| 129 |
host: |
| 130 |
description: Information about the dedicated host created/updated. |
| 131 |
returned: When O(state=present) |
| 132 |
type: complex |
| 133 |
contains: |
| 134 |
allocation_time: |
| 135 |
description: The time that the Dedicated Host was allocated. |
| 136 |
returned: always |
| 137 |
type: str |
| 138 |
sample: "2025-02-12T12:09:22+00:00" |
| 139 |
allows_multiple_instance_types: |
| 140 |
description: Indicates whether the Dedicated Host supports multiple instance types of the same instance family. |
| 141 |
returned: always |
| 142 |
type: str |
| 143 |
sample: "off" |
| 144 |
auto_placement: |
| 145 |
description: Whether auto-placement is on or off. |
| 146 |
returned: always |
| 147 |
type: str |
| 148 |
sample: "off" |
| 149 |
availability_zone: |
| 150 |
description: The Availability Zone of the Dedicated Host. |
| 151 |
returned: always |
| 152 |
type: str |
| 153 |
sample: "us-east-1b" |
| 154 |
availability_zone_id: |
| 155 |
description: The ID of the Availability Zone in which the Dedicated Host is allocated. |
| 156 |
returned: always |
| 157 |
type: str |
| 158 |
sample: "use1-az6" |
| 159 |
available_capacity: |
| 160 |
description: Information about the instances running on the Dedicated Host. |
| 161 |
returned: always |
| 162 |
type: dict |
| 163 |
contains: |
| 164 |
available_v_cpus: |
| 165 |
description: The number of vCPUs available for launching instances onto the Dedicated Host. |
| 166 |
returned: always |
| 167 |
type: int |
| 168 |
sample: 8 |
| 169 |
available_instance_capacity: |
| 170 |
description: The number of instances that can be launched onto the Dedicated Host. |
| 171 |
returned: always |
| 172 |
type: complex |
| 173 |
contains: |
| 174 |
available_capacity: |
| 175 |
description: The number of instances that can be launched onto the Dedicated Host. |
| 176 |
returned: always |
| 177 |
type: int |
| 178 |
sample: 1 |
| 179 |
instance_type: |
| 180 |
description: The instance type supported by the Dedicated Host. |
| 181 |
returned: always |
| 182 |
type: str |
| 183 |
sample: "mac2.metal" |
| 184 |
total_capacity: |
| 185 |
description: The total number of instances that can be launched onto the Dedicated Host if there are no instances running on it. |
| 186 |
returned: always |
| 187 |
type: dict |
| 188 |
sample: 1 |
| 189 |
host_id: |
| 190 |
description: The ID of the Dedicated Host. |
| 191 |
returned: always |
| 192 |
type: str |
| 193 |
sample: "h-03f51341e6e39f848" |
| 194 |
client_token: |
| 195 |
description: Unique, case-sensitive identifier that you provide to ensure the idempotency of the request. |
| 196 |
returned: always |
| 197 |
type: str |
| 198 |
sample: "token-0123456789a" |
| 199 |
host_properties: |
| 200 |
description: The hardware specifications of the Dedicated Host. |
| 201 |
returned: always |
| 202 |
type: dict |
| 203 |
contains: |
| 204 |
cores: |
| 205 |
description: The number of cores on the Dedicated Host. |
| 206 |
returned: always |
| 207 |
type: int |
| 208 |
sample: 8 |
| 209 |
instance_type: |
| 210 |
description: The instance type supported by the Dedicated Host. |
| 211 |
returned: always |
| 212 |
type: str |
| 213 |
sample: "mac2.metal" |
| 214 |
instance_family: |
| 215 |
description: The instance family supported by the Dedicated Host. |
| 216 |
returned: if defined |
| 217 |
type: str |
| 218 |
sample: "mac2" |
| 219 |
sockets: |
| 220 |
description: The number of sockets on the Dedicated Host. |
| 221 |
returned: always |
| 222 |
type: int |
| 223 |
sample: 1 |
| 224 |
total_v_cpus: |
| 225 |
description: The total number of vCPUs on the Dedicated Host. |
| 226 |
returned: always |
| 227 |
type: int |
| 228 |
sample: 8 |
| 229 |
host_reservation_id: |
| 230 |
description: The reservation ID of the Dedicated Host. |
| 231 |
returned: always |
| 232 |
type: str |
| 233 |
instances: |
| 234 |
description: The IDs and instance type that are currently running on the Dedicated Host. |
| 235 |
returned: always |
| 236 |
type: complex |
| 237 |
contains: |
| 238 |
instance_id: |
| 239 |
description: The ID of instance that is running on the Dedicated Host. |
| 240 |
returned: always |
| 241 |
type: str |
| 242 |
sample: "i-0123456789abcd" |
| 243 |
instance_type: |
| 244 |
description: The instance type of the running instance. |
| 245 |
returned: always |
| 246 |
type: str |
| 247 |
sample: "ec2.micro" |
| 248 |
owner_id: |
| 249 |
description: The ID of the Amazon Web Services account that owns the instance. |
| 250 |
returned: always |
| 251 |
type: str |
| 252 |
sample: "0123456789" |
| 253 |
state: |
| 254 |
description: The state of the Dedicated Host. |
| 255 |
returned: always |
| 256 |
type: str |
| 257 |
sample: "available" |
| 258 |
release_time: |
| 259 |
description: The time that the Dedicated Host was released. |
| 260 |
returned: always |
| 261 |
type: str |
| 262 |
sample: "2025-02-12T12:09:22+00:00" |
| 263 |
host_recovery: |
| 264 |
description: Indicates whether host recovery is enabled or disabled for the Dedicated Host. |
| 265 |
returned: always |
| 266 |
type: str |
| 267 |
sample: "off" |
| 268 |
owner_id: |
| 269 |
description: The ID of the Amazon Web Services account that owns the Dedicated Host. |
| 270 |
returned: always |
| 271 |
type: str |
| 272 |
sample: "0123456789" |
| 273 |
member_of_service_linked_resource_group: |
| 274 |
description: Indicates whether the Dedicated Host is in a host resource group. |
| 275 |
returned: always |
| 276 |
type: bool |
| 277 |
sample: false |
| 278 |
outpost_arn: |
| 279 |
description: The Amazon Resource Name (ARN) of the Amazon Web Services Outpost on which the Dedicated Host is allocated. |
| 280 |
returned: always |
| 281 |
type: str |
| 282 |
sample: "arn:aws:outposts:us-east-1:0123012301230123:outpost/op-0123456789abcdef0" |
| 283 |
host_maintenance: |
| 284 |
description: Indicates whether host maintenance is enabled or disabled for the Dedicated Host. |
| 285 |
returned: always |
| 286 |
type: str |
| 287 |
sample: "off" |
| 288 |
asset_id: |
| 289 |
description: The ID of the Outpost hardware asset on which the Dedicated Host is allocated. |
| 290 |
returned: always |
| 291 |
type: str |
| 292 |
sample: "abcdefgh" |
| 293 |
tags: |
| 294 |
description: Dictionary of tags added to the dedicated host. |
| 295 |
returned: always |
| 296 |
type: dict |
| 297 |
sample: {} |
| 298 |
""" |
| 299 |
|
| 300 |
from typing import Any |
| 301 |
from typing import Dict |
| 302 |
from typing import List |
| 303 |
from typing import NoReturn |
| 304 |
from typing import Optional |
| 305 |
|
| 306 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 307 |
|
| 308 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 309 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import allocate_ec2_dedicated_hosts |
| 310 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_ec2_dedicated_hosts |
| 311 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags |
| 312 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import modify_ec2_dedicated_hosts |
| 313 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import release_ec2_dedicated_host |
| 314 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 315 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 316 |
|
| 317 |
|
| 318 |
def describe_host(client, host_id: str) -> Optional[Dict[str, Any]]: |
| 319 |
host_info = None |
| 320 |
hosts = describe_ec2_dedicated_hosts(client, HostIds=[host_id]) |
| 321 |
if hosts: |
| 322 |
host_info = hosts[0] |
| 323 |
return host_info |
| 324 |
|
| 325 |
|
| 326 |
def tags_match(match_tags: Dict[str, str], candidate_tags: Dict[str, str]) -> bool: |
| 327 |
return all((k in candidate_tags and candidate_tags[k] == v for k, v in match_tags.items())) |
| 328 |
|
| 329 |
|
| 330 |
def get_ec2_dedicated_host(client, module: AnsibleAWSModule) -> Optional[Dict[str, Any]]: |
| 331 |
host_info = None |
| 332 |
# Select all states except 'released' and 'released-permanent-failure' |
| 333 |
params = {"Filters": [{"Name": "state", "Values": ["available", "under-assessment", "permanent-failure"]}]} |
| 334 |
|
| 335 |
lookup = module.params.get("lookup") |
| 336 |
host_id = module.params.get("host_id") |
| 337 |
ansible_tags = module.params.get("tags") |
| 338 |
|
| 339 |
if lookup == "host_id": |
| 340 |
params["HostIds"] = [host_id] |
| 341 |
elif lookup == "tag" and not ansible_tags: |
| 342 |
# lookup == 'tag' but tags was not provided |
| 343 |
return host_info |
| 344 |
hosts = describe_ec2_dedicated_hosts(client, **params) |
| 345 |
if hosts: |
| 346 |
if lookup == "tag": |
| 347 |
count = 0 |
| 348 |
for h in hosts: |
| 349 |
aws_tags = boto3_tag_list_to_ansible_dict(h.get("Tags", {})) |
| 350 |
if aws_tags == ansible_tags: |
| 351 |
host_info = h |
| 352 |
count += 1 |
| 353 |
if count > 1: |
| 354 |
module.fail_json(msg=f"Tags provided do not identify a unique dedicated host ({count} found).") |
| 355 |
else: |
| 356 |
host_info = hosts[0] |
| 357 |
|
| 358 |
return host_info |
| 359 |
|
| 360 |
|
| 361 |
def format_output(host: Dict[str, Any]) -> List[Dict[str, Any]]: |
| 362 |
host["tags"] = boto3_tag_list_to_ansible_dict(host.pop("Tags", {})) |
| 363 |
return camel_dict_to_snake_dict(host, ignore_list=["tags"]) |
| 364 |
|
| 365 |
|
| 366 |
def release_host(client, module: AnsibleAWSModule, existing: Dict[str, Any]) -> NoReturn: |
| 367 |
if not existing: |
| 368 |
module.exit_json(changed=False) |
| 369 |
host_id = module.params.get("host_id") |
| 370 |
if module.check_mode: |
| 371 |
module.exit_json(changed=True, msg=f"Would have released dedicated host '{host_id}' if not in check mode.") |
| 372 |
changed = release_ec2_dedicated_host(client, host_id) |
| 373 |
module.exit_json(changed=changed, host=format_output(existing)) |
| 374 |
|
| 375 |
|
| 376 |
def create_or_update_host(client, module: AnsibleAWSModule, existing: Dict[str, Any]) -> NoReturn: |
| 377 |
host_id = module.params.get("host_id") |
| 378 |
changed = False |
| 379 |
if existing: |
| 380 |
# Update dedicated host |
| 381 |
# Check if one of the following has changed ('HostRecovery', 'InstanceType', 'InstanceFamily', 'HostMaintenance', 'AutoPlacement') |
| 382 |
host_maintenance = module.params.get("host_maintenance") |
| 383 |
instance_type = module.params.get("instance_type") |
| 384 |
instance_family = module.params.get("instance_family") |
| 385 |
auto_placement = module.params.get("auto_placement") |
| 386 |
host_recovery = module.params.get("host_recovery") |
| 387 |
params_to_update = {} |
| 388 |
if host_recovery != existing.get("HostRecovery"): |
| 389 |
params_to_update["HostRecovery"] = host_recovery |
| 390 |
if instance_type != existing.get("HostProperties", {}).get("InstanceType"): |
| 391 |
params_to_update["InstanceType"] = instance_type |
| 392 |
if instance_family != existing.get("HostProperties", {}).get("InstanceFamily"): |
| 393 |
params_to_update["InstanceFamily"] = instance_family |
| 394 |
if host_maintenance != existing.get("HostMaintenance"): |
| 395 |
params_to_update["HostMaintenance"] = host_maintenance |
| 396 |
if auto_placement != existing.get("AutoPlacement"): |
| 397 |
params_to_update["AutoPlacement"] = auto_placement |
| 398 |
|
| 399 |
if params_to_update: |
| 400 |
if module.check_mode: |
| 401 |
module.exit_json(changed=True, msg="Would have update dedicated host if not in check mode.") |
| 402 |
|
| 403 |
result = modify_ec2_dedicated_hosts(client, host_id=host_id, **params_to_update) |
| 404 |
if result.get("Unsuccessful"): |
| 405 |
code = result["Unsuccessful"][0]["Error"]["Code"] |
| 406 |
message = result["Unsuccessful"][0]["Error"]["Message"] |
| 407 |
module.fail_json( |
| 408 |
msg=f"The Dedicated Hosts '{host_id}' could not be modified. Code='{code}' Message = '{message}'" |
| 409 |
) |
| 410 |
changed = True |
| 411 |
else: |
| 412 |
# Allocate dedicated host |
| 413 |
if module.check_mode: |
| 414 |
module.exit_json(changed=True, msg="Would have allocate dedicated host if not in check mode.") |
| 415 |
|
| 416 |
availability_zone = module.params.get("availability_zone") |
| 417 |
instance_family = module.params.get("instance_family") |
| 418 |
host_recovery = module.params.get("host_recovery") |
| 419 |
outpost_arn = module.params.get("outpost_arn") |
| 420 |
host_maintenance = module.params.get("host_maintenance") |
| 421 |
auto_placement = module.params.get("auto_placement") |
| 422 |
asset_ids = module.params.get("asset_ids") |
| 423 |
client_token = module.params.get("client_token") |
| 424 |
instance_type = module.params.get("instance_type") |
| 425 |
quantity = module.params.get("quantity") |
| 426 |
|
| 427 |
params = {} |
| 428 |
if instance_family: |
| 429 |
params["InstanceFamily"] = instance_family |
| 430 |
if host_recovery: |
| 431 |
params["HostRecovery"] = host_recovery |
| 432 |
if outpost_arn: |
| 433 |
params["OutpostArn"] = outpost_arn |
| 434 |
if host_maintenance: |
| 435 |
params["HostMaintenance"] = host_maintenance |
| 436 |
if asset_ids: |
| 437 |
params["AssetIds"] = asset_ids |
| 438 |
if client_token: |
| 439 |
params["ClientToken"] = client_token |
| 440 |
if instance_type: |
| 441 |
params["InstanceType"] = instance_type |
| 442 |
if quantity: |
| 443 |
params["Quantity"] = quantity |
| 444 |
|
| 445 |
host_id = allocate_ec2_dedicated_hosts(client, availability_zone=availability_zone, **params)[0] |
| 446 |
changed = True |
| 447 |
|
| 448 |
# Ensure tags |
| 449 |
tags = module.params.get("tags") |
| 450 |
purge_tags = module.params.get("purge_tags") |
| 451 |
changed |= ensure_ec2_tags(client, module, host_id, tags=tags, purge_tags=purge_tags) |
| 452 |
|
| 453 |
host_info = describe_host(client, host_id) |
| 454 |
module.exit_json(changed=changed, host=format_output(host_info)) |
| 455 |
|
| 456 |
|
| 457 |
def main(): |
| 458 |
module = AnsibleAWSModule( |
| 459 |
argument_spec=dict( |
| 460 |
state=dict(type="str", choices=["present", "absent"], default="present"), |
| 461 |
availability_zone=dict(type="str", required=False), |
| 462 |
host_id=dict(), |
| 463 |
lookup=dict(type="str", choices=["tag", "host_id"], default="tag"), |
| 464 |
instance_family=dict(type="str"), |
| 465 |
host_recovery=dict(type="str", choices=["on", "off"], default="off"), |
| 466 |
outpost_arn=dict(), |
| 467 |
host_maintenance=dict(type="str", choices=["on", "off"], default="off"), |
| 468 |
auto_placement=dict(type="str", choices=["on", "off"], default="off"), |
| 469 |
asset_ids=dict(type="list", elements="str"), |
| 470 |
client_token=dict(no_log=False), |
| 471 |
instance_type=dict(), |
| 472 |
quantity=dict(type="int"), |
| 473 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 474 |
purge_tags=dict(type="bool", default=True), |
| 475 |
), |
| 476 |
supports_check_mode=True, |
| 477 |
mutually_exclusive=[["instance_family", "instance_type"]], |
| 478 |
required_if=[ |
| 479 |
["state", "present", ["availability_zone", "quantity"]], |
| 480 |
["state", "absent", ["host_id"]], |
| 481 |
["lookup", "host_id", ["host_id"]], |
| 482 |
], |
| 483 |
) |
| 484 |
|
| 485 |
client = module.client("ec2") |
| 486 |
state = module.params.get("state") |
| 487 |
|
| 488 |
existing = get_ec2_dedicated_host(client, module) |
| 489 |
|
| 490 |
try: |
| 491 |
if state == "absent": |
| 492 |
# Release EC2 dedicated host |
| 493 |
release_host(client, module, existing) |
| 494 |
else: |
| 495 |
create_or_update_host(client, module, existing) |
| 496 |
except AnsibleEC2Error as e: |
| 497 |
module.fail_json_aws_error(e) |
| 498 |
|
| 499 |
|
| 500 |
if __name__ == "__main__": |
| 501 |
main() |