1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge branch 'jsmartin-fix_eip_regression' into devel

This commit is contained in:
James Cammarata 2014-08-05 09:35:37 -05:00
commit 2d84a9d32e
3 changed files with 92 additions and 59 deletions

View file

@ -41,6 +41,11 @@ options:
required: false required: false
default: false default: false
version_added: "1.6" version_added: "1.6"
wait_timeout:
description:
- how long to wait in seconds for newly provisioned EIPs to become available
default: 300
version_added: "1.7"
extends_documentation_fragment: aws extends_documentation_fragment: aws
author: Lorin Hochstein <lorin@nimbisservices.com> author: Lorin Hochstein <lorin@nimbisservices.com>
@ -91,6 +96,8 @@ else:
boto_found = True boto_found = True
wait_timeout = 0
def associate_ip_and_instance(ec2, address, instance_id, module): def associate_ip_and_instance(ec2, address, instance_id, module):
if ip_is_associated_with_instance(ec2, address.public_ip, 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) module.exit_json(changed=False, public_ip=address.public_ip)
@ -136,24 +143,27 @@ def disassociate_ip_and_instance(ec2, address, instance_id, module):
def find_address(ec2, public_ip, module): def find_address(ec2, public_ip, module):
""" Find an existing Elastic IP address """ """ Find an existing Elastic IP address """
if wait_timeout != 0:
wait_timeout = 600 timeout = time.time() + wait_timeout
wait_timeout = time.time() + wait_timeout while timeout > time.time():
try:
while wait_timeout > time.time(): addresses = ec2.get_all_addresses([public_ip])
break
except boto.exception.EC2ResponseError, e:
if "Address '%s' not found." % public_ip in e.message :
pass
else:
module.fail_json(msg=str(e.message))
time.sleep(5)
if timeout <= time.time():
module.fail_json(msg = "wait for EIPs timeout on %s" % time.asctime())
else:
try: try:
addresses = ec2.get_all_addresses([public_ip]) addresses = ec2.get_all_addresses([public_ip])
break
except boto.exception.EC2ResponseError, e: except boto.exception.EC2ResponseError, e:
if "Address '%s' not found." % public_ip in e.message : module.fail_json(msg=str(e.message))
pass
else:
module.fail_json(msg=str(e.message))
time.sleep(5)
if wait_timeout <= time.time():
module.fail_json(msg = "wait for EIPs timeout on %s" % time.asctime())
return addresses[0] return addresses[0]
@ -232,6 +242,7 @@ def main():
choices=['present', 'absent']), choices=['present', 'absent']),
in_vpc = dict(required=False, type='bool', default=False), in_vpc = dict(required=False, type='bool', default=False),
reuse_existing_ip_allowed = dict(required=False, type='bool', default=False), reuse_existing_ip_allowed = dict(required=False, type='bool', default=False),
wait_timeout = dict(default=300),
) )
) )
@ -250,29 +261,40 @@ def main():
state = module.params.get('state') state = module.params.get('state')
in_vpc = module.params.get('in_vpc') in_vpc = module.params.get('in_vpc')
domain = "vpc" if in_vpc else None domain = "vpc" if in_vpc else None
reuse_existing_ip_allowed = module.params.get('reuse_existing_ip_allowed'); reuse_existing_ip_allowed = module.params.get('reuse_existing_ip_allowed')
new_eip_timeout = int(module.params.get('wait_timeout'))
if state == 'present': if state == 'present':
if public_ip is None: # Allocate an EIP and exit
if instance_id is None: if not instance_id and not public_ip:
address = allocate_address(ec2, domain, module, reuse_existing_ip_allowed) address = allocate_address(ec2, domain, module, reuse_existing_ip_allowed)
module.exit_json(changed=True, public_ip=address.public_ip) module.exit_json(changed=True, public_ip=address.public_ip)
else:
# Determine if the instance is inside a VPC or not # Return the EIP object since we've been given a public IP
instance = find_instance(ec2, instance_id, module) if public_ip:
if instance.vpc_id != None:
domain = "vpc"
address = allocate_address(ec2, domain, module, reuse_existing_ip_allowed)
else:
address = find_address(ec2, public_ip, module) address = find_address(ec2, public_ip, module)
# Allocate an IP for instance since no public_ip was provided
if instance_id and not public_ip:
instance = find_instance(ec2, instance_id, module)
if instance.vpc_id:
domain = "vpc"
address = allocate_address(ec2, domain, module, reuse_existing_ip_allowed)
# overriding the timeout since this is a a newly provisioned ip
global wait_timeout
wait_timeout = new_eip_timeout
# Associate address object (provided or allocated) with instance
associate_ip_and_instance(ec2, address, instance_id, module) associate_ip_and_instance(ec2, address, instance_id, module)
else: else:
if instance_id is None: #disassociating address from instance
release_address(ec2, public_ip, module) if instance_id:
else:
address = find_address(ec2, public_ip, module) address = find_address(ec2, public_ip, module)
disassociate_ip_and_instance(ec2, address, instance_id, module) disassociate_ip_and_instance(ec2, address, instance_id, module)
#releasing address
else:
release_address(ec2, public_ip, module)
# import module snippets # import module snippets

