changing arg name for consistency, absent/present for registration
This commit is contained in:
parent
e625155cd5
commit
a45b312907
1 changed files with 18 additions and 18 deletions
|
@ -28,7 +28,7 @@ version_added: "1.2"
|
|||
requirements: [ "boto" ]
|
||||
author: John Jarvis
|
||||
options:
|
||||
action:
|
||||
state:
|
||||
description:
|
||||
- register or deregister the instance
|
||||
required: true
|
||||
|
@ -38,7 +38,7 @@ options:
|
|||
- EC2 Instance ID
|
||||
required: true
|
||||
|
||||
elb_names:
|
||||
ec2_elbs:
|
||||
description:
|
||||
- List of ELB names, required for registration. The elb_ec2 fact should be used if there was a previous de-register.
|
||||
required: false
|
||||
|
@ -65,7 +65,7 @@ pre_tasks:
|
|||
local_action: ec2_elb
|
||||
args:
|
||||
instance_id: "{{ ansible_ec2_instance_id }}"
|
||||
action: 'deregister'
|
||||
state: 'absent'
|
||||
roles:
|
||||
- myrole
|
||||
post_tasks:
|
||||
|
@ -73,8 +73,8 @@ post_tasks:
|
|||
local_action: ec2_elb
|
||||
args:
|
||||
instance_id: "{{ ansible_ec2_instance_id }}"
|
||||
elb_names: "{{ ec2_elbs }}"
|
||||
action: 'register'
|
||||
ec2_elbs: "{{ ec2_elbs }}"
|
||||
state: 'present'
|
||||
"""
|
||||
|
||||
import time
|
||||
|
@ -91,13 +91,13 @@ except ImportError:
|
|||
class ElbManager:
|
||||
"""Handles EC2 instance ELB registration and de-registration"""
|
||||
|
||||
def __init__(self, module, instance_id=None, elb_names=None,
|
||||
def __init__(self, module, instance_id=None, ec2_elbs=None,
|
||||
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
|
||||
self.lbs = self._get_instance_lbs(elb_names)
|
||||
self.lbs = self._get_instance_lbs(ec2_elbs)
|
||||
# if there are no ELBs to operate on
|
||||
# there will be no changes made
|
||||
if len(self.lbs) > 0:
|
||||
|
@ -133,7 +133,7 @@ class ElbManager:
|
|||
else:
|
||||
time.sleep(1)
|
||||
|
||||
def _get_instance_lbs(self, elb_names=None):
|
||||
def _get_instance_lbs(self, ec2_elbs=None):
|
||||
"""Returns a list of ELBs attached to self.instance_id"""
|
||||
|
||||
try:
|
||||
|
@ -142,8 +142,8 @@ class ElbManager:
|
|||
self.module.fail_json(msg=str(e))
|
||||
elbs = elb.get_all_load_balancers()
|
||||
|
||||
if elb_names:
|
||||
lbs = sorted(lb for lb in elbs if lb.name in elb_names)
|
||||
if ec2_elbs:
|
||||
lbs = sorted(lb for lb in elbs if lb.name in ec2_elbs)
|
||||
else:
|
||||
lbs = []
|
||||
for lb in elbs:
|
||||
|
@ -157,10 +157,10 @@ def main():
|
|||
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
action={'required': True,
|
||||
'choices': ['register', 'deregister']},
|
||||
state={'required': True,
|
||||
'choices': ['present', 'absent']},
|
||||
instance_id={'required': True},
|
||||
elb_names={'default': None, 'required': False},
|
||||
ec2_elbs={'default': None, 'required': False},
|
||||
ec2_secret_key={'default': None, 'aliases': ['EC2_SECRET_KEY']},
|
||||
ec2_access_key={'default': None, 'aliases': ['EC2_ACCESS_KEY']}
|
||||
)
|
||||
|
@ -168,9 +168,9 @@ def main():
|
|||
|
||||
ec2_secret_key = module.params['ec2_secret_key']
|
||||
ec2_access_key = module.params['ec2_access_key']
|
||||
elb_names = module.params['elb_names']
|
||||
ec2_elbs = module.params['ec2_elbs']
|
||||
|
||||
if module.params['action'] == 'register' and 'elb_names' not in module.params:
|
||||
if module.params['state'] == 'register' and 'ec2_elbs' not in module.params:
|
||||
module.fail_json(msg="ELBs are required for registration")
|
||||
|
||||
if not ec2_secret_key and 'EC2_SECRET_KEY' in os.environ:
|
||||
|
@ -179,12 +179,12 @@ def main():
|
|||
ec2_access_key = os.environ['EC2_ACCESS_KEY']
|
||||
|
||||
instance_id = module.params['instance_id']
|
||||
elb_man = ElbManager(module, instance_id, elb_names, ec2_access_key,
|
||||
elb_man = ElbManager(module, instance_id, ec2_elbs, ec2_access_key,
|
||||
ec2_secret_key)
|
||||
|
||||
if module.params['action'] == 'register':
|
||||
if module.params['state'] == 'register':
|
||||
elb_man.register()
|
||||
elif module.params['action'] == 'deregister':
|
||||
elif module.params['state'] == 'deregister':
|
||||
elb_man.deregister()
|
||||
|
||||
ansible_facts = {'ec2_elbs': [lb.name for lb in elb_man.lbs]}
|
||||
|
|
Loading…
Reference in a new issue