| 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_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Gather information about DHCP options sets in AWS |
| 12 |
description: |
| 13 |
- Gather information about DHCP options sets in AWS. |
| 14 |
author: "Nick Aslanidis (@naslanidis)" |
| 15 |
options: |
| 16 |
filters: |
| 17 |
description: |
| 18 |
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. |
| 19 |
See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeDhcpOptions.html) for possible filters. |
| 20 |
type: dict |
| 21 |
default: {} |
| 22 |
dhcp_options_ids: |
| 23 |
description: |
| 24 |
- Get details of specific DHCP option IDs. |
| 25 |
type: list |
| 26 |
elements: str |
| 27 |
dry_run: |
| 28 |
description: |
| 29 |
- Checks whether you have the required permissions to view the DHCP |
| 30 |
options. |
| 31 |
type: bool |
| 32 |
default: false |
| 33 |
extends_documentation_fragment: |
| 34 |
- amazon.aws.common.modules |
| 35 |
- amazon.aws.region.modules |
| 36 |
- amazon.aws.boto3 |
| 37 |
""" |
| 38 |
|
| 39 |
EXAMPLES = r""" |
| 40 |
# # Note: These examples do not set authentication details, see the AWS Guide for details. |
| 41 |
|
| 42 |
- name: Gather information about all DHCP Option sets for an account or profile |
| 43 |
amazon.aws.ec2_vpc_dhcp_option_info: |
| 44 |
region: ap-southeast-2 |
| 45 |
profile: production |
| 46 |
register: dhcp_info |
| 47 |
|
| 48 |
- name: Gather information about a filtered list of DHCP Option sets |
| 49 |
amazon.aws.ec2_vpc_dhcp_option_info: |
| 50 |
region: ap-southeast-2 |
| 51 |
profile: production |
| 52 |
filters: |
| 53 |
"tag:Name": "abc-123" |
| 54 |
register: dhcp_info |
| 55 |
|
| 56 |
- name: Gather information about a specific DHCP Option set by DhcpOptionId |
| 57 |
amazon.aws.ec2_vpc_dhcp_option_info: |
| 58 |
region: ap-southeast-2 |
| 59 |
profile: production |
| 60 |
dhcp_options_ids: dopt-123fece2 |
| 61 |
register: dhcp_info |
| 62 |
""" |
| 63 |
|
| 64 |
RETURN = r""" |
| 65 |
dhcp_options: |
| 66 |
description: The DHCP options created, associated or found. |
| 67 |
returned: always |
| 68 |
type: list |
| 69 |
elements: dict |
| 70 |
contains: |
| 71 |
dhcp_configurations: |
| 72 |
description: The DHCP configuration for the option set. |
| 73 |
type: list |
| 74 |
elements: dict |
| 75 |
sample: |
| 76 |
- '{"key": "ntp-servers", "values": [{"value": "10.0.0.2" , "value": "10.0.1.2"}]}' |
| 77 |
- '{"key": "netbios-name-servers", "values": [{value": "10.0.0.1"}, {"value": "10.0.1.1" }]}' |
| 78 |
dhcp_options_id: |
| 79 |
description: The aws resource id of the primary DHCP options set created or found. |
| 80 |
type: str |
| 81 |
sample: "dopt-0955331de6a20dd07" |
| 82 |
owner_id: |
| 83 |
description: The ID of the AWS account that owns the DHCP options set. |
| 84 |
type: str |
| 85 |
sample: 012345678912 |
| 86 |
tags: |
| 87 |
description: The tags to be applied to a DHCP options set. |
| 88 |
type: list |
| 89 |
elements: dict |
| 90 |
sample: |
| 91 |
- '{"Key": "CreatedBy", "Value": "ansible-test"}' |
| 92 |
- '{"Key": "Collection", "Value": "amazon.aws"}' |
| 93 |
dhcp_config: |
| 94 |
description: |
| 95 |
- The boto2-style DHCP options created, associated or found. |
| 96 |
- This return value has been deprecated and will be removed in a release after |
| 97 |
2026-12-01. Use RV(dhcp_options) instead. |
| 98 |
returned: always |
| 99 |
type: list |
| 100 |
elements: dict |
| 101 |
contains: |
| 102 |
domain-name-servers: |
| 103 |
description: The IP addresses of up to four domain name servers, or AmazonProvidedDNS. |
| 104 |
returned: when available |
| 105 |
type: list |
| 106 |
sample: |
| 107 |
- 10.0.0.1 |
| 108 |
- 10.0.1.1 |
| 109 |
domain-name: |
| 110 |
description: The domain name for hosts in the DHCP option sets. |
| 111 |
returned: when available |
| 112 |
type: list |
| 113 |
sample: |
| 114 |
- "my.example.com" |
| 115 |
ntp-servers: |
| 116 |
description: The IP addresses of up to four Network Time Protocol (NTP) servers. |
| 117 |
returned: when available |
| 118 |
type: list |
| 119 |
sample: |
| 120 |
- 10.0.0.1 |
| 121 |
- 10.0.1.1 |
| 122 |
netbios-name-servers: |
| 123 |
description: The IP addresses of up to four NetBIOS name servers. |
| 124 |
returned: when available |
| 125 |
type: list |
| 126 |
sample: |
| 127 |
- 10.0.0.1 |
| 128 |
- 10.0.1.1 |
| 129 |
netbios-node-type: |
| 130 |
description: The NetBIOS node type (1, 2, 4, or 8). |
| 131 |
returned: when available |
| 132 |
type: str |
| 133 |
sample: 2 |
| 134 |
changed: |
| 135 |
description: True if listing the dhcp options succeeds. |
| 136 |
type: bool |
| 137 |
returned: always |
| 138 |
""" |
| 139 |
|
| 140 |
from typing import Any |
| 141 |
from typing import Dict |
| 142 |
from typing import List |
| 143 |
from typing import Tuple |
| 144 |
|
| 145 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 146 |
|
| 147 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 148 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_dhcp_options |
| 149 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import normalize_ec2_vpc_dhcp_config |
| 150 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 151 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 152 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 153 |
|
| 154 |
|
| 155 |
def list_dhcp_options(client, module: AnsibleAWSModule) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]: |
| 156 |
params = dict(Filters=ansible_dict_to_boto3_filter_list(module.params.get("filters"))) |
| 157 |
|
| 158 |
if module.params.get("dry_run"): |
| 159 |
params["DryRun"] = True |
| 160 |
|
| 161 |
if module.params.get("dhcp_options_ids"): |
| 162 |
params["DhcpOptionsIds"] = module.params.get("dhcp_options_ids") |
| 163 |
|
| 164 |
all_dhcp_options = describe_dhcp_options(client, **params) |
| 165 |
if not all_dhcp_options and module.params.get("dhcp_options_ids"): |
| 166 |
module.fail_json(msg="Requested DHCP options does not exist") |
| 167 |
|
| 168 |
normalized_config = [normalize_ec2_vpc_dhcp_config(config["DhcpConfigurations"]) for config in all_dhcp_options] |
| 169 |
raw_config = [ |
| 170 |
camel_dict_to_snake_dict( |
| 171 |
{ |
| 172 |
"DhcpOptionsId": option["DhcpOptionsId"], |
| 173 |
"DhcpConfigurations": option["DhcpConfigurations"], |
| 174 |
"Tags": boto3_tag_list_to_ansible_dict(option.get("Tags", [{"Value": "", "Key": "Name"}])), |
| 175 |
}, |
| 176 |
ignore_list=["Tags"], |
| 177 |
) |
| 178 |
for option in all_dhcp_options |
| 179 |
] |
| 180 |
return raw_config, normalized_config |
| 181 |
|
| 182 |
|
| 183 |
def main() -> None: |
| 184 |
argument_spec = dict( |
| 185 |
filters=dict(type="dict", default={}), |
| 186 |
dry_run=dict(type="bool", default=False), |
| 187 |
dhcp_options_ids=dict(type="list", elements="str"), |
| 188 |
) |
| 189 |
|
| 190 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 191 |
|
| 192 |
module.deprecate( |
| 193 |
"The 'dhcp_config' return value is deprecated. Use 'dhcp_options' instead.", |
| 194 |
date="2026-12-01", |
| 195 |
collection_name="amazon.aws", |
| 196 |
) |
| 197 |
|
| 198 |
client = module.client("ec2") |
| 199 |
|
| 200 |
# call your function here |
| 201 |
try: |
| 202 |
results, normalized_config = list_dhcp_options(client, module) |
| 203 |
except AnsibleEC2Error as e: |
| 204 |
module.fail_json_aws_error(e) |
| 205 |
|
| 206 |
module.exit_json(dhcp_options=results, dhcp_config=normalized_config) |
| 207 |
|
| 208 |
|
| 209 |
if __name__ == "__main__": |
| 210 |
main() |