View file

@ -8,7 +8,7 @@
#- { role: test_ec2_tag, tags: test_ec2_tag } #- { role: test_ec2_tag, tags: test_ec2_tag }
#- { role: test_ec2_facts, tags: test_ec2_facts } #- { role: test_ec2_facts, tags: test_ec2_facts }
- { role: test_ec2_elb_lb, tags: test_ec2_elb_lb } - { role: test_ec2_elb_lb, tags: test_ec2_elb_lb }
#- { role: test_ec2_eip, tags: test_ec2_eip } - { role: test_ec2_eip, tags: test_ec2_eip }
#- { role: test_ec2_ami, tags: test_ec2_ami } #- { role: test_ec2_ami, tags: test_ec2_ami }
#- { role: test_ec2, tags: test_ec2 } #- { role: test_ec2, tags: test_ec2 }
- { role: test_ec2_asg, tags: test_ec2_asg } - { role: test_ec2_asg, tags: test_ec2_asg }

View file

@ -32,17 +32,28 @@
# test credential parameters # test credential parameters
# ============================================================ # ============================================================
# create a keypair using the ssh key
- name: create the keypair for ec2
ec2_key:
name: "{{ resource_prefix }}"
region: "{{ ec2_region }}"
ec2_access_key: "{{ ec2_access_key }}"
ec2_secret_key: "{{ ec2_secret_key }}"
key_material: "{{ key_material }}"
wait: yes
state: present
# ============================================================
# create test instance # create test instance
- name: create test instance - name: create test instance
local_action: local_action:
module: ec2 module: ec2
key_name: "{{ eip_ec2_keyname }}" key_name: "{{ resource_prefix }}"
region: "{{ ec2_region }}" region: "{{ ec2_region }}"
instance_type: t1.micro instance_type: t1.micro
image: "{{ eip_ec2_image }}" image: ami-fb8e9292
wait: yes wait: yes
instance_tags: instance_tags:
Name: "{{ tag_prefix }}" Name: "{{ tag_prefix }}"
@ -243,32 +254,32 @@
# ============================================================ # ============================================================
# re-use existing VPC EIP with instance # re-use existing VPC EIP with instance
# DISABLED BY JCAMMARATA
- name: re-use existing EIP with instance #- name: re-use existing VPC EIP with instance
ec2_eip: # ec2_eip:
in_vpc=yes # in_vpc=yes
reuse_existing_ip_allowed=yes # reuse_existing_ip_allowed=yes
instance_id={{ instance_id }} # instance_id={{ instance_id }}
ec2_access_key={{ ec2_access_key }} # ec2_access_key={{ ec2_access_key }}
ec2_secret_key={{ ec2_secret_key }} # ec2_secret_key={{ ec2_secret_key }}
region={{ ec2_region }} # region={{ ec2_region }}
register: result # register: result
#
- name: assert new EIP was assigned #- name: assert new VPC EIP was assigned
assert: # assert:
that: # that:
- '"failed" not in result' # - '"failed" not in result'
- '"public_ip" in result' # - '"public_ip" in result'
#
#
#- name: disassociate VPC EIP associated with instance
# ec2_eip: state=absent public_ip={{ eip }} region={{ ec2_region }}
# ec2_access_key={{ ec2_access_key }}
# ec2_secret_key={{ ec2_secret_key }}
# instance_id={{ instance_id }}
- name: disassociate EIP associated with instance - name: deactivate VPC EIP
ec2_eip: state=absent public_ip={{ eip }} region={{ ec2_region }}
ec2_access_key={{ ec2_access_key }}
ec2_secret_key={{ ec2_secret_key }}
instance_id={{ instance_id }}
- name: deactivate EIP
ec2_eip: state=absent public_ip={{ eip }} region={{ ec2_region }} ec2_eip: state=absent public_ip={{ eip }} region={{ ec2_region }}
ec2_access_key={{ ec2_access_key }} ec2_access_key={{ ec2_access_key }}
ec2_secret_key={{ ec2_secret_key }} ec2_secret_key={{ ec2_secret_key }}