def associate_ip_and_device(ec2, address, private_ip_address, device_id, check_mode, isinstance=True): if address_is_associated_with_device(ec2, address, device_id, isinstance): return {'changed': False} # If we're in check mode, nothing else to do if not check_mode: if isinstance: if address.domain == "vpc": res = ec2.associate_address( device_id, allocation_id=address.allocation_id, private_ip_address=private_ip_address) else: res = ec2.associate_address( device_id, public_ip=address.public_ip, private_ip_address=private_ip_address) else: res = ec2.associate_address(network_interface_id=device_id, allocation_id=address.allocation_id, private_ip_address=private_ip_address) if not res: raise EIPException('association failed') return {'changed': True}
def change_ip(instance_id): latest_ip = get_latest_ip(instance_id) if not latest_ip: raise RuntimeError('Invalid ec2 instance_id') allocation_id = get_allocation_id(latest_ip) new_ip = str(ec2.allocate_address()).split(':', 1)[-1] ec2.associate_address(instance_id, new_ip) ec2.release_address(allocation_id=allocation_id)
def associate_ip_and_instance(ec2, address, instance_id, module): if ip_is_associated_with_instance(ec2, address.public_ip, instance_id, module): module.exit_json(changed=False, public_ip=address.public_ip) # If we're in check mode, nothing else to do if module.check_mode: module.exit_json(changed=True) try: if address.domain == "vpc": res = ec2.associate_address(instance_id, allocation_id=address.allocation_id) else: res = ec2.associate_address(instance_id, public_ip=address.public_ip) except boto.exception.EC2ResponseError, e: module.fail_json(msg=str(e))
def associate_ip_and_instance(ec2, address, instance_id, check_mode): if address_is_associated_with_instance(ec2, address, instance_id): return {'changed': False} # If we're in check mode, nothing else to do if not check_mode: if address.domain == 'vpc': res = ec2.associate_address(instance_id, allocation_id=address.allocation_id) else: res = ec2.associate_address(instance_id, public_ip=address.public_ip) if not res: raise EIPException('association failed') return {'changed': True}
def associate_ip_and_device(ec2, address, device_id, check_mode, isinstance=True): if address_is_associated_with_device(ec2, address, device_id, isinstance): return {"changed": False} # If we're in check mode, nothing else to do if not check_mode: if isinstance: if address.domain == "vpc": res = ec2.associate_address(device_id, allocation_id=address.allocation_id) else: res = ec2.associate_address(device_id, public_ip=address.public_ip) else: res = ec2.associate_address(network_interface_id=device_id, allocation_id=address.allocation_id) if not res: raise EIPException("association failed") return {"changed": True}
def associate_ip_and_eni(ec2, module, address, eni_id, private_ip_address=None): if ip_is_associated_with_eni(ec2, address.public_ip, eni_id, module): module.exit_json(changed=False, public_ip=address.public_ip) # If we're in check mode, nothing else to do if module.check_mode: module.exit_json(changed=True) try: if address.domain == "vpc": res = ec2.associate_address(network_interface_id=eni_id, allocation_id=address.allocation_id, private_ip_address=private_ip_address) else: res = ec2.associate_address(network_interface_id=eni_id, public_ip=address.public_ip, private_ip_address=private_ip_address) except boto.exception.EC2ResponseError, e: module.fail_json(msg=str(e))
def associate_address(ec2, allocation_id, instance_id): return ec2.associate_address(instance_id=instance_id, allocation_id=allocation_id, allow_reassociation=True)
def eip_associate(instanceid): ec2 = boto.ec2.connect_to_region(region) address = ec2.get_all_addresses(addresses="5.2.29.7") ec2.associate_address(instanceid, address[0].public_ip)
def eip_associate(instanceid): ec2 = boto.ec2.connect_to_region(region) address = ec2.get_all_addresses(addresses='5.2.29.7') ec2.associate_address(instanceid, address[0].public_ip)
- path: /usr/bin/allocate_eip.py content: | #!/usr/bin/python import requests import boto3 import json instance_data = requests.get("http://169.254.169.254/latest/dynamic/instance-identity/document") response_json = instance_data.json() region = response_json.get('region') instance_id = response_json.get('instanceId') ec2 = boto3.client('ec2', region_name=region) try: allocate_eip = ec2.associate_address(AllocationId='eipalloc-0f7526dce6e0f1db8', InstanceId=instance_id) except: print("Associate IP failed") try: create_tag = ec2.create_tags(Resources=[instance_id], Tags=[{'Key':'ElasticIp', 'Value':'eipalloc-0f7526dce6e0f1db8'}]) except: print("Create tag failed") runcmd: - [ pip, install, boto3 ] - [ python, /usr/bin/rename_instance.py ] - [ sleep, 15 ] - [ python, /usr/bin/allocate_eip.py ] - [ service, sensu-client, stop ] - [ salt-call, saltutil.sync_all ]