changing arg name for consistency, absent/present for registration

This commit is contained in:
John Jarvis 2013-05-02 21:29:36 -04:00
parent 43910bd290
commit 8098b80e2d

View file

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