-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2_availability_zones.py
143 lines (121 loc) · 4.23 KB
/
ec2_availability_zones.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'maintainer'
}
DOCUMENTATION = '''
---
module: ec2_avail_zones
short_description: Grab availability zone info.
version_added: "2.4"
description:
- "A module to grab all information about availability zones for a certain region"
options:
aws_access_key:
description:
- AWS Access Key
required: false
aws_secret_key:
description:
- AWS Secret Key
required: false
region:
description:
- What region you wish to query.
required: true
state:
choices: ['available','information','impaired','unavailable']
default: available
description:
- What state should the availability zone be in when queried.
required: false
'''
EXAMPLES = '''
# Get Info
- name: With Keys
ec2_avail_zones:
aws_access_key: "AccessKey"
aws_secret_key: "SecretKey"
region: "RegionName"
# Without keys
- name: Without Keys
ec2_avail_zones:
region: "RegionName"
'''
RETURN = '''
Messages:
Description: "Any messages attached to the zone."
RegionName:
Description: "Name of the Region."
State:
Description: "What state is the availability zone in"
ZoneName:
Description: "The name of the zone."
'''
from ansible.module_utils.basic import AnsibleModule
try:
import boto3
HAS_BOTO = True
except ImportError:
HAS_BOTO = False
def main():
if not HAS_BOTO:
module.fail_json(msg='boto3 required for this module')
# define the available arguments/parameters that a user can pass to
# the module
module_args = dict(
region=dict(type='str', required=True),
aws_access_key=dict(type='str', required=False),
aws_secret_key=dict(type='str', required=False),
aws_session_token=dict(type='str', required=False),
state=dict(type='str', required=False,default='available',
choices=['available','information','impaired','unavailable'])
)
# seed the result dict in the object
# we primarily care about changed and state
# change is if this module effectively modified the target
# state will include any data that you want your module to pass back
# for consumption, for example, in a subsequent task
result = dict(
changed=False,
zones=list()
)
# the AnsibleModule object will be our abstraction working with Ansible
# this includes instantiation, a couple of common attr would be the
# args/params passed to the execution, as well as if the module
# supports check mode
module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=False
)
# manipulate or modify the state as needed (this is going to be the
# part where your module will do what it needs to do)
if not module.params['region']:
module.fail_json(msg="Region not specified.")
try:
ec2 = boto3.setup_default_session(region_name=module.params['region'])
if module.params['aws_session_token'] and module.params['aws_access_key'] and module.params['aws_secret_key']:
ec2 = boto3.client('ec2',
aws_access_key_id=module.params['aws_access_key'],
aws_secret_access_key=module.params['aws_secret_key'],
aws_session_token=module.params['aws_session_token'])
elif module.params['aws_access_key'] and module.params['aws_secret_key']:
ec2 = boto3.client('ec2',
aws_access_key_id=module.params['aws_access_key'],
aws_secret_access_key=module.params['aws_secret_key'])
else:
ec2 = boto3.client('ec2')
state = list()
state.append(module.params['state'])
filters = [{'Name':'state','Values':state}]
result['zones'] = ec2.describe_availability_zones(Filters=filters)['AvailabilityZones']
result['changed'] = True
except Exception as e:
result['changed'] = False
module.fail_json(msg=e.message)
# in the event of a successful module execution, you will want to
# simple AnsibleModule.exit_json(), passing the key/value results
module.exit_json(**result)
if __name__ == '__main__':
main()