2015-08-20 18:11:36 +02:00
|
|
|
#!/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/>.
|
|
|
|
|
|
|
|
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"
|
2016-08-10 09:01:31 +02:00
|
|
|
author:
|
|
|
|
- "Michael Schultz (github.com/mjschultz)"
|
|
|
|
- "Fernando Jose Pando (@nand0p)"
|
2015-08-20 18:11:36 +02:00
|
|
|
options:
|
2015-08-24 04:11:47 +02:00
|
|
|
names:
|
2015-08-20 18:11:36 +02:00
|
|
|
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
|
2015-08-24 04:11:47 +02:00
|
|
|
aliases: ['elb_ids', 'ec2_elbs']
|
2015-10-08 05:17:50 +02:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- aws
|
|
|
|
- ec2
|
2015-08-20 18:11:36 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
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
|
2015-08-24 04:11:47 +02:00
|
|
|
names: frontend-prod-elb
|
2015-08-20 18:11:36 +02:00
|
|
|
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
|
2015-08-24 04:11:47 +02:00
|
|
|
names:
|
2015-08-20 18:11:36 +02:00
|
|
|
- frontend-prod-elb
|
|
|
|
- backend-prod-elb
|
|
|
|
register: elb_facts
|
|
|
|
|
|
|
|
- action:
|
|
|
|
module: debug
|
|
|
|
msg: "{{ item.dns_name }}"
|
|
|
|
with_items: elb_facts.elbs
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
import boto.ec2.elb
|
2016-08-10 09:01:31 +02:00
|
|
|
from boto.ec2.tag import Tag
|
2015-08-20 18:11:36 +02:00
|
|
|
from boto.exception import BotoServerError
|
|
|
|
HAS_BOTO = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_BOTO = False
|
|
|
|
|
2016-08-10 09:01:31 +02:00
|
|
|
class ElbInformation(object):
|
|
|
|
""" Handles ELB information """
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
module,
|
|
|
|
names,
|
|
|
|
region,
|
|
|
|
**aws_connect_params):
|
2015-08-20 18:11:36 +02:00
|
|
|
|
2016-08-10 09:01:31 +02:00
|
|
|
self.module = module
|
|
|
|
self.names = names
|
|
|
|
self.region = region
|
|
|
|
self.aws_connect_params = aws_connect_params
|
|
|
|
self.connection = self._get_elb_connection()
|
2015-08-20 18:11:36 +02:00
|
|
|
|
2016-08-10 09:01:31 +02:00
|
|
|
def _get_tags(self, elbname):
|
|
|
|
params = {'LoadBalancerNames.member.1': elbname}
|
|
|
|
try:
|
|
|
|
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'))
|
|
|
|
except:
|
|
|
|
return {}
|
2015-08-20 18:11:36 +02:00
|
|
|
|
2016-08-10 09:01:31 +02:00
|
|
|
def _get_elb_connection(self):
|
|
|
|
try:
|
|
|
|
return connect_to_aws(boto.ec2.elb, self.region, **self.aws_connect_params)
|
|
|
|
except BotoServerError as err:
|
|
|
|
self.module.fail_json(msg=err.message)
|
|
|
|
|
|
|
|
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 = '/{}'.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,
|
|
|
|
}
|
2015-08-20 18:11:36 +02:00
|
|
|
|
2016-08-10 09:01:31 +02:00
|
|
|
if path:
|
|
|
|
health_check_dict['ping_path'] = path
|
|
|
|
return health_check_dict
|
|
|
|
|
|
|
|
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)
|
2015-08-20 18:11:36 +02:00
|
|
|
}
|
2016-08-10 09:01:31 +02:00
|
|
|
|
|
|
|
if elb.vpc_id:
|
|
|
|
elb_info['vpc_id'] = elb.vpc_id
|
|
|
|
|
|
|
|
if elb.instances:
|
|
|
|
try:
|
|
|
|
instance_health = self.connection.describe_instance_health(elb.name)
|
|
|
|
except BotoServerError as err:
|
|
|
|
self.module.fail_json(msg=err.message)
|
|
|
|
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'])
|
|
|
|
elb_info['instances_inservice_percent'] = float(elb_info['instances_inservice_count'])/(
|
|
|
|
float(elb_info['instances_inservice_count']) +
|
|
|
|
float(elb_info['instances_outofservice_count']))*100
|
|
|
|
return elb_info
|
|
|
|
|
|
|
|
|
|
|
|
def list_elbs(self):
|
|
|
|
elb_array = []
|
|
|
|
|
2015-08-20 18:11:36 +02:00
|
|
|
try:
|
2016-08-10 09:01:31 +02:00
|
|
|
all_elbs = self.connection.get_all_load_balancers()
|
|
|
|
except BotoServerError as err:
|
|
|
|
self.module.fail_json(msg = "%s: %s" % (err.error_code, err.error_message))
|
|
|
|
|
|
|
|
if all_elbs:
|
|
|
|
for existing_lb in all_elbs:
|
|
|
|
if existing_lb.name in self.names:
|
|
|
|
elb_array.append(self._get_elb_info(existing_lb))
|
2015-08-20 18:11:36 +02:00
|
|
|
|
2016-08-10 09:01:31 +02:00
|
|
|
return elb_array
|
2015-08-20 18:11:36 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = ec2_argument_spec()
|
2016-08-10 09:01:31 +02:00
|
|
|
argument_spec.update(dict(
|
2015-08-24 04:11:47 +02:00
|
|
|
names={'default': None, 'type': 'list'}
|
2015-08-20 18:11:36 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec)
|
|
|
|
|
|
|
|
if not HAS_BOTO:
|
|
|
|
module.fail_json(msg='boto required for this module')
|
|
|
|
|
|
|
|
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
|
|
|
|
|
2016-08-10 09:01:31 +02:00
|
|
|
if not region:
|
2015-08-20 18:11:36 +02:00
|
|
|
module.fail_json(msg="region must be specified")
|
|
|
|
|
2016-08-10 09:01:31 +02:00
|
|
|
names = module.params['names']
|
|
|
|
elb_information = ElbInformation(module,
|
|
|
|
names,
|
|
|
|
region,
|
|
|
|
**aws_connect_params)
|
|
|
|
|
|
|
|
ec2_facts_result = dict(changed=False,
|
|
|
|
elbs=elb_information.list_elbs())
|
|
|
|
|
|
|
|
module.exit_json(**ec2_facts_result)
|
2015-08-20 18:11:36 +02:00
|
|
|
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.ec2 import *
|
|
|
|
|
2015-12-01 04:01:57 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|