summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/cloud/amazon/ec2_elb_facts.py
blob: ead2a9bb4187b10f3ba278a94023f881927713c9 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/python
#
# This is a free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This Ansible library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this library.  If not, see <http://www.gnu.org/licenses/>.

ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}


DOCUMENTATION = '''
---
module: ec2_elb_facts
short_description: Gather facts about EC2 Elastic Load Balancers in AWS
description:
    - Gather facts about EC2 Elastic Load Balancers in AWS
version_added: "2.0"
author:
  - "Michael Schultz (github.com/mjschultz)"
  - "Fernando Jose Pando (@nand0p)"
options:
  names:
    description:
      - List of ELB names to gather facts about. Pass this option to gather facts about a set of ELBs, otherwise, all ELBs are returned.
    required: false
    default: null
    aliases: ['elb_ids', 'ec2_elbs']
extends_documentation_fragment:
    - aws
    - ec2
'''

EXAMPLES = '''
# Note: These examples do not set authentication details, see the AWS Guide for details.
# Output format tries to match ec2_elb_lb module input parameters

# Gather facts about all ELBs
- action:
    module: ec2_elb_facts
  register: elb_facts

- action:
    module: debug
    msg: "{{ item.dns_name }}"
  with_items: "{{ elb_facts.elbs }}"

# Gather facts about a particular ELB
- action:
    module: ec2_elb_facts
    names: frontend-prod-elb
  register: elb_facts

- action:
    module: debug
    msg: "{{ elb_facts.elbs.0.dns_name }}"

# Gather facts about a set of ELBs
- action:
    module: ec2_elb_facts
    names:
    - frontend-prod-elb
    - backend-prod-elb
  register: elb_facts

- action:
    module: debug
    msg: "{{ item.dns_name }}"
  with_items: "{{ elb_facts.elbs }}"

'''

import traceback

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ec2 import (
    AWSRetry,
    connect_to_aws,
    ec2_argument_spec,
    get_aws_connection_info,
)

try:
    import boto.ec2.elb
    from boto.ec2.tag import Tag
    from boto.exception import BotoServerError
    HAS_BOTO = True
except ImportError:
    HAS_BOTO = False


class ElbInformation(object):
    """Handles ELB information."""

    def __init__(self,
                 module,
                 names,
                 region,
                 **aws_connect_params):

        self.module = module
        self.names = names
        self.region = region
        self.aws_connect_params = aws_connect_params
        self.connection = self._get_elb_connection()

    def _get_tags(self, elbname):
        params = {'LoadBalancerNames.member.1': elbname}
        elb_tags = self.connection.get_list('DescribeTags', params, [('member', Tag)])
        return dict((tag.Key, tag.Value) for tag in elb_tags if hasattr(tag, 'Key'))

    @AWSRetry.backoff(tries=5, delay=5, backoff=2.0)
    def _get_elb_connection(self):
        return connect_to_aws(boto.ec2.elb, self.region, **self.aws_connect_params)

    def _get_elb_listeners(self, listeners):
        listener_list = []

        for listener in listeners:
            listener_dict = {
                'load_balancer_port': listener[0],
                'instance_port': listener[1],
                'protocol': listener[2],
            }

            try:
                ssl_certificate_id = listener[4]
            except IndexError:
                pass
            else:
                if ssl_certificate_id:
                    listener_dict['ssl_certificate_id'] = ssl_certificate_id

            listener_list.append(listener_dict)

        return listener_list

    def _get_health_check(self, health_check):
        protocol, port_path = health_check.target.split(':')
        try:
            port, path = port_path.split('/', 1)
            path = '/{0}'.format(path)
        except ValueError:
            port = port_path
            path = None

        health_check_dict = {
            'ping_protocol': protocol.lower(),
            'ping_port': int(port),
            'response_timeout': health_check.timeout,
            'interval': health_check.interval,
            'unhealthy_threshold': health_check.unhealthy_threshold,
            'healthy_threshold': health_check.healthy_threshold,
        }

        if path:
            health_check_dict['ping_path'] = path
        return health_check_dict

    @AWSRetry.backoff(tries=5, delay=5, backoff=2.0)
    def _get_elb_info(self, elb):
        elb_info = {
            'name': elb.name,
            'zones': elb.availability_zones,
            'dns_name': elb.dns_name,
            'canonical_hosted_zone_name': elb.canonical_hosted_zone_name,
            'canonical_hosted_zone_name_id': elb.canonical_hosted_zone_name_id,
            'hosted_zone_name': elb.canonical_hosted_zone_name,
            'hosted_zone_id': elb.canonical_hosted_zone_name_id,
            'instances': [instance.id for instance in elb.instances],
            'listeners': self._get_elb_listeners(elb.listeners),
            'scheme': elb.scheme,
            'security_groups': elb.security_groups,
            'health_check': self._get_health_check(elb.health_check),
            'subnets': elb.subnets,
            'instances_inservice': [],
            'instances_inservice_count': 0,
            'instances_outofservice': [],
            'instances_outofservice_count': 0,
            'instances_inservice_percent': 0.0,
            'tags': self._get_tags(elb.name)
        }

        if elb.vpc_id:
            elb_info['vpc_id'] = elb.vpc_id

        if elb.instances:
            instance_health = self.connection.describe_instance_health(elb.name)
            elb_info['instances_inservice'] = [inst.instance_id for inst in instance_health if inst.state == 'InService']
            elb_info['instances_inservice_count'] = len(elb_info['instances_inservice'])
            elb_info['instances_outofservice'] = [inst.instance_id for inst in instance_health if inst.state == 'OutOfService']
            elb_info['instances_outofservice_count'] = len(elb_info['instances_outofservice'])
            try:
                elb_info['instances_inservice_percent'] = (
                    float(elb_info['instances_inservice_count']) /
                    float(elb_info['instances_inservice_count'] + elb_info['instances_outofservice_count'])
                ) * 100.
            except ZeroDivisionError:
                elb_info['instances_inservice_percent'] = 0.
        return elb_info

    def list_elbs(self):
        elb_array, token = [], None
        get_elb_with_backoff = AWSRetry.backoff(tries=5, delay=5, backoff=2.0)(self.connection.get_all_load_balancers)
        while True:
            all_elbs = get_elb_with_backoff(marker=token)
            token = all_elbs.next_marker

            if all_elbs:
                if self.names:
                    for existing_lb in all_elbs:
                        if existing_lb.name in self.names:
                            elb_array.append(existing_lb)
                else:
                    elb_array.extend(all_elbs)
            else:
                break

            if token is None:
                break

        return list(map(self._get_elb_info, elb_array))


def main():
    argument_spec = ec2_argument_spec()
    argument_spec.update(dict(
        names={'default': [], 'type': 'list'}
    )
    )
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    if not HAS_BOTO:
        module.fail_json(msg='boto required for this module')

    try:
        region, ec2_url, aws_connect_params = get_aws_connection_info(module)
        if not region:
            module.fail_json(msg="region must be specified")

        names = module.params['names']
        elb_information = ElbInformation(
            module, names, region, **aws_connect_params)

        ec2_facts_result = dict(changed=False,
                                elbs=elb_information.list_elbs())

    except BotoServerError as err:
        module.fail_json(msg="{0}: {1}".format(err.error_code, err.error_message),
                         exception=traceback.format_exc())

    module.exit_json(**ec2_facts_result)


if __name__ == '__main__':
    main()