| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2017 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 |
module: aws_az_info |
| 9 |
short_description: Gather information about availability zones in AWS |
| 10 |
version_added: 1.0.0 |
| 11 |
description: |
| 12 |
- Gather information about availability zones in AWS. |
| 13 |
author: 'Henrique Rodrigues (@Sodki)' |
| 14 |
options: |
| 15 |
filters: |
| 16 |
description: |
| 17 |
- A dict of filters to apply. |
| 18 |
- Each dict item consists of a filter key and a filter value. |
| 19 |
- See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeAvailabilityZones.html) for possible filters. |
| 20 |
- Filter names and values are case sensitive. |
| 21 |
- You can use underscores instead of dashes (-) in the filter keys. |
| 22 |
- Filter keys with underscores will take precedence in case of conflict. |
| 23 |
required: false |
| 24 |
default: {} |
| 25 |
type: dict |
| 26 |
extends_documentation_fragment: |
| 27 |
- amazon.aws.common.modules |
| 28 |
- amazon.aws.region.modules |
| 29 |
- amazon.aws.boto3 |
| 30 |
""" |
| 31 |
|
| 32 |
EXAMPLES = r""" |
| 33 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 34 |
|
| 35 |
- name: Gather information about all availability zones |
| 36 |
amazon.aws.aws_az_info: |
| 37 |
|
| 38 |
- name: Gather information about a single availability zone |
| 39 |
amazon.aws.aws_az_info: |
| 40 |
filters: |
| 41 |
zone-name: eu-west-1a |
| 42 |
|
| 43 |
- name: Gather information in a availability zones based on their state, such as "available" |
| 44 |
amazon.aws.aws_az_info: |
| 45 |
region: us-east-1 |
| 46 |
filters: |
| 47 |
state: available |
| 48 |
""" |
| 49 |
|
| 50 |
RETURN = r""" |
| 51 |
availability_zones: |
| 52 |
returned: on success |
| 53 |
description: > |
| 54 |
Availability zones that match the provided filters. Each element consists of a dict with all the information |
| 55 |
related to that available zone. |
| 56 |
type: list |
| 57 |
elements: dict |
| 58 |
contains: |
| 59 |
state: |
| 60 |
description: |
| 61 |
- The state of the availability zone. |
| 62 |
- The value is always C(available). |
| 63 |
type: str |
| 64 |
returned: on success |
| 65 |
sample: 'available' |
| 66 |
opt_in_status: |
| 67 |
description: |
| 68 |
- The opt-in status. |
| 69 |
- The value is always C(opt-in-not-required) for availability zones. |
| 70 |
type: str |
| 71 |
returned: on success |
| 72 |
sample: 'opt-in-not-required' |
| 73 |
messages: |
| 74 |
description: List of messages about the availability zone. |
| 75 |
type: list |
| 76 |
elements: dict |
| 77 |
contains: |
| 78 |
message: |
| 79 |
description: The message about the availability zone. |
| 80 |
type: str |
| 81 |
returned: on success |
| 82 |
sample: 'msg' |
| 83 |
returned: on success |
| 84 |
sample: [ |
| 85 |
{ |
| 86 |
'message': 'message_one' |
| 87 |
}, |
| 88 |
{ |
| 89 |
'message': 'message_two' |
| 90 |
} |
| 91 |
] |
| 92 |
region_name: |
| 93 |
description: The name of the region. |
| 94 |
type: str |
| 95 |
returned: on success |
| 96 |
sample: 'us-east-1' |
| 97 |
zone_name: |
| 98 |
description: The name of the availability zone. |
| 99 |
type: str |
| 100 |
returned: on success |
| 101 |
sample: 'us-east-1e' |
| 102 |
zone_id: |
| 103 |
description: The ID of the availability zone. |
| 104 |
type: str |
| 105 |
returned: on success |
| 106 |
sample: 'use1-az5' |
| 107 |
group_name: |
| 108 |
description: |
| 109 |
- The name of the associated group. |
| 110 |
- For availability zones, this will be the same as RV(availability_zones.region_name). |
| 111 |
type: str |
| 112 |
returned: on success |
| 113 |
sample: 'us-east-1' |
| 114 |
network_border_group: |
| 115 |
description: The name of the network border group. |
| 116 |
type: str |
| 117 |
returned: on success |
| 118 |
sample: 'us-east-1' |
| 119 |
zone_type: |
| 120 |
description: The type of zone. |
| 121 |
type: str |
| 122 |
returned: on success |
| 123 |
sample: 'availability-zone' |
| 124 |
sample: [ |
| 125 |
{ |
| 126 |
"group_name": "us-east-1", |
| 127 |
"messages": [], |
| 128 |
"network_border_group": "us-east-1", |
| 129 |
"opt_in_status": "opt-in-not-required", |
| 130 |
"region_name": "us-east-1", |
| 131 |
"state": "available", |
| 132 |
"zone_id": "use1-az6", |
| 133 |
"zone_name": "us-east-1a", |
| 134 |
"zone_type": "availability-zone" |
| 135 |
}, |
| 136 |
{ |
| 137 |
"group_name": "us-east-1", |
| 138 |
"messages": [], |
| 139 |
"network_border_group": "us-east-1", |
| 140 |
"opt_in_status": "opt-in-not-required", |
| 141 |
"region_name": "us-east-1", |
| 142 |
"state": "available", |
| 143 |
"zone_id": "use1-az1", |
| 144 |
"zone_name": "us-east-1b", |
| 145 |
"zone_type": "availability-zone" |
| 146 |
} |
| 147 |
] |
| 148 |
""" |
| 149 |
|
| 150 |
try: |
| 151 |
from botocore.exceptions import BotoCoreError |
| 152 |
from botocore.exceptions import ClientError |
| 153 |
except ImportError: |
| 154 |
pass # Handled by AnsibleAWSModule |
| 155 |
|
| 156 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 157 |
|
| 158 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_availability_zones |
| 159 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 160 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import sanitize_filters_to_boto3_filter_list |
| 161 |
|
| 162 |
|
| 163 |
def main(): |
| 164 |
argument_spec = dict(filters=dict(default={}, type="dict")) |
| 165 |
|
| 166 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 167 |
|
| 168 |
connection = module.client("ec2") |
| 169 |
|
| 170 |
# Sanitize filters |
| 171 |
sanitized_filters = sanitize_filters_to_boto3_filter_list(module.params.get("filters")) |
| 172 |
try: |
| 173 |
availability_zones = describe_availability_zones(connection, Filters=sanitized_filters) |
| 174 |
except (BotoCoreError, ClientError) as e: |
| 175 |
module.fail_json_aws(e, msg="Unable to describe availability zones.") |
| 176 |
|
| 177 |
# Turn the boto3 result into ansible_friendly_snaked_names |
| 178 |
snaked_availability_zones = [camel_dict_to_snake_dict(az) for az in availability_zones] |
| 179 |
|
| 180 |
module.exit_json(availability_zones=snaked_availability_zones) |
| 181 |
|
| 182 |
|
| 183 |
if __name__ == "__main__": |
| 184 |
main() |