| 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_vpc_dhcp_option |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manages DHCP Options, and can ensure the DHCP options for the given VPC match what's |
| 12 |
requested |
| 13 |
description: |
| 14 |
- This module removes, or creates DHCP option sets, and can associate them to a VPC. |
| 15 |
- Optionally, a new DHCP Options set can be created that converges a VPC's existing |
| 16 |
DHCP option set with values provided. |
| 17 |
- When dhcp_options_id is provided, the module will |
| 18 |
1. remove (with state='absent') |
| 19 |
2. ensure tags are applied (if state='present' and tags are provided |
| 20 |
3. attach it to a VPC (if state='present' and a vpc_id is provided. |
| 21 |
- If any of the optional values are missing, they will either be treated |
| 22 |
as a no-op (i.e., inherit what already exists for the VPC) |
| 23 |
- To remove existing options while inheriting, supply an empty value |
| 24 |
(e.g. set ntp_servers to [] if you want to remove them from the VPC's options) |
| 25 |
author: |
| 26 |
- "Joel Thompson (@joelthompson)" |
| 27 |
options: |
| 28 |
domain_name: |
| 29 |
description: |
| 30 |
- The domain name to set in the DHCP option sets. |
| 31 |
type: str |
| 32 |
dns_servers: |
| 33 |
description: |
| 34 |
- A list of IP addresses to set the DNS servers for the VPC to. |
| 35 |
type: list |
| 36 |
elements: str |
| 37 |
ntp_servers: |
| 38 |
description: |
| 39 |
- List of hosts to advertise as NTP servers for the VPC. |
| 40 |
type: list |
| 41 |
elements: str |
| 42 |
netbios_name_servers: |
| 43 |
description: |
| 44 |
- List of hosts to advertise as NetBIOS servers. |
| 45 |
type: list |
| 46 |
elements: str |
| 47 |
netbios_node_type: |
| 48 |
description: |
| 49 |
- NetBIOS node type to advertise in the DHCP options. |
| 50 |
The AWS recommendation is to use 2 (when using netbios name services) |
| 51 |
U(https://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_DHCP_Options.html) |
| 52 |
type: int |
| 53 |
vpc_id: |
| 54 |
description: |
| 55 |
- VPC ID to associate with the requested DHCP option set. |
| 56 |
- If no VPC ID is provided, and no matching option set is found then a new |
| 57 |
DHCP option set is created. |
| 58 |
type: str |
| 59 |
delete_old: |
| 60 |
description: |
| 61 |
- Whether to delete the old VPC DHCP option set when associating a new one. |
| 62 |
- This is primarily useful for debugging/development purposes when you |
| 63 |
want to quickly roll back to the old option set. Note that this setting |
| 64 |
will be ignored, and the old DHCP option set will be preserved, if it |
| 65 |
is in use by any other VPC. (Otherwise, AWS will return an error.) |
| 66 |
type: bool |
| 67 |
default: true |
| 68 |
inherit_existing: |
| 69 |
description: |
| 70 |
- For any DHCP options not specified in these parameters, whether to |
| 71 |
inherit them from the options set already applied to O(vpc_id), or to |
| 72 |
reset them to be empty. |
| 73 |
type: bool |
| 74 |
default: false |
| 75 |
dhcp_options_id: |
| 76 |
description: |
| 77 |
- The resource_id of an existing DHCP options set. |
| 78 |
If this is specified, then it will override other settings, except tags |
| 79 |
(which will be updated to match) |
| 80 |
type: str |
| 81 |
state: |
| 82 |
description: |
| 83 |
- create/assign or remove the DHCP options. |
| 84 |
If state is set to absent, then a DHCP options set matched either |
| 85 |
by id, or tags and options will be removed if possible. |
| 86 |
default: present |
| 87 |
choices: [ 'absent', 'present' ] |
| 88 |
type: str |
| 89 |
notes: |
| 90 |
- Support for O(purge_tags) was added in release 2.0.0. |
| 91 |
extends_documentation_fragment: |
| 92 |
- amazon.aws.common.modules |
| 93 |
- amazon.aws.region.modules |
| 94 |
- amazon.aws.tags |
| 95 |
- amazon.aws.boto3 |
| 96 |
""" |
| 97 |
|
| 98 |
RETURN = r""" |
| 99 |
changed: |
| 100 |
description: Whether the dhcp options were changed |
| 101 |
type: bool |
| 102 |
returned: always |
| 103 |
dhcp_options: |
| 104 |
description: The DHCP options created, associated or found |
| 105 |
returned: when available |
| 106 |
type: dict |
| 107 |
contains: |
| 108 |
dhcp_configurations: |
| 109 |
description: The DHCP configuration for the option set |
| 110 |
type: list |
| 111 |
sample: |
| 112 |
- '{"key": "ntp-servers", "values": [{"value": "10.0.0.2" , "value": "10.0.1.2"}]}' |
| 113 |
- '{"key": "netbios-name-servers", "values": [{value": "10.0.0.1"}, {"value": "10.0.1.1" }]}' |
| 114 |
dhcp_options_id: |
| 115 |
description: The aws resource id of the primary DCHP options set created or found |
| 116 |
type: str |
| 117 |
sample: "dopt-0955331de6a20dd07" |
| 118 |
owner_id: |
| 119 |
description: The ID of the AWS account that owns the DHCP options set. |
| 120 |
type: str |
| 121 |
sample: 012345678912 |
| 122 |
tags: |
| 123 |
description: The tags to be applied to a DHCP options set |
| 124 |
type: list |
| 125 |
sample: |
| 126 |
- '{"Key": "CreatedBy", "Value": "ansible-test"}' |
| 127 |
- '{"Key": "Collection", "Value": "amazon.aws"}' |
| 128 |
dhcp_options_id: |
| 129 |
description: The aws resource id of the primary DCHP options set created, found or removed |
| 130 |
type: str |
| 131 |
returned: when available |
| 132 |
dhcp_config: |
| 133 |
description: |
| 134 |
- The boto2-style DHCP options created, associated or found. |
| 135 |
- This return value has been deprecated and will be removed in a release after |
| 136 |
2026-12-01. Use RV(dhcp_options) instead. |
| 137 |
returned: when available |
| 138 |
type: dict |
| 139 |
contains: |
| 140 |
domain-name-servers: |
| 141 |
description: The IP addresses of up to four domain name servers, or AmazonProvidedDNS. |
| 142 |
returned: when available |
| 143 |
type: list |
| 144 |
sample: |
| 145 |
- 10.0.0.1 |
| 146 |
- 10.0.1.1 |
| 147 |
domain-name: |
| 148 |
description: The domain name for hosts in the DHCP option sets. |
| 149 |
returned: when available |
| 150 |
type: list |
| 151 |
sample: |
| 152 |
- "my.example.com" |
| 153 |
ntp-servers: |
| 154 |
description: The IP addresses of up to four Network Time Protocol (NTP) servers. |
| 155 |
returned: when available |
| 156 |
type: list |
| 157 |
sample: |
| 158 |
- 10.0.0.1 |
| 159 |
- 10.0.1.1 |
| 160 |
netbios-name-servers: |
| 161 |
description: The IP addresses of up to four NetBIOS name servers. |
| 162 |
returned: when available |
| 163 |
type: list |
| 164 |
sample: |
| 165 |
- 10.0.0.1 |
| 166 |
- 10.0.1.1 |
| 167 |
netbios-node-type: |
| 168 |
description: The NetBIOS node type (1, 2, 4, or 8). |
| 169 |
returned: when available |
| 170 |
type: str |
| 171 |
sample: 2 |
| 172 |
""" |
| 173 |
|
| 174 |
EXAMPLES = r""" |
| 175 |
# Completely overrides the VPC DHCP options associated with VPC vpc-123456 and deletes any existing |
| 176 |
# DHCP option set that may have been attached to that VPC. |
| 177 |
- amazon.aws.ec2_vpc_dhcp_option: |
| 178 |
domain_name: "foo.example.com" |
| 179 |
region: us-east-1 |
| 180 |
dns_servers: |
| 181 |
- 10.0.0.1 |
| 182 |
- 10.0.1.1 |
| 183 |
ntp_servers: |
| 184 |
- 10.0.0.2 |
| 185 |
- 10.0.1.2 |
| 186 |
netbios_name_servers: |
| 187 |
- 10.0.0.1 |
| 188 |
- 10.0.1.1 |
| 189 |
netbios_node_type: 2 |
| 190 |
vpc_id: vpc-123456 |
| 191 |
delete_old: true |
| 192 |
inherit_existing: false |
| 193 |
|
| 194 |
# Ensure the DHCP option set for the VPC has 10.0.0.4 and 10.0.1.4 as the specified DNS servers, but |
| 195 |
# keep any other existing settings. Also, keep the old DHCP option set around. |
| 196 |
- amazon.aws.ec2_vpc_dhcp_option: |
| 197 |
region: us-east-1 |
| 198 |
dns_servers: |
| 199 |
- "{{groups['dns-primary']}}" |
| 200 |
- "{{groups['dns-secondary']}}" |
| 201 |
vpc_id: vpc-123456 |
| 202 |
inherit_existing: true |
| 203 |
delete_old: false |
| 204 |
|
| 205 |
## Create a DHCP option set with 4.4.4.4 and 8.8.8.8 as the specified DNS servers, with tags |
| 206 |
## but do not assign to a VPC |
| 207 |
- amazon.aws.ec2_vpc_dhcp_option: |
| 208 |
region: us-east-1 |
| 209 |
dns_servers: |
| 210 |
- 4.4.4.4 |
| 211 |
- 8.8.8.8 |
| 212 |
tags: |
| 213 |
Name: google servers |
| 214 |
Environment: Test |
| 215 |
|
| 216 |
## Delete a DHCP options set that matches the tags and options specified |
| 217 |
- amazon.aws.ec2_vpc_dhcp_option: |
| 218 |
region: us-east-1 |
| 219 |
dns_servers: |
| 220 |
- 4.4.4.4 |
| 221 |
- 8.8.8.8 |
| 222 |
tags: |
| 223 |
Name: google servers |
| 224 |
Environment: Test |
| 225 |
state: absent |
| 226 |
|
| 227 |
## Associate a DHCP options set with a VPC by ID |
| 228 |
- amazon.aws.ec2_vpc_dhcp_option: |
| 229 |
region: us-east-1 |
| 230 |
dhcp_options_id: dopt-12345678 |
| 231 |
vpc_id: vpc-123456 |
| 232 |
""" |
| 233 |
|
| 234 |
from typing import Any |
| 235 |
from typing import Dict |
| 236 |
from typing import List |
| 237 |
from typing import Optional |
| 238 |
from typing import Tuple |
| 239 |
from typing import Union |
| 240 |
|
| 241 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 242 |
|
| 243 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 244 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import associate_dhcp_options |
| 245 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import create_dhcp_options |
| 246 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import delete_dhcp_options |
| 247 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_dhcp_options |
| 248 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpcs |
| 249 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags |
| 250 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import normalize_ec2_vpc_dhcp_config |
| 251 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 252 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list |
| 253 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 254 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_specifications |
| 255 |
|
| 256 |
|
| 257 |
def fetch_dhcp_options_for_vpc( |
| 258 |
client, module: AnsibleAWSModule, vpc_id: str |
| 259 |
) -> Tuple[Optional[List[Dict[str, Union[str, List[Dict[str, str]]]]]], Optional[str]]: |
| 260 |
try: |
| 261 |
vpcs = describe_vpcs(client, VpcIds=[vpc_id]) |
| 262 |
except AnsibleEC2Error as e: |
| 263 |
module.fail_json_aws(e, msg=f"Unable to describe vpc {vpc_id}") |
| 264 |
|
| 265 |
if len(vpcs) != 1: |
| 266 |
return None, None |
| 267 |
try: |
| 268 |
dhcp_options = describe_dhcp_options(client, DhcpOptionsIds=[vpcs[0]["DhcpOptionsId"]]) |
| 269 |
except AnsibleEC2Error as e: |
| 270 |
module.fail_json_aws(e, msg=f"Unable to describe dhcp option {vpcs[0]['DhcpOptionsId']}") |
| 271 |
|
| 272 |
if len(dhcp_options) != 1: |
| 273 |
return None, None |
| 274 |
return dhcp_options[0]["DhcpConfigurations"], dhcp_options[0]["DhcpOptionsId"] |
| 275 |
|
| 276 |
|
| 277 |
def remove_dhcp_options_by_id(client, module: AnsibleAWSModule, dhcp_options_id: str) -> bool: |
| 278 |
changed = False |
| 279 |
# First, check if this dhcp option is associated to any other vpcs |
| 280 |
try: |
| 281 |
associations = describe_vpcs(client, Filters=[{"Name": "dhcp-options-id", "Values": [dhcp_options_id]}]) |
| 282 |
except AnsibleEC2Error as e: |
| 283 |
module.fail_json_aws(e, msg=f"Unable to describe VPC associations for dhcp option id {dhcp_options_id}") |
| 284 |
if len(associations) > 0: |
| 285 |
return changed |
| 286 |
|
| 287 |
changed = True |
| 288 |
if not module.check_mode: |
| 289 |
try: |
| 290 |
changed = delete_dhcp_options(client, dhcp_options_id) |
| 291 |
except AnsibleEC2Error as e: |
| 292 |
module.fail_json_aws(e, msg=f"Unable to delete dhcp option {dhcp_options_id}") |
| 293 |
|
| 294 |
return changed |
| 295 |
|
| 296 |
|
| 297 |
def match_dhcp_options(client, module: AnsibleAWSModule, new_config: List[Dict[str, Any]]) -> Optional[str]: |
| 298 |
""" |
| 299 |
Returns a DhcpOptionsId if the module parameters match; else None |
| 300 |
Filter by tags, if any are specified |
| 301 |
""" |
| 302 |
try: |
| 303 |
all_dhcp_options = describe_dhcp_options(client) |
| 304 |
except AnsibleEC2Error as e: |
| 305 |
module.fail_json_aws(e, msg="Unable to describe dhcp options") |
| 306 |
|
| 307 |
for dopts in all_dhcp_options: |
| 308 |
if module.params["tags"]: |
| 309 |
# If we were given tags, try to match on them |
| 310 |
boto_tags = ansible_dict_to_boto3_tag_list(module.params["tags"]) |
| 311 |
if dopts["DhcpConfigurations"] == new_config and dopts["Tags"] == boto_tags: |
| 312 |
return dopts["DhcpOptionsId"] |
| 313 |
elif dopts["DhcpConfigurations"] == new_config: |
| 314 |
return dopts["DhcpOptionsId"] |
| 315 |
|
| 316 |
return None |
| 317 |
|
| 318 |
|
| 319 |
def create_dhcp_config(params: Dict[str, Any]) -> List[Dict[str, Any]]: |
| 320 |
""" |
| 321 |
Convert provided parameters into a DhcpConfigurations list that conforms to what the API returns: |
| 322 |
https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeDhcpOptions.html |
| 323 |
[{'Key': 'domain-name', |
| 324 |
'Values': [{'Value': 'us-west-2.compute.internal'}]}, |
| 325 |
{'Key': 'domain-name-servers', |
| 326 |
'Values': [{'Value': 'AmazonProvidedDNS'}]}, |
| 327 |
...], |
| 328 |
""" |
| 329 |
new_config = [] |
| 330 |
if params["domain_name"] is not None: |
| 331 |
new_config.append({"Key": "domain-name", "Values": [{"Value": params["domain_name"]}]}) |
| 332 |
if params["dns_servers"] is not None: |
| 333 |
dns_server_list = [] |
| 334 |
for server in params["dns_servers"]: |
| 335 |
dns_server_list.append({"Value": server}) |
| 336 |
new_config.append({"Key": "domain-name-servers", "Values": dns_server_list}) |
| 337 |
if params["ntp_servers"] is not None: |
| 338 |
ntp_server_list = [] |
| 339 |
for server in params["ntp_servers"]: |
| 340 |
ntp_server_list.append({"Value": server}) |
| 341 |
new_config.append({"Key": "ntp-servers", "Values": ntp_server_list}) |
| 342 |
if params["netbios_name_servers"] is not None: |
| 343 |
netbios_server_list = [] |
| 344 |
for server in params["netbios_name_servers"]: |
| 345 |
netbios_server_list.append({"Value": server}) |
| 346 |
new_config.append({"Key": "netbios-name-servers", "Values": netbios_server_list}) |
| 347 |
if params["netbios_node_type"] is not None: |
| 348 |
new_config.append({"Key": "netbios-node-type", "Values": params["netbios_node_type"]}) |
| 349 |
|
| 350 |
return new_config |
| 351 |
|
| 352 |
|
| 353 |
def create_dhcp_option_set( |
| 354 |
client, params: Dict[str, Any], new_config: List[Dict[str, Any]], check_mode: bool |
| 355 |
) -> Optional[str]: |
| 356 |
""" |
| 357 |
A CreateDhcpOptions object looks different than the object we create in create_dhcp_config() |
| 358 |
This is the only place we use it, so create it now |
| 359 |
https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateDhcpOptions.html |
| 360 |
We have to do this after inheriting any existing_config, so we need to start with the object |
| 361 |
that we made in create_dhcp_config(). |
| 362 |
normalize_config() gives us the nicest format to work with for this. |
| 363 |
""" |
| 364 |
desired_config = normalize_ec2_vpc_dhcp_config(new_config) |
| 365 |
create_config = [] |
| 366 |
tags_list = [] |
| 367 |
|
| 368 |
for option in ["domain-name", "domain-name-servers", "ntp-servers", "netbios-name-servers"]: |
| 369 |
if desired_config.get(option): |
| 370 |
create_config.append({"Key": option, "Values": desired_config[option]}) |
| 371 |
if desired_config.get("netbios-node-type"): |
| 372 |
# We need to listify this one |
| 373 |
create_config.append({"Key": "netbios-node-type", "Values": [desired_config["netbios-node-type"]]}) |
| 374 |
|
| 375 |
if params.get("tags"): |
| 376 |
tags_list = boto3_tag_specifications(params["tags"], ["dhcp-options"]) |
| 377 |
|
| 378 |
dhcp_options_id = None |
| 379 |
if not check_mode: |
| 380 |
dhcp_options = create_dhcp_options(client, DhcpConfigurations=create_config, TagSpecifications=tags_list) |
| 381 |
dhcp_options_id = dhcp_options["DhcpOptionsId"] |
| 382 |
return dhcp_options_id |
| 383 |
|
| 384 |
|
| 385 |
def find_opt_index(config: List[Dict[str, Any]], option: str) -> Optional[int]: |
| 386 |
return next((i for i, item in enumerate(config) if item["Key"] == option), None) |
| 387 |
|
| 388 |
|
| 389 |
def inherit_dhcp_config( |
| 390 |
existing_config: List[Dict[str, Any]], new_config: List[Dict[str, Any]] |
| 391 |
) -> Tuple[bool, List[Dict[str, Any]]]: |
| 392 |
""" |
| 393 |
Compare two DhcpConfigurations lists and apply existing options to unset parameters |
| 394 |
|
| 395 |
If there's an existing option config and the new option is not set or it's none, |
| 396 |
inherit the existing config. |
| 397 |
The configs are unordered lists of dicts with non-unique keys, so we have to find |
| 398 |
the right list index for a given config option first. |
| 399 |
""" |
| 400 |
changed = False |
| 401 |
for option in ["domain-name", "domain-name-servers", "ntp-servers", "netbios-name-servers", "netbios-node-type"]: |
| 402 |
existing_index = find_opt_index(existing_config, option) |
| 403 |
new_index = find_opt_index(new_config, option) |
| 404 |
# `if existing_index` evaluates to False on index 0, so be very specific and verbose |
| 405 |
if existing_index is not None and new_index is None: |
| 406 |
new_config.append(existing_config[existing_index]) |
| 407 |
changed = True |
| 408 |
|
| 409 |
return changed, new_config |
| 410 |
|
| 411 |
|
| 412 |
def get_dhcp_options_info(client, dhcp_options_id: Optional[str], check_mode: bool) -> Optional[Dict[str, Any]]: |
| 413 |
# Return boto3-style details, consistent with the _info module |
| 414 |
|
| 415 |
if check_mode and dhcp_options_id is None: |
| 416 |
# We can't describe without an option id, we might get here when creating a new option set in check_mode |
| 417 |
return None |
| 418 |
|
| 419 |
dhcp_options = describe_dhcp_options(client, DhcpOptionsIds=[dhcp_options_id]) |
| 420 |
dhcp_option_info = None |
| 421 |
if dhcp_options: |
| 422 |
dhcp_option_info = camel_dict_to_snake_dict( |
| 423 |
{ |
| 424 |
"DhcpOptionsId": dhcp_options[0]["DhcpOptionsId"], |
| 425 |
"DhcpConfigurations": dhcp_options[0]["DhcpConfigurations"], |
| 426 |
"Tags": boto3_tag_list_to_ansible_dict(dhcp_options[0].get("Tags", [{"Value": "", "Key": "Name"}])), |
| 427 |
}, |
| 428 |
ignore_list=["Tags"], |
| 429 |
) |
| 430 |
return dhcp_option_info |
| 431 |
|
| 432 |
|
| 433 |
def ensure_absent(client, module: AnsibleAWSModule, new_config: List[Dict[str, Any]]) -> None: |
| 434 |
dhcp_options_id = module.params["dhcp_options_id"] |
| 435 |
if not dhcp_options_id: |
| 436 |
# Look up the option id first by matching the supplied options |
| 437 |
dhcp_options_id = match_dhcp_options(client, module, new_config) |
| 438 |
changed = remove_dhcp_options_by_id(client, module, dhcp_options_id) |
| 439 |
module.exit_json(changed=changed, dhcp_options={}, dhcp_config={}) |
| 440 |
|
| 441 |
|
| 442 |
def ensure_present(client, module: AnsibleAWSModule, new_config: List[Dict[str, Any]]) -> None: |
| 443 |
vpc_id = module.params["vpc_id"] |
| 444 |
delete_old = module.params["delete_old"] |
| 445 |
inherit_existing = module.params["inherit_existing"] |
| 446 |
tags = module.params["tags"] |
| 447 |
purge_tags = module.params["purge_tags"] |
| 448 |
dhcp_options_id = module.params["dhcp_options_id"] |
| 449 |
changed = False |
| 450 |
existing_config = None |
| 451 |
existing_id = None |
| 452 |
|
| 453 |
if not dhcp_options_id: |
| 454 |
# If we were given a vpc_id then we need to look at the configuration on that |
| 455 |
if vpc_id: |
| 456 |
existing_config, existing_id = fetch_dhcp_options_for_vpc(client, module, vpc_id) |
| 457 |
# if we've been asked to inherit existing options, do that now |
| 458 |
if inherit_existing and existing_config: |
| 459 |
changed, new_config = inherit_dhcp_config(existing_config, new_config) |
| 460 |
# Do the vpc's dhcp options already match what we're asked for? if so we are done |
| 461 |
if existing_config: |
| 462 |
if new_config == existing_config: |
| 463 |
dhcp_options_id = existing_id |
| 464 |
if tags or purge_tags: |
| 465 |
changed |= ensure_ec2_tags( |
| 466 |
client, |
| 467 |
module, |
| 468 |
dhcp_options_id, |
| 469 |
resource_type="dhcp-options", |
| 470 |
tags=tags, |
| 471 |
purge_tags=purge_tags, |
| 472 |
) |
| 473 |
return_config = normalize_ec2_vpc_dhcp_config(new_config) |
| 474 |
results = get_dhcp_options_info(client, dhcp_options_id, module.check_mode) |
| 475 |
module.exit_json( |
| 476 |
changed=changed, |
| 477 |
dhcp_options_id=dhcp_options_id, |
| 478 |
dhcp_options=results, |
| 479 |
dhcp_config=return_config, |
| 480 |
) |
| 481 |
# If no vpc_id was given, or the options don't match then look for an existing set using tags |
| 482 |
dhcp_options_id = match_dhcp_options(client, module, new_config) |
| 483 |
|
| 484 |
else: |
| 485 |
# Now let's cover the case where there are existing options that we were told about by id |
| 486 |
# If a dhcp_options_id was supplied we don't look at options inside, just set tags (if given) |
| 487 |
# Preserve the boto2 module's behaviour of checking if the option set exists first, |
| 488 |
# and return the same error message if it does not |
| 489 |
dhcp_options = describe_dhcp_options(client, DhcpOptionsIds=[dhcp_options_id]) |
| 490 |
if not dhcp_options: |
| 491 |
module.fail_json(msg="a dhcp_options_id was supplied, but does not exist") |
| 492 |
|
| 493 |
if not dhcp_options_id: |
| 494 |
# If we still don't have an options ID, create it |
| 495 |
dhcp_options_id = create_dhcp_option_set(client, module.params, new_config, module.check_mode) |
| 496 |
changed = True |
| 497 |
else: |
| 498 |
if tags or purge_tags: |
| 499 |
changed |= ensure_ec2_tags( |
| 500 |
client, module, dhcp_options_id, resource_type="dhcp-options", tags=tags, purge_tags=purge_tags |
| 501 |
) |
| 502 |
|
| 503 |
# If we were given a vpc_id, then attach the options we now have to that before we finish |
| 504 |
if vpc_id: |
| 505 |
if module.check_mode: |
| 506 |
changed = True |
| 507 |
else: |
| 508 |
try: |
| 509 |
changed = associate_dhcp_options(client, dhcp_options_id=dhcp_options_id, vpc_id=vpc_id) |
| 510 |
except AnsibleEC2Error as e: |
| 511 |
module.fail_json_aws_error(e) |
| 512 |
|
| 513 |
if delete_old and existing_id: |
| 514 |
remove_dhcp_options_by_id(client, module, existing_id) |
| 515 |
|
| 516 |
return_config = normalize_ec2_vpc_dhcp_config(new_config) |
| 517 |
results = get_dhcp_options_info(client, dhcp_options_id, module.check_mode) |
| 518 |
module.exit_json(changed=changed, dhcp_options_id=dhcp_options_id, dhcp_options=results, dhcp_config=return_config) |
| 519 |
|
| 520 |
|
| 521 |
def main() -> None: |
| 522 |
argument_spec = dict( |
| 523 |
dhcp_options_id=dict(type="str", default=None), |
| 524 |
domain_name=dict(type="str", default=None), |
| 525 |
dns_servers=dict(type="list", elements="str", default=None), |
| 526 |
ntp_servers=dict(type="list", elements="str", default=None), |
| 527 |
netbios_name_servers=dict(type="list", elements="str", default=None), |
| 528 |
netbios_node_type=dict(type="int", default=None), |
| 529 |
vpc_id=dict(type="str", default=None), |
| 530 |
delete_old=dict(type="bool", default=True), |
| 531 |
inherit_existing=dict(type="bool", default=False), |
| 532 |
tags=dict(type="dict", default=None, aliases=["resource_tags"]), |
| 533 |
purge_tags=dict(default=True, type="bool"), |
| 534 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 535 |
) |
| 536 |
|
| 537 |
module = AnsibleAWSModule(argument_spec=argument_spec, check_boto3=False, supports_check_mode=True) |
| 538 |
|
| 539 |
module.deprecate( |
| 540 |
"The 'dhcp_config' return value is deprecated. Use 'dhcp_options' instead.", |
| 541 |
date="2026-12-01", |
| 542 |
collection_name="amazon.aws", |
| 543 |
) |
| 544 |
|
| 545 |
state = module.params["state"] |
| 546 |
new_config = create_dhcp_config(module.params) |
| 547 |
|
| 548 |
client = module.client("ec2") |
| 549 |
|
| 550 |
try: |
| 551 |
if state == "absent": |
| 552 |
ensure_absent(client, module, new_config) |
| 553 |
else: |
| 554 |
ensure_present(client, module, new_config) |
| 555 |
except AnsibleEC2Error as e: |
| 556 |
module.fail_json_aws_error(e) |
| 557 |
|
| 558 |
|
| 559 |
if __name__ == "__main__": |
| 560 |
main() |