| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# This file is part of Ansible |
| 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: cloudfront_distribution_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Obtain facts about an AWS CloudFront distribution |
| 12 |
description: |
| 13 |
- Gets information about an AWS CloudFront distribution. |
| 14 |
- Prior to release 5.0.0 this module was called C(community.aws.cloudfront_info). |
| 15 |
The usage did not change. |
| 16 |
author: |
| 17 |
- Willem van Ketwich (@wilvk) |
| 18 |
options: |
| 19 |
distribution_id: |
| 20 |
description: |
| 21 |
- The id of the CloudFront distribution. Used with I(distribution), I(distribution_config), |
| 22 |
I(invalidation), I(streaming_distribution), I(streaming_distribution_config), I(list_invalidations). |
| 23 |
required: false |
| 24 |
type: str |
| 25 |
invalidation_id: |
| 26 |
description: |
| 27 |
- The id of the invalidation to get information about. |
| 28 |
- Used with I(invalidation). |
| 29 |
required: false |
| 30 |
type: str |
| 31 |
origin_access_identity_id: |
| 32 |
description: |
| 33 |
- The id of the CloudFront origin access identity to get information about. |
| 34 |
required: false |
| 35 |
type: str |
| 36 |
# web_acl_id: |
| 37 |
# description: |
| 38 |
# - Used with I(list_distributions_by_web_acl_id). |
| 39 |
# required: false |
| 40 |
# type: str |
| 41 |
domain_name_alias: |
| 42 |
description: |
| 43 |
- Can be used instead of I(distribution_id) - uses the aliased CNAME for the CloudFront |
| 44 |
distribution to get the distribution id where required. |
| 45 |
required: false |
| 46 |
type: str |
| 47 |
all_lists: |
| 48 |
description: |
| 49 |
- Get all CloudFront lists that do not require parameters. |
| 50 |
required: false |
| 51 |
default: false |
| 52 |
type: bool |
| 53 |
origin_access_identity: |
| 54 |
description: |
| 55 |
- Get information about an origin access identity. |
| 56 |
- Requires I(origin_access_identity_id) to be specified. |
| 57 |
required: false |
| 58 |
default: false |
| 59 |
type: bool |
| 60 |
origin_access_identity_config: |
| 61 |
description: |
| 62 |
- Get the configuration information about an origin access identity. |
| 63 |
- Requires I(origin_access_identity_id) to be specified. |
| 64 |
required: false |
| 65 |
default: false |
| 66 |
type: bool |
| 67 |
distribution: |
| 68 |
description: |
| 69 |
- Get information about a distribution. |
| 70 |
- Requires I(distribution_id) or I(domain_name_alias) to be specified. |
| 71 |
required: false |
| 72 |
default: false |
| 73 |
type: bool |
| 74 |
distribution_config: |
| 75 |
description: |
| 76 |
- Get the configuration information about a distribution. |
| 77 |
- Requires I(distribution_id) or I(domain_name_alias) to be specified. |
| 78 |
required: false |
| 79 |
default: false |
| 80 |
type: bool |
| 81 |
invalidation: |
| 82 |
description: |
| 83 |
- Get information about an invalidation. |
| 84 |
- Requires I(invalidation_id) to be specified. |
| 85 |
required: false |
| 86 |
default: false |
| 87 |
type: bool |
| 88 |
streaming_distribution: |
| 89 |
description: |
| 90 |
- Get information about a specified RTMP distribution. |
| 91 |
- Requires I(distribution_id) or I(domain_name_alias) to be specified. |
| 92 |
required: false |
| 93 |
default: false |
| 94 |
type: bool |
| 95 |
streaming_distribution_config: |
| 96 |
description: |
| 97 |
- Get the configuration information about a specified RTMP distribution. |
| 98 |
- Requires I(distribution_id) or I(domain_name_alias) to be specified. |
| 99 |
required: false |
| 100 |
default: false |
| 101 |
type: bool |
| 102 |
list_origin_access_identities: |
| 103 |
description: |
| 104 |
- Get a list of CloudFront origin access identities. |
| 105 |
- Requires I(origin_access_identity_id) to be set. |
| 106 |
required: false |
| 107 |
default: false |
| 108 |
type: bool |
| 109 |
list_distributions: |
| 110 |
description: |
| 111 |
- Get a list of CloudFront distributions. |
| 112 |
required: false |
| 113 |
default: false |
| 114 |
type: bool |
| 115 |
list_distributions_by_web_acl_id: |
| 116 |
description: |
| 117 |
- Get a list of distributions using web acl id as a filter. |
| 118 |
- Requires I(web_acl_id) to be set. |
| 119 |
required: false |
| 120 |
default: false |
| 121 |
type: bool |
| 122 |
list_invalidations: |
| 123 |
description: |
| 124 |
- Get a list of invalidations. |
| 125 |
- Requires I(distribution_id) or I(domain_name_alias) to be specified. |
| 126 |
required: false |
| 127 |
default: false |
| 128 |
type: bool |
| 129 |
list_streaming_distributions: |
| 130 |
description: |
| 131 |
- Get a list of streaming distributions. |
| 132 |
required: false |
| 133 |
default: false |
| 134 |
type: bool |
| 135 |
summary: |
| 136 |
description: |
| 137 |
- Returns a summary of all distributions, streaming distributions and origin_access_identities. |
| 138 |
- This is the default behaviour if no option is selected. |
| 139 |
required: false |
| 140 |
default: false |
| 141 |
type: bool |
| 142 |
|
| 143 |
extends_documentation_fragment: |
| 144 |
- amazon.aws.common.modules |
| 145 |
- amazon.aws.region.modules |
| 146 |
- amazon.aws.boto3 |
| 147 |
""" |
| 148 |
|
| 149 |
EXAMPLES = r""" |
| 150 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 151 |
|
| 152 |
- name: Get a summary of distributions |
| 153 |
community.aws.cloudfront_distribution_info: |
| 154 |
summary: true |
| 155 |
register: result |
| 156 |
|
| 157 |
- name: Get information about a distribution |
| 158 |
community.aws.cloudfront_distribution_info: |
| 159 |
distribution: true |
| 160 |
distribution_id: my-cloudfront-distribution-id |
| 161 |
register: result_did |
| 162 |
- ansible.builtin.debug: |
| 163 |
msg: "{{ result_did['cloudfront']['my-cloudfront-distribution-id'] }}" |
| 164 |
|
| 165 |
- name: Get information about a distribution using the CNAME of the cloudfront distribution. |
| 166 |
community.aws.cloudfront_distribution_info: |
| 167 |
distribution: true |
| 168 |
domain_name_alias: www.my-website.com |
| 169 |
register: result_website |
| 170 |
- ansible.builtin.debug: |
| 171 |
msg: "{{ result_website['cloudfront']['www.my-website.com'] }}" |
| 172 |
|
| 173 |
- name: Get all information about an invalidation for a distribution. |
| 174 |
community.aws.cloudfront_distribution_info: |
| 175 |
invalidation: true |
| 176 |
distribution_id: my-cloudfront-distribution-id |
| 177 |
invalidation_id: my-cloudfront-invalidation-id |
| 178 |
|
| 179 |
- name: Get all information about a CloudFront origin access identity. |
| 180 |
community.aws.cloudfront_distribution_info: |
| 181 |
origin_access_identity: true |
| 182 |
origin_access_identity_id: my-cloudfront-origin-access-identity-id |
| 183 |
|
| 184 |
- name: Get all information about lists not requiring parameters (ie. list_origin_access_identities, list_distributions, list_streaming_distributions) |
| 185 |
community.aws.cloudfront_distribution_info: |
| 186 |
origin_access_identity: true |
| 187 |
origin_access_identity_id: my-cloudfront-origin-access-identity-id |
| 188 |
|
| 189 |
- name: Get all information about lists not requiring parameters (ie. list_origin_access_identities, list_distributions, list_streaming_distributions) |
| 190 |
community.aws.cloudfront_distribution_info: |
| 191 |
all_lists: true |
| 192 |
""" |
| 193 |
|
| 194 |
RETURN = r""" |
| 195 |
origin_access_identity: |
| 196 |
description: Describes the origin access identity information. Requires I(origin_access_identity_id) to be set. |
| 197 |
returned: only if I(origin_access_identity) is true |
| 198 |
type: dict |
| 199 |
origin_access_identity_configuration: |
| 200 |
description: Describes the origin access identity information configuration information. Requires I(origin_access_identity_id) to be set. |
| 201 |
returned: only if I(origin_access_identity_configuration) is true |
| 202 |
type: dict |
| 203 |
distribution: |
| 204 |
description: > |
| 205 |
Facts about a CloudFront distribution. Requires I(distribution_id) or I(domain_name_alias) |
| 206 |
to be specified. Requires I(origin_access_identity_id) to be set. |
| 207 |
returned: only if distribution is true |
| 208 |
type: dict |
| 209 |
distribution_config: |
| 210 |
description: > |
| 211 |
Facts about a CloudFront distribution's config. Requires I(distribution_id) or I(domain_name_alias) |
| 212 |
to be specified. |
| 213 |
returned: only if I(distribution_config) is true |
| 214 |
type: dict |
| 215 |
invalidation: |
| 216 |
description: > |
| 217 |
Describes the invalidation information for the distribution. Requires |
| 218 |
I(invalidation_id) to be specified and either I(distribution_id) or I(domain_name_alias.) |
| 219 |
returned: only if invalidation is true |
| 220 |
type: dict |
| 221 |
streaming_distribution: |
| 222 |
description: > |
| 223 |
Describes the streaming information for the distribution. Requires |
| 224 |
I(distribution_id) or I(domain_name_alias) to be specified. |
| 225 |
returned: only if I(streaming_distribution) is true |
| 226 |
type: dict |
| 227 |
streaming_distribution_config: |
| 228 |
description: > |
| 229 |
Describes the streaming configuration information for the distribution. |
| 230 |
Requires I(distribution_id) or I(domain_name_alias) to be specified. |
| 231 |
returned: only if I(streaming_distribution_config) is true |
| 232 |
type: dict |
| 233 |
summary: |
| 234 |
description: Gives a summary of distributions, streaming distributions and origin access identities. |
| 235 |
returned: as default or if summary is true |
| 236 |
type: dict |
| 237 |
result: |
| 238 |
description: > |
| 239 |
Result dict not nested under the CloudFront ID to access results of module without the knowledge of that id |
| 240 |
as figuring out the DistributionId is usually the reason one uses this module in the first place. |
| 241 |
returned: always |
| 242 |
type: dict |
| 243 |
""" |
| 244 |
|
| 245 |
from ansible_collections.amazon.aws.plugins.module_utils.cloudfront_facts import CloudFrontFactsServiceManager |
| 246 |
|
| 247 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 248 |
|
| 249 |
|
| 250 |
def set_facts_for_distribution_id_and_alias(details, facts, distribution_id, aliases): |
| 251 |
facts[distribution_id] = details |
| 252 |
# also have a fixed key for accessing results/details returned |
| 253 |
facts["result"] = details |
| 254 |
facts["result"]["DistributionId"] = distribution_id |
| 255 |
|
| 256 |
for alias in aliases: |
| 257 |
facts[alias] = details |
| 258 |
return facts |
| 259 |
|
| 260 |
|
| 261 |
def main(): |
| 262 |
argument_spec = dict( |
| 263 |
distribution_id=dict(required=False, type="str"), |
| 264 |
invalidation_id=dict(required=False, type="str"), |
| 265 |
origin_access_identity_id=dict(required=False, type="str"), |
| 266 |
domain_name_alias=dict(required=False, type="str"), |
| 267 |
all_lists=dict(required=False, default=False, type="bool"), |
| 268 |
distribution=dict(required=False, default=False, type="bool"), |
| 269 |
distribution_config=dict(required=False, default=False, type="bool"), |
| 270 |
origin_access_identity=dict(required=False, default=False, type="bool"), |
| 271 |
origin_access_identity_config=dict(required=False, default=False, type="bool"), |
| 272 |
invalidation=dict(required=False, default=False, type="bool"), |
| 273 |
streaming_distribution=dict(required=False, default=False, type="bool"), |
| 274 |
streaming_distribution_config=dict(required=False, default=False, type="bool"), |
| 275 |
list_origin_access_identities=dict(required=False, default=False, type="bool"), |
| 276 |
list_distributions=dict(required=False, default=False, type="bool"), |
| 277 |
list_distributions_by_web_acl_id=dict(required=False, default=False, type="bool"), |
| 278 |
list_invalidations=dict(required=False, default=False, type="bool"), |
| 279 |
list_streaming_distributions=dict(required=False, default=False, type="bool"), |
| 280 |
summary=dict(required=False, default=False, type="bool"), |
| 281 |
) |
| 282 |
|
| 283 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 284 |
|
| 285 |
service_mgr = CloudFrontFactsServiceManager(module) |
| 286 |
|
| 287 |
distribution_id = module.params.get("distribution_id") |
| 288 |
invalidation_id = module.params.get("invalidation_id") |
| 289 |
origin_access_identity_id = module.params.get("origin_access_identity_id") |
| 290 |
web_acl_id = module.params.get("web_acl_id") |
| 291 |
domain_name_alias = module.params.get("domain_name_alias") |
| 292 |
all_lists = module.params.get("all_lists") |
| 293 |
distribution = module.params.get("distribution") |
| 294 |
distribution_config = module.params.get("distribution_config") |
| 295 |
origin_access_identity = module.params.get("origin_access_identity") |
| 296 |
origin_access_identity_config = module.params.get("origin_access_identity_config") |
| 297 |
invalidation = module.params.get("invalidation") |
| 298 |
streaming_distribution = module.params.get("streaming_distribution") |
| 299 |
streaming_distribution_config = module.params.get("streaming_distribution_config") |
| 300 |
list_origin_access_identities = module.params.get("list_origin_access_identities") |
| 301 |
list_distributions = module.params.get("list_distributions") |
| 302 |
list_distributions_by_web_acl_id = module.params.get("list_distributions_by_web_acl_id") |
| 303 |
list_invalidations = module.params.get("list_invalidations") |
| 304 |
list_streaming_distributions = module.params.get("list_streaming_distributions") |
| 305 |
summary = module.params.get("summary") |
| 306 |
|
| 307 |
aliases = [] |
| 308 |
result = {"cloudfront": {}} |
| 309 |
facts = {} |
| 310 |
|
| 311 |
require_distribution_id = ( |
| 312 |
distribution |
| 313 |
or distribution_config |
| 314 |
or invalidation |
| 315 |
or streaming_distribution |
| 316 |
or streaming_distribution_config |
| 317 |
or list_invalidations |
| 318 |
) |
| 319 |
|
| 320 |
# set default to summary if no option specified |
| 321 |
summary = summary or not ( |
| 322 |
distribution |
| 323 |
or distribution_config |
| 324 |
or origin_access_identity |
| 325 |
or origin_access_identity_config |
| 326 |
or invalidation |
| 327 |
or streaming_distribution |
| 328 |
or streaming_distribution_config |
| 329 |
or list_origin_access_identities |
| 330 |
or list_distributions_by_web_acl_id |
| 331 |
or list_invalidations |
| 332 |
or list_streaming_distributions |
| 333 |
or list_distributions |
| 334 |
) |
| 335 |
|
| 336 |
# validations |
| 337 |
if require_distribution_id and distribution_id is None and domain_name_alias is None: |
| 338 |
module.fail_json(msg="Error distribution_id or domain_name_alias have not been specified.") |
| 339 |
if invalidation and invalidation_id is None: |
| 340 |
module.fail_json(msg="Error invalidation_id has not been specified.") |
| 341 |
if (origin_access_identity or origin_access_identity_config) and origin_access_identity_id is None: |
| 342 |
module.fail_json(msg="Error origin_access_identity_id has not been specified.") |
| 343 |
if list_distributions_by_web_acl_id and web_acl_id is None: |
| 344 |
module.fail_json(msg="Error web_acl_id has not been specified.") |
| 345 |
|
| 346 |
# get distribution id from domain name alias |
| 347 |
if require_distribution_id and distribution_id is None: |
| 348 |
distribution_id = service_mgr.get_distribution_id_from_domain_name(domain_name_alias) |
| 349 |
if not distribution_id: |
| 350 |
module.fail_json(msg="Error unable to source a distribution id from domain_name_alias") |
| 351 |
|
| 352 |
# set appropriate cloudfront id |
| 353 |
if invalidation_id is not None and invalidation: |
| 354 |
facts.update({invalidation_id: {}}) |
| 355 |
if origin_access_identity_id and (origin_access_identity or origin_access_identity_config): |
| 356 |
facts.update({origin_access_identity_id: {}}) |
| 357 |
if web_acl_id: |
| 358 |
facts.update({web_acl_id: {}}) |
| 359 |
|
| 360 |
# get details based on options |
| 361 |
if distribution: |
| 362 |
facts_to_set = service_mgr.get_distribution(id=distribution_id) |
| 363 |
if distribution_config: |
| 364 |
facts_to_set = service_mgr.get_distribution_config(id=distribution_id) |
| 365 |
if origin_access_identity: |
| 366 |
facts[origin_access_identity_id].update(service_mgr.get_origin_access_identity(id=origin_access_identity_id)) |
| 367 |
if origin_access_identity_config: |
| 368 |
facts[origin_access_identity_id].update( |
| 369 |
service_mgr.get_origin_access_identity_config(id=origin_access_identity_id) |
| 370 |
) |
| 371 |
if invalidation: |
| 372 |
facts_to_set = service_mgr.get_invalidation(distribution_id=distribution_id, id=invalidation_id) |
| 373 |
facts[invalidation_id].update(facts_to_set) |
| 374 |
if streaming_distribution: |
| 375 |
facts_to_set = service_mgr.get_streaming_distribution(id=distribution_id) |
| 376 |
if streaming_distribution_config: |
| 377 |
facts_to_set = service_mgr.get_streaming_distribution_config(id=distribution_id) |
| 378 |
if list_invalidations: |
| 379 |
invalidations = service_mgr.list_invalidations(distribution_id=distribution_id) or {} |
| 380 |
facts_to_set = {"invalidations": invalidations} |
| 381 |
if "facts_to_set" in vars(): |
| 382 |
aliases = service_mgr.get_aliases_from_distribution_id(distribution_id) |
| 383 |
facts = set_facts_for_distribution_id_and_alias(facts_to_set, facts, distribution_id, aliases) |
| 384 |
|
| 385 |
# get list based on options |
| 386 |
if all_lists or list_origin_access_identities: |
| 387 |
facts["origin_access_identities"] = service_mgr.list_origin_access_identities() or {} |
| 388 |
if all_lists or list_distributions: |
| 389 |
facts["distributions"] = service_mgr.list_distributions() or {} |
| 390 |
if all_lists or list_streaming_distributions: |
| 391 |
facts["streaming_distributions"] = service_mgr.list_streaming_distributions() or {} |
| 392 |
if list_distributions_by_web_acl_id: |
| 393 |
facts["distributions_by_web_acl_id"] = service_mgr.list_distributions_by_web_acl_id(web_acl_id=web_acl_id) or {} |
| 394 |
if list_invalidations: |
| 395 |
facts["invalidations"] = service_mgr.list_invalidations(distribution_id=distribution_id) or {} |
| 396 |
|
| 397 |
# default summary option |
| 398 |
if summary: |
| 399 |
facts["summary"] = service_mgr.summary() |
| 400 |
|
| 401 |
result["changed"] = False |
| 402 |
result["cloudfront"].update(facts) |
| 403 |
|
| 404 |
module.exit_json(msg="Retrieved CloudFront info.", **result) |
| 405 |
|
| 406 |
|
| 407 |
if __name__ == "__main__": |
| 408 |
main() |