ec2_elb 6.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/usr/bin/python
# This file is part of Ansible
#
# Ansible is 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.
#
# Ansible 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 Ansible.  If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = """
---
module: ec2_elb
20
short_description: De-registers or registers instances from EC2 ELB(s)
21
description:
22
  - This module de-registers or registers an AWS EC2 instance from the ELB(s)
23
    that it belongs to.
24 25
  - Returns fact "ec2_elbs" which is a list of elbs attached to the instance
    if state=absent is passed as an argument.
John Jarvis committed
26
  - Will be marked changed when called only if there are ELBs found to operate on.
27 28 29 30
version_added: "1.2"
requirements: [ "boto" ]
author: John Jarvis
options:
31
  state:
32 33 34 35 36 37 38
    description:
      - register or deregister the instance
    required: true

  instance_id:
    description:
      - EC2 Instance ID
39
    required: true
40

41
  ec2_elbs:
42
    description:
43
      - List of ELB names, required for registration. The ec2_elbs fact should be used if there was a previous de-register.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    required: false
    default: None
  ec2_secret_key:
    description:
      - AWS Secret API key
    required: false
    default: None
  ec2_access_key:
    description:
      - AWS Access API key
    required: false
    default: None

"""

EXAMPLES = """
# basic pre_task and post_task example
pre_tasks:
  - name: Gathering ec2 facts
    ec2_facts:
  - name: Instance De-register
    local_action: ec2_elb
    args:
      instance_id: "{{ ansible_ec2_instance_id }}"
68
      state: 'absent'
69 70 71 72 73 74 75
roles:
  - myrole
post_tasks:
  - name: Instance Register
    local_action: ec2_elb
    args:
      instance_id: "{{ ansible_ec2_instance_id }}"
76 77
      ec2_elbs: "{{ ec2_elbs }}"
      state: 'present'
78 79 80 81 82 83 84 85 86 87 88 89 90 91
"""

import time
import sys
import os

try:
    import boto
except ImportError:
    print "failed=True msg='boto required for this module'"
    sys.exit(1)


class ElbManager:
92
    """Handles EC2 instance ELB registration and de-registration"""
93

94
    def __init__(self, module, instance_id=None, ec2_elbs=None,
95 96 97 98 99
                 ec2_access_key=None, ec2_secret_key=None):
        self.ec2_access_key = ec2_access_key
        self.ec2_secret_key = ec2_secret_key
        self.module = module
        self.instance_id = instance_id
100
        self.lbs = self._get_instance_lbs(ec2_elbs)
101 102 103 104 105 106
        # if there are no ELBs to operate on
        # there will be no changes made
        if len(self.lbs) > 0:
            self.changed = True
        else:
            self.changed = False
107 108

    def deregister(self):
109
        """De-register the instance from all ELBs and wait for the ELB
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
        to report it out-of-service"""

        for lb in self.lbs:
            lb.deregister_instances([self.instance_id])
            self._await_elb_instance_state(lb, 'OutOfService')

    def register(self):
        """Register the instance for all ELBs and wait for the ELB
        to report the instance in-service"""

        for lb in self.lbs:
            lb.register_instances([self.instance_id])
            self._await_elb_instance_state(lb, 'InService')

    def _await_elb_instance_state(self, lb, awaited_state):
        """Wait for an ELB to change state
        lb: load balancer
        awaited_state : state to poll for (string)"""

        while True:
            state = lb.get_instance_health([self.instance_id])[0].state
            if state == awaited_state:
                break
            else:
                time.sleep(1)

136
    def _get_instance_lbs(self, ec2_elbs=None):
137 138 139 140
        """Returns a list of ELBs attached to self.instance_id
        ec2_elbs: an optional list of elb names that will be used
                  for elb lookup instead of returning what elbs
                  are attached to self.instance_id"""
141 142 143 144 145 146 147

        try:
            elb = boto.connect_elb(self.ec2_access_key, self.ec2_secret_key)
        except boto.exception.NoAuthHandlerFound, e:
            self.module.fail_json(msg=str(e))
        elbs = elb.get_all_load_balancers()

148 149
        if ec2_elbs:
            lbs = sorted(lb for lb in elbs if lb.name in ec2_elbs)
150 151 152 153 154 155 156 157 158 159 160 161 162
        else:
            lbs = []
            for lb in elbs:
                for info in lb.instances:
                    if self.instance_id == info.id:
                        lbs.append(lb)
        return lbs


def main():

    module = AnsibleModule(
        argument_spec=dict(
163 164
            state={'required': True,
                    'choices': ['present', 'absent']},
165
            instance_id={'required': True},
166
            ec2_elbs={'default': None, 'required': False},
167 168 169 170 171 172 173
            ec2_secret_key={'default': None, 'aliases': ['EC2_SECRET_KEY']},
            ec2_access_key={'default': None, 'aliases': ['EC2_ACCESS_KEY']}
        )
    )

    ec2_secret_key = module.params['ec2_secret_key']
    ec2_access_key = module.params['ec2_access_key']
174
    ec2_elbs = module.params['ec2_elbs']
175

176
    if module.params['state'] == 'present' and 'ec2_elbs' not in module.params:
177 178 179 180 181 182 183 184
        module.fail_json(msg="ELBs are required for registration")

    if not ec2_secret_key and 'EC2_SECRET_KEY' in os.environ:
        ec2_secret_key = os.environ['EC2_SECRET_KEY']
    if not ec2_access_key and 'EC2_ACCESS_KEY' in os.environ:
        ec2_access_key = os.environ['EC2_ACCESS_KEY']

    instance_id = module.params['instance_id']
185
    elb_man = ElbManager(module, instance_id, ec2_elbs, ec2_access_key,
186 187
                         ec2_secret_key)

188
    if module.params['state'] == 'present':
189
        elb_man.register()
190
    elif module.params['state'] == 'absent':
191 192 193
        elb_man.deregister()

    ansible_facts = {'ec2_elbs': [lb.name for lb in elb_man.lbs]}
194
    ec2_facts_result = dict(changed=elb_man.changed, ansible_facts=ansible_facts)
195 196 197 198 199 200 201

    module.exit_json(**ec2_facts_result)

# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>

main()