#!/usr/bin/python DOCUMENTATION = ''' --- module: ec2_eip short_decription: associate an EC2 elastic IP with an instance. description: - This module associates AWS EC2 elastic IP addresses with instances options: instance_id: description: - The EC2 instance id required: true public_ip: description: - The elastic IP address to associate with the instance required: true state: description: - If present, associate the IP with the instance. - If absent, disassociate the IP with the instance. required: false choices: ['present', 'absent'] default: present ec2_url: description: - URL to use to connect to EC2-compatible cloud (by default the module will use EC2 endpoints) required: false default: null aliases: [ EC2_URL ] ec2_access_key: description: - EC2 access key. If not specified then the EC2_ACCESS_KEY environment variable is used. required: false default: null aliases: [ EC2_ACCESS_KEY ] ec2_secret_key: description: - EC2 secret key. If not specified then the EC2_SECRET_KEY environment variable is used. required: false default: null aliases: [ EC2_SECRET_KEY ] region: description: - the EC2 region to use required: false default: null aliases: [ ec2_region ] requirements: [ "boto" ] author: Lorin Hochstein ''' EXAMPLES = ''' - ec2_eip: instance_id=i-1212f003 ip=93.184.216.119 - ec2_eip: instance_id=i-1212f003 ip=93.184.216.119 state=absent ''' try: import boto.ec2 except ImportError: boto_found = False else: boto_found = True def connect(ec2_url, ec2_secret_key, ec2_access_key, region): """ Return an ec2 connection""" # allow environment variables to be used if ansible vars aren't set if not ec2_url and 'EC2_URL' in os.environ: ec2_url = os.environ['EC2_URL'] 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'] # If we have a region specified, connect to its endpoint. if region: try: ec2 = boto.ec2.connect_to_region(region, aws_access_key_id=ec2_access_key, aws_secret_access_key=ec2_secret_key) except boto.exception.NoAuthHandlerFound, e: module.fail_json(msg = str(" %s %s %s " % (region, ec2_access_key, ec2_secret_key))) # Otherwise, no region so we fallback to the old connection method else: try: if ec2_url: # if we have an URL set, connect to the specified endpoint ec2 = boto.connect_ec2_endpoint(ec2_url, ec2_access_key, ec2_secret_key) else: # otherwise it's Amazon. ec2 = boto.connect_ec2(ec2_access_key, ec2_secret_key) except boto.exception.NoAuthHandlerFound, e: module.fail_json(msg = str(e)) return ec2 def associate_ip_and_instance(ec2, public_ip, instance_id, module): if ip_is_associated_with_instance(ec2, public_ip, instance_id): module.exit_json(changed=False) # If we're in check mode, nothing else to do if module.check_mode: module.exit_json(changed=True) res = ec2.associate_address(instance_id, public_ip) if res: module.exit_json(changed=True) else: module.fail_json(msg="association failed") def disassociate_ip_and_instance(ec2, public_ip, instance_id, module): if not ip_is_associated_with_instance(ec2, public_ip, instance_id): module.exit_json(changed=False) # If we're in check mode, nothing else to do if module.check_mode: module.exit_json(changed=True) res = ec2.disassociate_address(public_ip) if res: module.exit_json(changed=True) else: module.fail_json(msg="disassociation failed") def ip_is_associated_with_instance(ec2, public_ip, instance_id): """ Check if the elastic IP is currently associated with the instance """ addresses = ec2.get_all_addresses([public_ip]) if addresses: return addresses[0].instance_id == instance_id else: return False def main(): module = AnsibleModule( argument_spec = dict( instance_id = dict(required=True), public_ip = dict(required=True, alises= ['ip'] ), state = dict(required=False, default='present', choices=['present', 'absent']), ec2_url = dict(required=False, aliases=['EC2_URL']), ec2_secret_key = dict(required=False, aliases=['EC2_SECRET_KEY'], no_log=True), ec2_access_key = dict(required=False, aliases=['EC2_ACCESS_KEY']), region = dict(required=False, aliases=['ec2_region']) ), supports_check_mode=True ) if not boto_found: module.fail_json(msg="boto is required") ec2 = connect(ec2_url=module.params.get('ec2_url'), ec2_secret_key=module.params.get('ec2_secret_key'), ec2_access_key=module.params.get('ec2_access_key'), region=module.params.get('region')) instance_id = module.params.get('instance_id') public_ip = module.params.get('public_ip') state = module.params.get('state') if state == 'present': associate_ip_and_instance(ec2, public_ip, instance_id, module) else: disassociate_ip_and_instance(ec2, public_ip, instance_id, module) # this is magic, see lib/ansible/module_common.py #<> if __name__ == '__main__': main()